smart-file-upload
Version:
Smart file upload
166 lines • 7.71 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
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 __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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.FileUploadService = void 0;
const common_1 = require("@nestjs/common");
const path = __importStar(require("path"));
const file_upload_schema_1 = require("./schemas/file-upload.schema");
const mongoose_1 = require("@nestjs/mongoose");
const mongoose_2 = require("mongoose");
const enum_1 = require("./common/enum");
const ffmpeg_service_1 = require("./common/ffmpeg.service");
const function_1 = require("./common/function");
const bull_1 = require("@nestjs/bull");
const queue_enum_1 = require("./queue/queue.enum");
let FileUploadService = class FileUploadService {
constructor(fileUploadModel, ffmpegService, imageMagickQueue, ffmpegVideoQueue, fileUploadQueue) {
this.fileUploadModel = fileUploadModel;
this.ffmpegService = ffmpegService;
this.imageMagickQueue = imageMagickQueue;
this.ffmpegVideoQueue = ffmpegVideoQueue;
this.fileUploadQueue = fileUploadQueue;
}
async upload(file) {
this.init(file);
if (this.type === enum_1.FileTypeEnum.VIDEO) {
await this.processVideo(file);
}
if (this.type === enum_1.FileTypeEnum.AUDIO) {
await this.processAudio(file);
}
const data = await this.fileUploadModel.create({
sizeInBytes: file.size,
mimeType: file.mimetype,
type: this.type,
originalName: file.originalname,
fileName: file.filename,
ext: this.ext,
thumbnailName: this.thumbnailName,
durationInSeconds: this.durationInSeconds,
durationInString: this.durationInString,
});
if (this.type === enum_1.FileTypeEnum.IMAGE) {
await this.imageMagickQueue.add(queue_enum_1.ImageMagickQueueEnum.COMPRESS_IMAGE, {
id: data.id,
name: this.name,
ext: this.ext,
fileName: file.filename,
uploads: [],
all: [file.filename],
});
}
else if (this.type === enum_1.FileTypeEnum.VIDEO) {
await this.ffmpegVideoQueue.add(queue_enum_1.FFmpegVideoQueueEnum.GENERATE_ADAPTIVE_BITRATE_VIDEO, {
id: data.id,
name: this.name,
ext: this.ext,
fileName: file.filename,
uploads: [this.thumbnailName],
all: [file.filename, this.thumbnailName],
});
}
else {
await this.fileUploadQueue.add(queue_enum_1.FileUploadQueueEnum.MULTIPART_UPLOAD, {
id: data.id,
name: this.name,
ext: this.ext,
fileName: file.filename,
uploads: [file.filename],
all: [file.filename],
});
}
return data.id;
}
init(file) {
this.type = file.mimetype.split("/")[0].toUpperCase();
const parsed = path.parse(file.filename);
this.name = parsed.name;
this.ext = parsed.ext.substring(1);
}
async processVideo(file) {
this.thumbnailName = this.name + ".jpg";
const durationInSecondsCommand = this.ffmpegService.durationInSecondsCommand(file.filename);
const durationInStringCommand = this.ffmpegService.durationInStringCommand(file.filename);
const thumbnailCommand = this.ffmpegService.thumbnailCommand(file.filename, this.thumbnailName);
const [durationInSeconds, durationInString] = await Promise.all([
(0, function_1.execAsync)(durationInSecondsCommand),
(0, function_1.execAsync)(durationInStringCommand),
(0, function_1.execAsync)(thumbnailCommand),
]);
this.durationInSeconds = +durationInSeconds;
this.durationInString = durationInString;
}
async processAudio(file) {
const durationInSecondsCommand = this.ffmpegService.durationInSecondsCommand(file.filename);
const durationInStringCommand = this.ffmpegService.durationInStringCommand(file.filename);
const [durationInSeconds, durationInString] = await Promise.all([
await (0, function_1.execAsync)(durationInSecondsCommand),
await (0, function_1.execAsync)(durationInStringCommand),
]);
this.durationInSeconds = +durationInSeconds;
this.durationInString = durationInString;
}
async getFileDetail(id) {
const data = (await this.fileUploadModel
.findOne({ _id: id }, { _id: 0, __v: 0 })
.lean());
if (data.upload) {
data.url = process.env.S3_URL + "/" + data.fileName;
data.thumbnailUrl = data.thumbnailName
? process.env.S3_URL + "/" + data.thumbnailName
: undefined;
}
else {
data.url = process.env.SERVER_URL + "/" + data.fileName;
data.thumbnailUrl = data.thumbnailName
? process.env.SERVER_URL + "/" + data.thumbnailName
: undefined;
}
return data;
}
};
FileUploadService = __decorate([
(0, common_1.Injectable)({ scope: common_1.Scope.REQUEST }),
__param(0, (0, mongoose_1.InjectModel)(file_upload_schema_1.FileUpload.name)),
__param(2, (0, bull_1.InjectQueue)(queue_enum_1.ImageMagickQueueEnum.NAME)),
__param(3, (0, bull_1.InjectQueue)(queue_enum_1.FFmpegVideoQueueEnum.NAME)),
__param(4, (0, bull_1.InjectQueue)(queue_enum_1.FileUploadQueueEnum.NAME)),
__metadata("design:paramtypes", [mongoose_2.Model,
ffmpeg_service_1.FFmpegService, Object, Object, Object])
], FileUploadService);
exports.FileUploadService = FileUploadService;
//# sourceMappingURL=smart-file-upload.service.js.map