@tc-libs/file-handler 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/package.json +11 -0
- package/src/file-handler.module.d.ts +6 -0
- package/src/file-handler.module.js +53 -0
- package/src/file-handler.module.js.map +1 -0
- package/src/file-handler.module.options.d.ts +12 -0
- package/src/file-handler.module.options.js +5 -0
- package/src/file-handler.module.options.js.map +1 -0
- package/src/index.d.ts +6 -0
- package/src/index.js +10 -0
- package/src/index.js.map +1 -0
- package/src/interfaces/file-handler.interface.d.ts +43 -0
- package/src/interfaces/file-handler.interface.js +3 -0
- package/src/interfaces/file-handler.interface.js.map +1 -0
- package/src/interfaces/file.interface.d.ts +12 -0
- package/src/interfaces/file.interface.js +3 -0
- package/src/interfaces/file.interface.js.map +1 -0
- package/src/repository/entities/file.entity.d.ts +43 -0
- package/src/repository/entities/file.entity.js +132 -0
- package/src/repository/entities/file.entity.js.map +1 -0
- package/src/repository/file.repository.module.d.ts +2 -0
- package/src/repository/file.repository.module.js +27 -0
- package/src/repository/file.repository.module.js.map +1 -0
- package/src/repository/index.d.ts +3 -0
- package/src/repository/index.js +7 -0
- package/src/repository/index.js.map +1 -0
- package/src/repository/repositories/file.repository.d.ts +11 -0
- package/src/repository/repositories/file.repository.js +28 -0
- package/src/repository/repositories/file.repository.js.map +1 -0
- package/src/services/file-handler.service.d.ts +26 -0
- package/src/services/file-handler.service.js +147 -0
- package/src/services/file-handler.service.js.map +1 -0
- package/src/services/file-handler.text.service.d.ts +10 -0
- package/src/services/file-handler.text.service.js +63 -0
- package/src/services/file-handler.text.service.js.map +1 -0
- package/src/services/file-handler.video.service.d.ts +13 -0
- package/src/services/file-handler.video.service.js +147 -0
- package/src/services/file-handler.video.service.js.map +1 -0
- package/src/services/handlers/audio.d.ts +0 -0
- package/src/services/handlers/audio.js +174 -0
- package/src/services/handlers/audio.js.map +1 -0
- package/src/services/handlers/file.d.ts +5 -0
- package/src/services/handlers/file.js +32 -0
- package/src/services/handlers/file.js.map +1 -0
- package/src/services/handlers/index.d.ts +2 -0
- package/src/services/handlers/index.js +8 -0
- package/src/services/handlers/index.js.map +1 -0
- package/src/services/handlers/jpg.d.ts +5 -0
- package/src/services/handlers/jpg.js +96 -0
- package/src/services/handlers/jpg.js.map +1 -0
- package/src/services/handlers/png.d.ts +0 -0
- package/src/services/handlers/png.js +2 -0
- package/src/services/handlers/png.js.map +1 -0
- package/src/services/handlers/video.d.ts +0 -0
- package/src/services/handlers/video.js +174 -0
- package/src/services/handlers/video.js.map +1 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var FileHandlerService_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.FileHandlerService = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const common_1 = require("@nestjs/common");
|
|
7
|
+
const storage_1 = require("@tc-libs/storage");
|
|
8
|
+
const bytes_1 = tslib_1.__importDefault(require("bytes"));
|
|
9
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
10
|
+
const path_1 = require("path");
|
|
11
|
+
const uuid_1 = require("uuid");
|
|
12
|
+
// import { WorkBook, utils } from 'xlsx';
|
|
13
|
+
const promises_1 = require("fs/promises");
|
|
14
|
+
const file_handler_module_options_1 = require("../file-handler.module.options");
|
|
15
|
+
const handlers_1 = require("./handlers");
|
|
16
|
+
const file_1 = require("./handlers/file");
|
|
17
|
+
let FileHandlerService = FileHandlerService_1 = class FileHandlerService {
|
|
18
|
+
constructor(_options, _storage) {
|
|
19
|
+
this._options = _options;
|
|
20
|
+
this._storage = _storage;
|
|
21
|
+
this.logger = new common_1.Logger(FileHandlerService_1.name);
|
|
22
|
+
// const workDir = this._config.getOrThrow('file.workDir');
|
|
23
|
+
this._imgConf = {
|
|
24
|
+
// hasToClean: this._config.getOrThrow('file.hasToClean'),
|
|
25
|
+
quality: this._options.image.quality,
|
|
26
|
+
resizes: this._options.resizes,
|
|
27
|
+
// workDirectory: workDir,
|
|
28
|
+
};
|
|
29
|
+
// if (!existsSync(workDir)) mkdirSync(workDir);
|
|
30
|
+
}
|
|
31
|
+
async fileToBuffer(filePath) {
|
|
32
|
+
try {
|
|
33
|
+
const buffer = await (0, promises_1.readFile)(filePath);
|
|
34
|
+
return buffer;
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
console.error('Error reading file:', error);
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// createExcelWorkbook(
|
|
42
|
+
// rows: IFileRows[],
|
|
43
|
+
// options?: IFileCreateExcelWorkbookOptions
|
|
44
|
+
// ): WorkBook {
|
|
45
|
+
// // headers
|
|
46
|
+
// const headers = rows.length > 0 ? Object.keys(rows[0]) : [];
|
|
47
|
+
// // worksheet
|
|
48
|
+
// const worksheet = utils.json_to_sheet(rows);
|
|
49
|
+
// // workbook
|
|
50
|
+
// const workbook = utils.book_new();
|
|
51
|
+
// utils.sheet_add_aoa(worksheet, [headers], { origin: 'A1' });
|
|
52
|
+
// utils.book_append_sheet(
|
|
53
|
+
// workbook,
|
|
54
|
+
// worksheet,
|
|
55
|
+
// options?.sheetName ?? 'Sheet 1'
|
|
56
|
+
// );
|
|
57
|
+
// return workbook;
|
|
58
|
+
// }
|
|
59
|
+
convertToBytes(megabytes) {
|
|
60
|
+
return (0, bytes_1.default)(megabytes);
|
|
61
|
+
}
|
|
62
|
+
async handleFiles(files) {
|
|
63
|
+
// array of url responses
|
|
64
|
+
const responses = [];
|
|
65
|
+
for (const file of files) {
|
|
66
|
+
responses.push(await this.handleFile(file));
|
|
67
|
+
}
|
|
68
|
+
return responses;
|
|
69
|
+
}
|
|
70
|
+
getFileInfo(file) {
|
|
71
|
+
// get mime type
|
|
72
|
+
const mime = file.mimetype;
|
|
73
|
+
// get file type
|
|
74
|
+
const fileType = mime.split('/')[0];
|
|
75
|
+
// remote folder + filename
|
|
76
|
+
const hashName = this.hashName(file.originalname);
|
|
77
|
+
// get extension
|
|
78
|
+
const ext = (0, path_1.parse)(file.originalname).ext;
|
|
79
|
+
return {
|
|
80
|
+
mime,
|
|
81
|
+
fileType,
|
|
82
|
+
hashName,
|
|
83
|
+
ext,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
async handleFile(file, options = { acl: storage_1.ACL.public_read }) {
|
|
87
|
+
// get mime type
|
|
88
|
+
const mime = file.mimetype;
|
|
89
|
+
// get file type
|
|
90
|
+
const fileType = mime.split('/')[0];
|
|
91
|
+
// remote folder + filename
|
|
92
|
+
const hashName = this.hashName(file.originalname);
|
|
93
|
+
// get extension
|
|
94
|
+
const ext = (0, path_1.parse)(file.originalname).ext;
|
|
95
|
+
// content
|
|
96
|
+
const content = file.buffer;
|
|
97
|
+
// final response
|
|
98
|
+
let response;
|
|
99
|
+
switch (fileType) {
|
|
100
|
+
case 'image':
|
|
101
|
+
if (ext === '.gif') {
|
|
102
|
+
response = await (0, file_1.handleFile)(ext, hashName, file, mime, content, fileType, this._storage, options);
|
|
103
|
+
}
|
|
104
|
+
else
|
|
105
|
+
response = await (0, handlers_1.handleImg)(this._imgConf, ext, hashName, file, mime, content, fileType, this._storage, options);
|
|
106
|
+
break;
|
|
107
|
+
// case 'video':
|
|
108
|
+
// response = await handleVideo(
|
|
109
|
+
// this._imgConf,
|
|
110
|
+
// ext,
|
|
111
|
+
// hashName,
|
|
112
|
+
// file,
|
|
113
|
+
// mime,
|
|
114
|
+
// content,
|
|
115
|
+
// fileType,
|
|
116
|
+
// this._storage,
|
|
117
|
+
// );
|
|
118
|
+
// break;
|
|
119
|
+
// case 'audio':
|
|
120
|
+
// response = await handleAudio(
|
|
121
|
+
// this._imgConf,
|
|
122
|
+
// ext,
|
|
123
|
+
// hashName,
|
|
124
|
+
// file,
|
|
125
|
+
// mime,
|
|
126
|
+
// content,
|
|
127
|
+
// fileType,
|
|
128
|
+
// this._storage,
|
|
129
|
+
// );
|
|
130
|
+
// break;
|
|
131
|
+
default:
|
|
132
|
+
response = null;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
return response;
|
|
136
|
+
}
|
|
137
|
+
hashName(name) {
|
|
138
|
+
return `${lodash_1.default.snakeCase(name)}__${(0, uuid_1.v4)()}`;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
exports.FileHandlerService = FileHandlerService;
|
|
142
|
+
exports.FileHandlerService = FileHandlerService = FileHandlerService_1 = tslib_1.__decorate([
|
|
143
|
+
(0, common_1.Injectable)(),
|
|
144
|
+
tslib_1.__param(0, (0, common_1.Inject)(file_handler_module_options_1.FILE_HANDLER_CONFIG_OPTIONS)),
|
|
145
|
+
tslib_1.__metadata("design:paramtypes", [Object, storage_1.StorageAwsS3Service])
|
|
146
|
+
], FileHandlerService);
|
|
147
|
+
//# sourceMappingURL=file-handler.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-handler.service.js","sourceRoot":"","sources":["../../../../../packages/file-handler/src/services/file-handler.service.ts"],"names":[],"mappings":";;;;;AAAA,2CAA4D;AAC5D,8CAA4D;AAC5D,0DAA0B;AAC1B,4DAAuB;AACvB,+BAA6B;AAC7B,+BAAoC;AACpC,0CAA0C;AAC1C,0CAAuC;AACvC,gFAGwC;AAMxC,yCAAuC;AACvC,0CAA6C;AAOtC,IAAM,kBAAkB,0BAAxB,MAAM,kBAAkB;IAG7B,YAEE,QAAmD,EAClC,QAA6B;QAD7B,aAAQ,GAAR,QAAQ,CAA0B;QAClC,aAAQ,GAAR,QAAQ,CAAqB;QALvC,WAAM,GAAW,IAAI,eAAM,CAAC,oBAAkB,CAAC,IAAI,CAAC,CAAC;QAO5D,2DAA2D;QAC3D,IAAI,CAAC,QAAQ,GAAG;YACd,0DAA0D;YAC1D,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO;YACpC,OAAO,EAAE,IAAI,CAAC,QAAQ,CA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CAAkC;YAClC,qBAAqB;YACrB,WAAW;YACX,gBAAgB;YAChB,YAAY;YACZ,YAAY;YACZ,eAAe;YACf,gBAAgB;YAChB,qBAAqB;YACrB,OAAO;YACP,WAAW;YACX,gBAAgB;YAChB,kCAAkC;YAClC,qBAAqB;YACrB,WAAW;YACX,gBAAgB;YAChB,YAAY;YACZ,YAAY;YACZ,eAAe;YACf,gBAAgB;YAChB,qBAAqB;YACrB,OAAO;YACP,WAAW;YACX;gBACE,QAAQ,GAAG,IAAI,CAAC;gBAChB,MAAM;QACV,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,QAAQ,CAAC,IAAY;QACnB,OAAO,GAAG,gBAAC,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,IAAA,SAAM,GAAE,EAAE,CAAC;IAC7C,CAAC;CACF,CAAA;AAlKY,gDAAkB;6BAAlB,kBAAkB;IAD9B,IAAA,mBAAU,GAAE;IAKR,mBAAA,IAAA,eAAM,EAAC,yDAA2B,CAAC,CAAA;qDAET,6BAAmB;GANrC,kBAAkB,CAkK9B"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FileHandlerService } from './file-handler.service';
|
|
2
|
+
import { IFile } from '../interfaces/file.interface';
|
|
3
|
+
export declare class FileHandlerTextService extends FileHandlerService {
|
|
4
|
+
private _extractPlain;
|
|
5
|
+
private _extractOdt;
|
|
6
|
+
private _extractPdf;
|
|
7
|
+
private _extractDocx;
|
|
8
|
+
private _extractPowerPoint;
|
|
9
|
+
extractText(file: IFile): Promise<string>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FileHandlerTextService = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const constant_1 = require("@tc-libs/constant");
|
|
6
|
+
const common_1 = require("@nestjs/common");
|
|
7
|
+
const officeparser_1 = tslib_1.__importDefault(require("officeparser"));
|
|
8
|
+
const file_handler_service_1 = require("./file-handler.service");
|
|
9
|
+
let FileHandlerTextService = class FileHandlerTextService extends file_handler_service_1.FileHandlerService {
|
|
10
|
+
async _extractPlain(file) {
|
|
11
|
+
return file.buffer.toString('utf-8');
|
|
12
|
+
}
|
|
13
|
+
async _extractOdt(file) {
|
|
14
|
+
const result = await officeparser_1.default.parseOfficeAsync(file.buffer, {
|
|
15
|
+
ignoreNotes: true,
|
|
16
|
+
});
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
async _extractPdf(file) {
|
|
20
|
+
const result = await officeparser_1.default.parseOfficeAsync(file.buffer, {
|
|
21
|
+
ignoreNotes: true,
|
|
22
|
+
});
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
async _extractDocx(file) {
|
|
26
|
+
const result = await officeparser_1.default.parseOfficeAsync(file.buffer, {
|
|
27
|
+
ignoreNotes: true,
|
|
28
|
+
});
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
async _extractPowerPoint(file) {
|
|
32
|
+
const result = await officeparser_1.default.parseOfficeAsync(file.buffer, {
|
|
33
|
+
ignoreNotes: true,
|
|
34
|
+
});
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
async extractText(file) {
|
|
38
|
+
const { mime } = super.getFileInfo(file);
|
|
39
|
+
// is odt
|
|
40
|
+
if (Object.values(constant_1.ENUM_FILE_ODT_MIME).includes(mime)) {
|
|
41
|
+
return this._extractOdt(file);
|
|
42
|
+
}
|
|
43
|
+
// is pdf
|
|
44
|
+
else if (Object.values(constant_1.ENUM_FILE_PDF_MIME).includes(mime)) {
|
|
45
|
+
return this._extractPdf(file);
|
|
46
|
+
}
|
|
47
|
+
// is docx
|
|
48
|
+
else if (Object.values(constant_1.ENUM_FILE_DOCX_MIME).includes(mime)) {
|
|
49
|
+
return this._extractDocx(file);
|
|
50
|
+
}
|
|
51
|
+
// is pp
|
|
52
|
+
else if (Object.values(constant_1.ENUM_FILE_PP_MIME).includes(mime)) {
|
|
53
|
+
return this._extractPowerPoint(file);
|
|
54
|
+
}
|
|
55
|
+
else
|
|
56
|
+
return this._extractPlain(file);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
exports.FileHandlerTextService = FileHandlerTextService;
|
|
60
|
+
exports.FileHandlerTextService = FileHandlerTextService = tslib_1.__decorate([
|
|
61
|
+
(0, common_1.Injectable)()
|
|
62
|
+
], FileHandlerTextService);
|
|
63
|
+
//# sourceMappingURL=file-handler.text.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-handler.text.service.js","sourceRoot":"","sources":["../../../../../packages/file-handler/src/services/file-handler.text.service.ts"],"names":[],"mappings":";;;;AAAA,gDAK2B;AAC3B,2CAA4C;AAC5C,wEAAwC;AACxC,iEAA4D;AAIrD,IAAM,sBAAsB,GAA5B,MAAM,sBAAuB,SAAQ,yCAAkB;IACpD,KAAK,CAAC,aAAa,CAAC,IAAW;QACrC,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;IAEO,KAAK,CAAC,WAAW,CAAC,IAAW;QACnC,MAAM,MAAM,GAAG,MAAM,sBAAY,CAAC,gBAAgB,CAAC,IAAI,CAAC,MAAM,EAAE;YAC9D,WAAW,EAAE,IAAI;SAClB,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC;IACO,KAAK,CAAC,WAAW,CAAC,IAAW;QACnC,MAAM,MAAM,GAAG,MAAM,sBAAY,CAAC,gBAAgB,CAAC,IAAI,CAAC,MAAM,EAAE;YAC9D,WAAW,EAAE,IAAI;SAClB,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC;IAEO,KAAK,CAAC,YAAY,CAAC,IAAW;QACpC,MAAM,MAAM,GAAG,MAAM,sBAAY,CAAC,gBAAgB,CAAC,IAAI,CAAC,MAAM,EAAE;YAC9D,WAAW,EAAE,I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1,13 @@
|
|
|
1
|
+
import { FileHandlerService } from './file-handler.service';
|
|
2
|
+
export interface IConvertToLowQualityAudioResult {
|
|
3
|
+
buffer: Buffer;
|
|
4
|
+
duration: number;
|
|
5
|
+
}
|
|
6
|
+
export declare class FileHandlerVideoService extends FileHandlerService {
|
|
7
|
+
getDuration(buffer: Buffer, filename: string): Promise<number>;
|
|
8
|
+
convertToLowQualityAudioFromBuffer(buffer: Buffer, filename: string): Promise<IConvertToLowQualityAudioResult>;
|
|
9
|
+
convertToLowQualityVideoFromBuffer(buffer: Buffer, filename: string): Promise<IConvertToLowQualityAudioResult>;
|
|
10
|
+
convertToLowQualityAudio(buffer: Buffer, hashName: string, ext: string): Promise<IConvertToLowQualityAudioResult>;
|
|
11
|
+
convertToLowQualityVideo(buffer: Buffer, hashName: string, ext: string): Promise<IConvertToLowQualityAudioResult>;
|
|
12
|
+
generateThumbnail(buffer: Buffer, videoFileName: string, thumbFileName: string, duration: number): Promise<Buffer>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FileHandlerVideoService = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const file_handler_service_1 = require("./file-handler.service");
|
|
7
|
+
const tmp_promise_1 = tslib_1.__importDefault(require("tmp-promise"));
|
|
8
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
9
|
+
const fluent_ffmpeg_1 = tslib_1.__importDefault(require("fluent-ffmpeg"));
|
|
10
|
+
const path_1 = require("path");
|
|
11
|
+
let FileHandlerVideoService = class FileHandlerVideoService extends file_handler_service_1.FileHandlerService {
|
|
12
|
+
async getDuration(buffer, filename) {
|
|
13
|
+
const tempDir = await tmp_promise_1.default.dir({
|
|
14
|
+
unsafeCleanup: true,
|
|
15
|
+
});
|
|
16
|
+
const inPath = (0, path_1.resolve)((0, path_1.join)(tempDir.path, filename));
|
|
17
|
+
(0, path_1.resolve)((0, path_1.join)(tempDir.path, filename));
|
|
18
|
+
await fs_1.default.promises.writeFile(inPath, buffer);
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
fluent_ffmpeg_1.default.ffprobe(inPath, (err, metadata) => {
|
|
21
|
+
if (err) {
|
|
22
|
+
tempDir.cleanup();
|
|
23
|
+
return reject(err);
|
|
24
|
+
}
|
|
25
|
+
tempDir.cleanup();
|
|
26
|
+
resolve(metadata.format.duration ?? -1);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
async convertToLowQualityAudioFromBuffer(buffer, filename) {
|
|
31
|
+
const tempDir = await tmp_promise_1.default.dir({
|
|
32
|
+
unsafeCleanup: true,
|
|
33
|
+
});
|
|
34
|
+
const outName = `${filename}.mp3`;
|
|
35
|
+
const inPath = (0, path_1.resolve)((0, path_1.join)(tempDir.path, filename));
|
|
36
|
+
const outPath = (0, path_1.resolve)((0, path_1.join)(tempDir.path, outName));
|
|
37
|
+
await fs_1.default.promises.writeFile(inPath, buffer);
|
|
38
|
+
return new Promise((resolve, reject) => {
|
|
39
|
+
(0, fluent_ffmpeg_1.default)()
|
|
40
|
+
.input(inPath)
|
|
41
|
+
.audioCodec('libmp3lame')
|
|
42
|
+
.audioChannels(1)
|
|
43
|
+
.outputOptions('-af', 'lowpass=3000,highpass=85')
|
|
44
|
+
.noVideo()
|
|
45
|
+
.save(outPath)
|
|
46
|
+
// .on('start', (commandLine) => {
|
|
47
|
+
// console.log('Spawned Ffmpeg with command: ' + commandLine);
|
|
48
|
+
// })
|
|
49
|
+
.on('error', (err) => {
|
|
50
|
+
tempDir.cleanup();
|
|
51
|
+
console.error('Cannot process: ' + err.message);
|
|
52
|
+
reject(err.message);
|
|
53
|
+
})
|
|
54
|
+
.on('end', () => {
|
|
55
|
+
const bufs = [];
|
|
56
|
+
const inStream = fs_1.default.createReadStream(outPath);
|
|
57
|
+
inStream.on('data', function (d) {
|
|
58
|
+
bufs.push(d);
|
|
59
|
+
});
|
|
60
|
+
inStream.on('end', function () {
|
|
61
|
+
fluent_ffmpeg_1.default.ffprobe(outPath, (err, metadata) => {
|
|
62
|
+
tempDir.cleanup();
|
|
63
|
+
resolve({
|
|
64
|
+
buffer: Buffer.concat(bufs),
|
|
65
|
+
duration: metadata.format.duration ?? -1,
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
async convertToLowQualityVideoFromBuffer(buffer, filename) {
|
|
73
|
+
const tempDir = await tmp_promise_1.default.dir({
|
|
74
|
+
unsafeCleanup: true,
|
|
75
|
+
});
|
|
76
|
+
const outName = `${filename}.mp4`;
|
|
77
|
+
const inPath = (0, path_1.resolve)((0, path_1.join)(tempDir.path, filename));
|
|
78
|
+
const outPath = (0, path_1.resolve)((0, path_1.join)(tempDir.path, outName));
|
|
79
|
+
await fs_1.default.promises.writeFile(inPath, buffer);
|
|
80
|
+
return new Promise((resolve) => {
|
|
81
|
+
(0, fluent_ffmpeg_1.default)()
|
|
82
|
+
.input(inPath)
|
|
83
|
+
.videoFilter('scale=720:-1')
|
|
84
|
+
.audioChannels(1)
|
|
85
|
+
.save(outPath)
|
|
86
|
+
.on('error', (err) => {
|
|
87
|
+
tempDir.cleanup();
|
|
88
|
+
console.error('Cannot process: ' + err.message);
|
|
89
|
+
})
|
|
90
|
+
.on('end', () => {
|
|
91
|
+
const bufs = [];
|
|
92
|
+
const inStream = fs_1.default.createReadStream(outPath);
|
|
93
|
+
inStream.on('data', function (d) {
|
|
94
|
+
bufs.push(d);
|
|
95
|
+
});
|
|
96
|
+
inStream.on('end', function () {
|
|
97
|
+
fluent_ffmpeg_1.default.ffprobe(outPath, (err, metadata) => {
|
|
98
|
+
tempDir.cleanup();
|
|
99
|
+
resolve({
|
|
100
|
+
buffer: Buffer.concat(bufs),
|
|
101
|
+
duration: metadata.format.duration ?? -1,
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
async convertToLowQualityAudio(buffer, hashName, ext) {
|
|
109
|
+
const filename = `${hashName}${ext}`;
|
|
110
|
+
return this.convertToLowQualityAudioFromBuffer(buffer, filename);
|
|
111
|
+
}
|
|
112
|
+
async convertToLowQualityVideo(buffer, hashName, ext) {
|
|
113
|
+
const filename = `${hashName}${ext}`;
|
|
114
|
+
return this.convertToLowQualityVideoFromBuffer(buffer, filename);
|
|
115
|
+
}
|
|
116
|
+
async generateThumbnail(buffer, videoFileName, thumbFileName, duration) {
|
|
117
|
+
const tempDir = await tmp_promise_1.default.dir({
|
|
118
|
+
unsafeCleanup: true,
|
|
119
|
+
});
|
|
120
|
+
const inPath = (0, path_1.resolve)((0, path_1.join)(tempDir.path, videoFileName));
|
|
121
|
+
await fs_1.default.promises.writeFile(inPath, buffer);
|
|
122
|
+
return new Promise((resolveP, rejectP) => {
|
|
123
|
+
const screenshotTime = duration / 2;
|
|
124
|
+
(0, fluent_ffmpeg_1.default)(inPath)
|
|
125
|
+
.on('error', (err) => {
|
|
126
|
+
tempDir.cleanup();
|
|
127
|
+
rejectP(err);
|
|
128
|
+
})
|
|
129
|
+
.screenshots({
|
|
130
|
+
timestamps: [screenshotTime],
|
|
131
|
+
filename: thumbFileName,
|
|
132
|
+
folder: tempDir.path,
|
|
133
|
+
count: 1,
|
|
134
|
+
})
|
|
135
|
+
.on('end', async () => {
|
|
136
|
+
const buffer = await super.fileToBuffer((0, path_1.resolve)((0, path_1.join)(tempDir.path, thumbFileName)));
|
|
137
|
+
tempDir.cleanup();
|
|
138
|
+
resolveP(buffer);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
exports.FileHandlerVideoService = FileHandlerVideoService;
|
|
144
|
+
exports.FileHandlerVideoService = FileHandlerVideoService = tslib_1.__decorate([
|
|
145
|
+
(0, common_1.Injectable)()
|
|
146
|
+
], FileHandlerVideoService);
|
|
147
|
+
//# sourceMappingURL=file-handler.video.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-handler.video.service.js","sourceRoot":"","sources":["../../../../../packages/file-handler/src/services/file-handler.video.service.ts"],"names":[],"mappings":";;;;AAAA,2CAA4C;AAC5C,iEAA4D;AAC5D,sEAA8B;AAC9B,oDAAoB;AACpB,0EAAmC;AACnC,+BAAqC;AAQ9B,IAAM,uBAAuB,GAA7B,MAAM,uBAAwB,SAAQ,yCAAkB;IAC7D,KAAK,CAAC,WAAW,CAAC,MAAc,EAAE,QAAgB;QAChD,MAAM,OAAO,GAAG,MAAM,qBAAG,CAAC,GAAG,CAAC;YAC5B,aAAa,EAAE,IAAI;SACpB,CAAC,CAAC;QACH,MAAM,MAAM,GAAG,IAAA,cAAO,EAAC,IAAA,WAAI,EAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC;QACrD,IAAA,cAAO,EAAC,IAAA,WAAI,EAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC;QAEtC,MAAM,YAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC5C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,uBAAM,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,QAAQ,EAAE,EAAE;gBACvC,IAAI,GAAG,EAAE,CAAC;oBACR,OAAO,CAAC,OAAO,EAAE,CAAC;oBAClB,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;gBACrB,CAAC;gBACD,OAAO,CAAC,OAAO,EAAE,CAAC;gBAClB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,QAAQ,IAAI,CAAC,CAAC,CAAC,CAAC;YAC1C,CAAC,CAAC,CAAC;Q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AC,CAAC;QACP,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AAtJY,0DAAuB;kCAAvB,uBAAuB;IADnC,IAAA,mBAAU,GAAE;GACA,uBAAuB,CAsJnC"}
|
|
File without changes
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// import path = require('path');
|
|
3
|
+
// import shortid from 'shortid';
|
|
4
|
+
// import FFmpeg from 'fluent-ffmpeg';
|
|
5
|
+
// import { existsSync, unlinkSync } from 'fs';
|
|
6
|
+
// import {
|
|
7
|
+
// FileHandlerConf,
|
|
8
|
+
// HandleFileResponse,
|
|
9
|
+
// } from '../../interfaces/file-handler.interface';
|
|
10
|
+
// import { StorageAwsS3Service } from '@tc-libs/storage';
|
|
11
|
+
// import { IFile } from '../interfaces/file.interface';
|
|
12
|
+
// import { Readable } from 'stream';
|
|
13
|
+
// const getVideoInfo = (content: Buffer) => {
|
|
14
|
+
// return new Promise((resolve, reject) => {
|
|
15
|
+
// FFmpeg.ffprobe(videoPath, (err: any, metadata: any) => {
|
|
16
|
+
// if (err) return reject(err);
|
|
17
|
+
// return resolve(metadata);
|
|
18
|
+
// });
|
|
19
|
+
// });
|
|
20
|
+
// };
|
|
21
|
+
// const resizeAndUploadVideo = (
|
|
22
|
+
// imgConf: FileHandlerConf,
|
|
23
|
+
// resize: { name: string | null; size: number | null; quality: number },
|
|
24
|
+
// remoteFolder: string,
|
|
25
|
+
// remoteKey: string,
|
|
26
|
+
// mime: string,
|
|
27
|
+
// content: Buffer,
|
|
28
|
+
// storage: StorageAwsS3Service,
|
|
29
|
+
// ): Promise<{ width: number; height: number; size: number }> => {
|
|
30
|
+
// return new Promise(async (resolve, reject) => {
|
|
31
|
+
// const tmpName = `video_${shortid.generate()}.mp4`;
|
|
32
|
+
// const output = path.resolve(path.join(imgConf.workDirectory, tmpName));
|
|
33
|
+
// // get file info
|
|
34
|
+
// const metadata: any = await getVideoInfo(content);
|
|
35
|
+
// // get stream data
|
|
36
|
+
// let stream;
|
|
37
|
+
// if (metadata && metadata.streams && metadata.streams.length)
|
|
38
|
+
// stream = metadata.streams[0];
|
|
39
|
+
// if (!stream) reject('Missing stream video');
|
|
40
|
+
// // extract w,h,ratio
|
|
41
|
+
// const { display_aspect_ratio: ratio, height: h, width: w } = stream;
|
|
42
|
+
// // if resize is bigger than original, skip it
|
|
43
|
+
// // if (resize.size && Math.max(w, h) < resize.size)
|
|
44
|
+
// // return resolve({ width: w, height: h, size: metadata.format.size });
|
|
45
|
+
// const readable: Readable = Readable.from(content);
|
|
46
|
+
// const video = FFmpeg(
|
|
47
|
+
// { source: readable as string | Readable },
|
|
48
|
+
// { objectMode: false },
|
|
49
|
+
// );
|
|
50
|
+
// // .writeToStream(bufferStream);
|
|
51
|
+
// let computedW = w;
|
|
52
|
+
// let computedH = h;
|
|
53
|
+
// // has to resize
|
|
54
|
+
// if (resize.size) {
|
|
55
|
+
// const [wRatio, hRatio] = ratio.split(':');
|
|
56
|
+
// const biggerDimension = w > h ? 'width' : 'height';
|
|
57
|
+
// if (biggerDimension === 'width') {
|
|
58
|
+
// const r = wRatio / hRatio;
|
|
59
|
+
// computedW = resize.size;
|
|
60
|
+
// computedH = Math.round(resize.size / r);
|
|
61
|
+
// } else {
|
|
62
|
+
// const r = hRatio / wRatio;
|
|
63
|
+
// computedH = resize.size;
|
|
64
|
+
// computedW = Math.round(resize.size / r);
|
|
65
|
+
// }
|
|
66
|
+
// video.size(`${computedW}x${computedH}`);
|
|
67
|
+
// }
|
|
68
|
+
// video
|
|
69
|
+
// .on('end', async () => {
|
|
70
|
+
// const {
|
|
71
|
+
// format: { size: computedSize },
|
|
72
|
+
// }: any = await getVideoInfo(output);
|
|
73
|
+
// const result = await storage.uploadItem(remoteKey, output, {
|
|
74
|
+
// mime,
|
|
75
|
+
// path: remoteFolder,
|
|
76
|
+
// });
|
|
77
|
+
// // delete file
|
|
78
|
+
// if (imgConf.hasToClean && existsSync(output)) unlinkSync(output);
|
|
79
|
+
// resolve({
|
|
80
|
+
// height: computedH,
|
|
81
|
+
// width: computedW,
|
|
82
|
+
// size: computedSize,
|
|
83
|
+
// });
|
|
84
|
+
// })
|
|
85
|
+
// .save(output);
|
|
86
|
+
// // const buffers = [];
|
|
87
|
+
// // bufferStream.on('data', (buf) => {
|
|
88
|
+
// // buffers.push(buf);
|
|
89
|
+
// // });
|
|
90
|
+
// // bufferStream.on('end', async () => {
|
|
91
|
+
// // const outputBuffer = Buffer.concat(buffers);
|
|
92
|
+
// // // use outputBuffer
|
|
93
|
+
// // // resize img to a buffer
|
|
94
|
+
// // // upload buffer to blob storage
|
|
95
|
+
// // if (outputBuffer.length) {
|
|
96
|
+
// // console.log(12);
|
|
97
|
+
// // await this._storage.uploadFileFromBuffer(
|
|
98
|
+
// // user,
|
|
99
|
+
// // outputBuffer,
|
|
100
|
+
// // ACL.PUBLIC_READ,
|
|
101
|
+
// // mime,
|
|
102
|
+
// // remoteFilePath,
|
|
103
|
+
// // );
|
|
104
|
+
// // }
|
|
105
|
+
// // });
|
|
106
|
+
// });
|
|
107
|
+
// };
|
|
108
|
+
// export const handleVideo = async (
|
|
109
|
+
// imgConf: FileHandlerConf,
|
|
110
|
+
// ext: string,
|
|
111
|
+
// hashName: string,
|
|
112
|
+
// file: IFile,
|
|
113
|
+
// mime: any,
|
|
114
|
+
// content: Buffer,
|
|
115
|
+
// fileType: string,
|
|
116
|
+
// storage: StorageAwsS3Service,
|
|
117
|
+
// ): Promise<HandleFileResponse> => {
|
|
118
|
+
// // get all resizes
|
|
119
|
+
// const resizes = imgConf.resizes;
|
|
120
|
+
// const originalRemoteFullPath = `${fileType}/${hashName}${ext}`;
|
|
121
|
+
// // original upload to s3
|
|
122
|
+
// const originalInfo = await resizeAndUploadVideo(
|
|
123
|
+
// imgConf,
|
|
124
|
+
// {
|
|
125
|
+
// size: null,
|
|
126
|
+
// name: null,
|
|
127
|
+
// quality: imgConf.quality,
|
|
128
|
+
// },
|
|
129
|
+
// fileType,
|
|
130
|
+
// `${hashName}${ext}`,
|
|
131
|
+
// mime,
|
|
132
|
+
// content,
|
|
133
|
+
// storage,
|
|
134
|
+
// );
|
|
135
|
+
// // return metadata
|
|
136
|
+
// const response: HandleFileResponse = {
|
|
137
|
+
// path: file.path,
|
|
138
|
+
// filename: file.filename,
|
|
139
|
+
// hash: hashName,
|
|
140
|
+
// ext,
|
|
141
|
+
// mime,
|
|
142
|
+
// size: file.size,
|
|
143
|
+
// url: originalRemoteFullPath,
|
|
144
|
+
// provider: 'aws-s3',
|
|
145
|
+
// width: originalInfo.width,
|
|
146
|
+
// height: originalInfo.height,
|
|
147
|
+
// formats: [],
|
|
148
|
+
// };
|
|
149
|
+
// for (const resize of resizes) {
|
|
150
|
+
// // build resized img path
|
|
151
|
+
// const remoteFullPath = `${fileType}/${resize.name}/${hashName}${ext}`;
|
|
152
|
+
// const resizedInfo = await resizeAndUploadVideo(
|
|
153
|
+
// imgConf,
|
|
154
|
+
// resize,
|
|
155
|
+
// `${fileType}/${resize.name}`,
|
|
156
|
+
// `${hashName}${ext}`,
|
|
157
|
+
// mime,
|
|
158
|
+
// content,
|
|
159
|
+
// storage,
|
|
160
|
+
// );
|
|
161
|
+
// if (!resizedInfo) continue;
|
|
162
|
+
// response.formats.push({
|
|
163
|
+
// name: resize.name,
|
|
164
|
+
// meta: {
|
|
165
|
+
// width: resizedInfo.width,
|
|
166
|
+
// height: resizedInfo.height,
|
|
167
|
+
// size: resizedInfo.size,
|
|
168
|
+
// url: remoteFullPath,
|
|
169
|
+
// },
|
|
170
|
+
// });
|
|
171
|
+
// }
|
|
172
|
+
// return response;
|
|
173
|
+
// };
|
|
174
|
+
//# sourceMappingURL=audio.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio.js","sourceRoot":"","sources":["../../../../../../packages/file-handler/src/services/handlers/audio.ts"],"names":[],"mappings":";AAAA,iCAAiC;AACjC,iCAAiC;AACjC,sCAAsC;AACtC,+CAA+C;AAC/C,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D,8BAA8B;AAC9B,oCAAoC;AACpC,2CAA2C;AAC3C,qCAAqC;AACrC,4BAA4B;AAC5B,qDAAqD;AACrD,kBAAkB;AAClB,0BAA0B;AAC1B,6BAA6B;AAC7B,kBAAkB;AAClB,4BAA4B;AAC5B,cAAc;AACd,YAAY;AACZ,aAAa;AACb,QAAQ;AACR,KAAK;AAEL,qCAAqC;AACrC,8BAA8B;AAC9B,iBAAiB;AACjB,sBAAsB;AACtB,iBAAiB;AACjB,eAAe;AACf,qBAAqB;AACrB,sBAAsB;AACtB,kCAAkC;AAClC,sCAAsC;AACtC,uBAAuB;AACvB,qCAAqC;AAErC,oEAAoE;AACpE,6BAA6B;AAC7B,qDAAqD;AACrD,eAAe;AACf,QAAQ;AACR,oBAAoB;AACpB,oBAAoB;AACpB,kCAAkC;AAClC,SAAS;AACT,gBAAgB;AAChB,2BAA2B;AAC3B,YAAY;AACZ,eAAe;AACf,eAAe;AACf,OAAO;AAEP,uBAAuB;AACvB,2CAA2C;AAC3C,uBAAuB;AACvB,+BAA+B;AAC/B,sBAAsB;AACtB,WAAW;AACX,YAAY;AACZ,uBAAuB;AACvB,mCAAmC;AACnC,0BAA0B;AAC1B,iCAAiC;AACjC,mCAAmC;AACnC,mBAAmB;AACnB,OAAO;AAEP,oCAAoC;AACpC,gCAAgC;AAChC,6EAA6E;AAE7E,sDAAsD;AACtD,iBAAiB;AACjB,gBAAgB;AAChB,sCAAsC;AACtC,6BAA6B;AAC7B,cAAc;AACd,iBAAiB;AACjB,iBAAiB;AACjB,SAAS;AACT,kCAAkC;AAElC,8BAA8B;AAC9B,2BAA2B;AAC3B,gBAAgB;AAChB,oCAAoC;AACpC,sCAAsC;AACtC,kCAAkC;AAClC,+BAA+B;AAC/B,WAAW;AACX,UAAU;AACV,MAAM;AACN,qBAAqB;AACrB,KAAK"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { StorageAwsS3Service } from '@tc-libs/storage';
|
|
2
|
+
import { HandleFileResponse } from '../../interfaces/file-handler.interface';
|
|
3
|
+
import { HandleFileOptions } from '../file-handler.service';
|
|
4
|
+
import { IFile } from '../../interfaces/file.interface';
|
|
5
|
+
export declare const handleFile: (ext: string, hashName: string, file: IFile, mime: any, content: Buffer, fileType: string, storage: StorageAwsS3Service, options: HandleFileOptions) => Promise<HandleFileResponse>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handleFile = void 0;
|
|
4
|
+
const handleFile = async (ext, hashName, file, mime, content, fileType, storage, options) => {
|
|
5
|
+
// get all resizes
|
|
6
|
+
const key = `${hashName}${ext}`;
|
|
7
|
+
const storageResponse = await storage.uploadItem(key, content, {
|
|
8
|
+
path: fileType,
|
|
9
|
+
mime: mime,
|
|
10
|
+
acl: options.acl,
|
|
11
|
+
});
|
|
12
|
+
console.log(storageResponse);
|
|
13
|
+
// return metadata
|
|
14
|
+
const response = {
|
|
15
|
+
filename: storageResponse.filename,
|
|
16
|
+
path: storageResponse.pathWithFilename,
|
|
17
|
+
folder: storageResponse.path,
|
|
18
|
+
hash: hashName,
|
|
19
|
+
ext,
|
|
20
|
+
mime,
|
|
21
|
+
size: file.size,
|
|
22
|
+
baseUrl: storageResponse.baseUrl,
|
|
23
|
+
completeUrl: storageResponse.completeUrl,
|
|
24
|
+
provider: storageResponse.provider,
|
|
25
|
+
width: null,
|
|
26
|
+
height: null,
|
|
27
|
+
formats: [],
|
|
28
|
+
};
|
|
29
|
+
return response;
|
|
30
|
+
};
|
|
31
|
+
exports.handleFile = handleFile;
|
|
32
|
+
//# sourceMappingURL=file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file.js","sourceRoot":"","sources":["../../../../../../packages/file-handler/src/services/handlers/file.ts"],"names":[],"mappings":";;;AAKO,MAAM,UAAU,GAAG,KAAK,EAC7B,GAAW,EACX,QAAgB,EAChB,IAAW,EACX,IAAS,EACT,OAAe,EACf,QAAgB,EAChB,OAA4B,EAC5B,OAA0B,EACG,EAAE;IAC/B,kBAAkB;IAClB,MAAM,GAAG,GAAG,GAAG,QAAQ,GAAG,GAAG,EAAE,CAAC;IAChC,MAAM,eAAe,GAAG,MAAM,OAAO,CAAC,UAAU,CAAC,GAAG,EAAE,OAAO,EAAE;QAC7D,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,IAAI;QACV,GAAG,EAAE,OAAO,CAAC,GAAG;KACjB,CAAC,CAAC;IAEH,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IAE7B,kBAAkB;IAClB,MAAM,QAAQ,GAAuB;QACnC,QAAQ,EAAE,eAAe,CAAC,QAAQ;QAClC,IAAI,EAAE,eAAe,CAAC,gBAAgB;QACtC,MAAM,EAAE,eAAe,CAAC,IAAI;QAC5B,IAAI,EAAE,QAAQ;QACd,GAAG;QACH,IAAI;QACJ,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,OAAO,EAAE,eAAe,CAAC,OAAO;QAChC,WAAW,EAAE,eAAe,CAAC,WAAW;QACxC,QAAQ,EAAE,eAAe,CAAC,QAAQ;QAClC,KAAK,EAAE,IAAI;QACX,MAAM,EAAE,IAAI;QACZ,OAAO,EAAE,EAAE;KACZ,CAAC;IAEF,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAtCW,QAAA,UAAU,cAsCrB"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./file"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./jpg"), exports);
|
|
6
|
+
// export * from './video';
|
|
7
|
+
// export * from './png';
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/file-handler/src/services/handlers/index.ts"],"names":[],"mappings":";;;AAAA,iDAAuB;AACvB,gDAAsB;AACtB,2BAA2B;AAC3B,yBAAyB"}
|