????

Your IP : 216.73.216.152


Current Path : C:/inetpub/vhost/binhdinhinvest.gdtvietnam.com/api/dist/cms/controller/
Upload File :
Current File : C:/inetpub/vhost/binhdinhinvest.gdtvietnam.com/api/dist/cms/controller/upload.controller.js

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DownloadControler = exports.UploadController = void 0;
const openapi = require("@nestjs/swagger");
const common_1 = require("@nestjs/common");
const crud_1 = require("@rewiko/crud");
const platform_express_1 = require("@nestjs/platform-express");
const swagger_1 = require("@nestjs/swagger");
const express = require("express");
const fs_1 = require("fs");
const url_1 = require("url");
const multer_1 = require("multer");
const path_1 = require("path");
const service_1 = require("../service");
const cms_interceptor_1 = require("../interceptor/cms.interceptor");
const entity_1 = require("../entity");
const auth_guard_1 = require("../../auth/auth.guard");
let UploadController = class UploadController {
    constructor(service) {
        this.service = service;
    }
    get base() {
        return this;
    }
    createOne(req, crudReq, body, file) {
        body.tenFile = file.originalname;
        body.duongDan = new url_1.URL(file.filename, req.res.locals.fileUrl).href;
        body.mimeType = file.mimetype;
        body.dungLuong = file.size;
        return this.base.createOneBase(crudReq, body);
    }
};
__decorate([
    (0, common_1.UseGuards)(auth_guard_1.JwtAuthGuard),
    (0, swagger_1.ApiBearerAuth)('JwtBearerToken'),
    (0, crud_1.Override)('createOneBase'),
    (0, swagger_1.ApiConsumes)('multipart/form-data'),
    (0, swagger_1.ApiBody)({
        schema: {
            type: 'object',
            properties: {
                file: {
                    type: 'string',
                    format: 'binary'
                },
                moTa: { type: 'string' }
            }
        }
    }),
    (0, common_1.HttpCode)(201),
    openapi.ApiResponse({ status: 201, type: require("../entity/upload.entity").Upload }),
    __param(0, (0, common_1.Request)()),
    __param(1, (0, crud_1.ParsedRequest)()),
    __param(2, (0, crud_1.ParsedBody)()),
    __param(3, (0, common_1.UploadedFile)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, entity_1.Upload, Object]),
    __metadata("design:returntype", Promise)
], UploadController.prototype, "createOne", null);
UploadController = __decorate([
    (0, crud_1.Crud)({
        model: { type: entity_1.Upload },
        params: {
            upload_id: {
                field: 'id',
                primary: true,
                type: 'string',
            }
        },
        query: {
            join: {
                nguoiTao: {
                    allow: ['tenHienThi', 'email'],
                    alias: 'nguoiTao'
                }
            }
        },
        routes: {
            exclude: ['createManyBase'],
            createOneBase: {
                interceptors: [(0, platform_express_1.FileInterceptor)("file", {
                        storage: (0, multer_1.diskStorage)({
                            destination: (req, file, cb) => {
                                try {
                                    const now = new Date();
                                    const url = `/upload/${now.getFullYear().toString()}/${(now.getMonth() + 1).toString().padStart(2, '0')}/`;
                                    req.res.locals.fileUrl = url;
                                    const dir = (0, path_1.join)(process.cwd(), url);
                                    if (!(0, fs_1.existsSync)(dir))
                                        (0, fs_1.mkdirSync)(dir, { recursive: true });
                                    return cb(null, dir);
                                }
                                catch (err) {
                                    return cb(err, './upload');
                                }
                            },
                            filename: (req, file, cb) => {
                                const randomName = Array(32).fill(null).map(() => (Math.round(Math.random() * 16)).toString(16)).join('');
                                return cb(null, `${randomName}${(0, path_1.extname)(file.originalname)}`);
                            }
                        }),
                        limits: {
                            fileSize: 20971520,
                            files: 1,
                            fields: 5
                        }
                    }), cms_interceptor_1.CmsInterceptor]
            },
            updateOneBase: { decorators: [(0, common_1.UseGuards)(auth_guard_1.JwtAuthGuard), (0, swagger_1.ApiBearerAuth)('JwtBearerToken')] },
            replaceOneBase: { decorators: [(0, common_1.UseGuards)(auth_guard_1.JwtAuthGuard), (0, swagger_1.ApiBearerAuth)('JwtBearerToken')] },
            deleteOneBase: { decorators: [(0, common_1.UseGuards)(auth_guard_1.JwtAuthGuard), (0, swagger_1.ApiBearerAuth)('JwtBearerToken')] }
        },
    }),
    (0, swagger_1.ApiTags)('Quản lý tập tin'),
    (0, common_1.Controller)('tep-tin'),
    __metadata("design:paramtypes", [service_1.UploadService])
], UploadController);
exports.UploadController = UploadController;
let DownloadControler = class DownloadControler {
    constructor(service) {
        this.service = service;
    }
    async download(req, res, util) {
        const duongDan = req.url.replace(/^\/api/, '');
        const upload = await this.service.findOne({ where: { duongDan } });
        const jimp = await this.service.download(duongDan, util);
        const mimeType = upload.mimeType || jimp.getMIME();
        res.type(mimeType);
        res.status(200).send(await jimp.getBufferAsync(mimeType));
    }
};
__decorate([
    (0, common_1.Get)('*'),
    (0, swagger_1.ApiExcludeEndpoint)(true),
    openapi.ApiResponse({ status: 200 }),
    __param(0, (0, common_1.Request)()),
    __param(1, (0, common_1.Response)()),
    __param(2, (0, common_1.Query)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, entity_1.ImageUtil]),
    __metadata("design:returntype", Promise)
], DownloadControler.prototype, "download", null);
DownloadControler = __decorate([
    (0, common_1.Controller)('upload'),
    __metadata("design:paramtypes", [service_1.UploadService])
], DownloadControler);
exports.DownloadControler = DownloadControler;