@ackplus/nest-file-storage 0.1.35
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 +409 -0
- package/package.json +52 -0
- package/src/index.d.ts +4 -0
- package/src/index.js +8 -0
- package/src/index.js.map +1 -0
- package/src/lib/constants.d.ts +1 -0
- package/src/lib/constants.js +5 -0
- package/src/lib/constants.js.map +1 -0
- package/src/lib/file-storage.service.d.ts +7 -0
- package/src/lib/file-storage.service.js +31 -0
- package/src/lib/file-storage.service.js.map +1 -0
- package/src/lib/index.d.ts +5 -0
- package/src/lib/index.js +9 -0
- package/src/lib/index.js.map +1 -0
- package/src/lib/interceptor/file-storage.interceptor.d.ts +21 -0
- package/src/lib/interceptor/file-storage.interceptor.js +122 -0
- package/src/lib/interceptor/file-storage.interceptor.js.map +1 -0
- package/src/lib/nest-file-storage.module.d.ts +8 -0
- package/src/lib/nest-file-storage.module.js +75 -0
- package/src/lib/nest-file-storage.module.js.map +1 -0
- package/src/lib/storage/azure.storage.d.ts +18 -0
- package/src/lib/storage/azure.storage.js +153 -0
- package/src/lib/storage/azure.storage.js.map +1 -0
- package/src/lib/storage/local.storage.d.ts +17 -0
- package/src/lib/storage/local.storage.js +133 -0
- package/src/lib/storage/local.storage.js.map +1 -0
- package/src/lib/storage/s3.storage.d.ts +19 -0
- package/src/lib/storage/s3.storage.js +208 -0
- package/src/lib/storage/s3.storage.js.map +1 -0
- package/src/lib/storage.factory.d.ts +8 -0
- package/src/lib/storage.factory.js +52 -0
- package/src/lib/storage.factory.js.map +1 -0
- package/src/lib/types.d.ts +76 -0
- package/src/lib/types.js +10 -0
- package/src/lib/types.js.map +1 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FileStorageInterceptor = FileStorageInterceptor;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const multer_1 = tslib_1.__importDefault(require("multer"));
|
|
6
|
+
const file_storage_service_1 = require("../file-storage.service");
|
|
7
|
+
const storage_factory_1 = require("../storage.factory");
|
|
8
|
+
const types_1 = require("../types");
|
|
9
|
+
function mapFileObject(file) {
|
|
10
|
+
return {
|
|
11
|
+
fieldName: file.fieldname,
|
|
12
|
+
originalName: file.originalname,
|
|
13
|
+
fileName: file.filename,
|
|
14
|
+
mimetype: file.mimetype,
|
|
15
|
+
size: file.size,
|
|
16
|
+
key: file.key,
|
|
17
|
+
path: file.path,
|
|
18
|
+
url: file.url,
|
|
19
|
+
encoding: file.encoding,
|
|
20
|
+
fullPath: file.fullPath
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function applyFileKeyMapping(request, fileConfig, interceptorOptions) {
|
|
24
|
+
const mapCallback = (interceptorOptions === null || interceptorOptions === void 0 ? void 0 : interceptorOptions.mapToRequestBody) || ((file) => {
|
|
25
|
+
if (Array.isArray(file)) {
|
|
26
|
+
return file.map(f => f.key);
|
|
27
|
+
}
|
|
28
|
+
return file.key;
|
|
29
|
+
});
|
|
30
|
+
if (fileConfig.type === 'single') {
|
|
31
|
+
const file = request.file;
|
|
32
|
+
if (file) {
|
|
33
|
+
const fieldName = fileConfig.fieldName || 'file';
|
|
34
|
+
const mappedFile = mapFileObject(file);
|
|
35
|
+
request.body[fieldName] = mapCallback(mappedFile, fieldName, request);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else if (fileConfig.type === 'array') {
|
|
39
|
+
const files = request.files;
|
|
40
|
+
if (files && files.length > 0) {
|
|
41
|
+
const fieldName = fileConfig.fieldName || 'files';
|
|
42
|
+
const mappedFiles = files.map(file => mapFileObject(file));
|
|
43
|
+
request.body[fieldName] = mapCallback(mappedFiles, fieldName, request);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else if (fileConfig.type === 'fields') {
|
|
47
|
+
const files = request.files;
|
|
48
|
+
if (files) {
|
|
49
|
+
Object.keys(files).forEach(fieldName => {
|
|
50
|
+
const mappedFiles = files[fieldName].map(file => mapFileObject(file));
|
|
51
|
+
request.body[fieldName] = mapCallback(mappedFiles, fieldName, request);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function FileStorageInterceptor(fileConfig, interceptorOptions) {
|
|
57
|
+
if (typeof fileConfig === 'string') {
|
|
58
|
+
fileConfig = {
|
|
59
|
+
type: 'single',
|
|
60
|
+
fieldName: fileConfig,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
intercept(context, next) {
|
|
65
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
var _a, _b;
|
|
67
|
+
const options = file_storage_service_1.FileStorageService.getOptions();
|
|
68
|
+
const request = context.switchToHttp().getRequest();
|
|
69
|
+
const response = context.switchToHttp().getResponse();
|
|
70
|
+
let storageType;
|
|
71
|
+
let storageConfig;
|
|
72
|
+
if ('storage' in options) {
|
|
73
|
+
const configOptions = options;
|
|
74
|
+
storageType = (_a = interceptorOptions === null || interceptorOptions === void 0 ? void 0 : interceptorOptions.storageType) !== null && _a !== void 0 ? _a : configOptions.storage;
|
|
75
|
+
storageConfig = configOptions[`${storageType}Config`];
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
storageType = (_b = interceptorOptions === null || interceptorOptions === void 0 ? void 0 : interceptorOptions.storageType) !== null && _b !== void 0 ? _b : types_1.FileStorageEnum.LOCAL;
|
|
79
|
+
storageConfig = {};
|
|
80
|
+
}
|
|
81
|
+
const storageOptions = Object.assign(Object.assign(Object.assign({}, storageConfig), ((interceptorOptions === null || interceptorOptions === void 0 ? void 0 : interceptorOptions.storageOptions) || {})), { fileName: (interceptorOptions === null || interceptorOptions === void 0 ? void 0 : interceptorOptions.fileName) || (storageConfig === null || storageConfig === void 0 ? void 0 : storageConfig.fileName), fileDist: (file, req) => {
|
|
82
|
+
var _a;
|
|
83
|
+
if (interceptorOptions === null || interceptorOptions === void 0 ? void 0 : interceptorOptions.fileDist) {
|
|
84
|
+
return interceptorOptions.fileDist(file, req);
|
|
85
|
+
}
|
|
86
|
+
return (_a = storageConfig === null || storageConfig === void 0 ? void 0 : storageConfig.fileDist) === null || _a === void 0 ? void 0 : _a.call(storageConfig, file, req);
|
|
87
|
+
}, prefix: (interceptorOptions === null || interceptorOptions === void 0 ? void 0 : interceptorOptions.prefix) || (storageConfig === null || storageConfig === void 0 ? void 0 : storageConfig.prefix) });
|
|
88
|
+
const storage = yield storage_factory_1.StorageFactory.createStorage(storageType, storageOptions);
|
|
89
|
+
const multerInstance = (0, multer_1.default)({ storage });
|
|
90
|
+
let multerMiddleware;
|
|
91
|
+
switch (fileConfig.type) {
|
|
92
|
+
case 'single':
|
|
93
|
+
if (!fileConfig.fieldName) {
|
|
94
|
+
throw new Error('fieldName is required for single file upload.');
|
|
95
|
+
}
|
|
96
|
+
multerMiddleware = multerInstance.single(fileConfig.fieldName);
|
|
97
|
+
break;
|
|
98
|
+
case 'array':
|
|
99
|
+
if (!fileConfig.fieldName) {
|
|
100
|
+
throw new Error('fieldName is required for multiple file upload.');
|
|
101
|
+
}
|
|
102
|
+
multerMiddleware = multerInstance.array(fileConfig.fieldName, fileConfig.maxCount);
|
|
103
|
+
break;
|
|
104
|
+
case 'fields':
|
|
105
|
+
if (!fileConfig.fields || !Array.isArray(fileConfig.fields)) {
|
|
106
|
+
throw new Error('fields array is required for multiple fields file upload.');
|
|
107
|
+
}
|
|
108
|
+
multerMiddleware = multerInstance.fields(fileConfig.fields);
|
|
109
|
+
break;
|
|
110
|
+
default:
|
|
111
|
+
throw new Error('Invalid file upload type. Use "single", "array", or "fields".');
|
|
112
|
+
}
|
|
113
|
+
yield new Promise((resolve, reject) => {
|
|
114
|
+
multerMiddleware(request, response, (err) => (err ? reject(err) : resolve(true)));
|
|
115
|
+
});
|
|
116
|
+
applyFileKeyMapping(request, fileConfig, interceptorOptions);
|
|
117
|
+
return next.handle();
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=file-storage.interceptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-storage.interceptor.js","sourceRoot":"","sources":["../../../../../../packages/nest-file-storage/src/lib/interceptor/file-storage.interceptor.ts"],"names":[],"mappings":";;AAwFA,wDAqFC;;AA3KD,4DAA4B;AAG5B,kEAA6D;AAC7D,wDAAoD;AACpD,oCAAmG;AAsBnG,SAAS,aAAa,CAAC,IAAI;IACvB,OAAO;QACH,SAAS,EAAE,IAAI,CAAC,SAAS;QACzB,YAAY,EAAE,IAAI,CAAC,YAAY;QAC/B,QAAQ,EAAE,IAAI,CAAC,QAAQ;QACvB,QAAQ,EAAE,IAAI,CAAC,QAAQ;QACvB,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,GAAG,EAAG,IAAY,CAAC,GAAG;QACtB,IAAI,EAAG,IAAY,CAAC,IAAI;QACxB,GAAG,EAAG,IAAY,CAAC,GAAG;QACtB,QAAQ,EAAE,IAAI,CAAC,QAAQ;QACvB,QAAQ,EAAG,IAAY,CAAC,QAAQ;KACR,CAAC;AACjC,CAAC;AAGD,SAAS,mBAAmB,CACxB,OAAgB,EAChB,UAA4B,EAC5B,kBAAkD;IAGlD,MAAM,WAAW,GAAG,CAAA,kBAAkB,aAAlB,kBAAkB,uBAAlB,kBAAkB,CAAE,gBAAgB,KAAI,CAAC,CAAC,IAAS,EAAE,EAAE;QAEvE,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAChC,CAAC;QAED,OAAO,IAAI,CAAC,GAAG,CAAC;IACpB,CAAC,CAAC,CAAC;IAEH,IAAI,UAAU,CAAC,IAAI,KAAK,QAAQ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BACT,IAAI,CAAC,UAAU,CAAC,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;4BAC1D,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAC;wBACjF,CAAC;wBACD,gBAAgB,GAAG,cAAc,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;wBAC5D,MAAM;oBACV;wBACI,MAAM,IAAI,KAAK,CAAC,+DAA+D,CAAC,CAAC;gBACzF,CAAC;gBAGD,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;oBAClC,gBAAgB,CAAC,OAAO,EAAE,QAAQ,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;gBACtF,CAAC,CAAC,CAAC;gBAGH,mBAAmB,CAAC,OAAO,EAAE,UAAU,EAAE,kBAAkB,CAAC,CAAC;gBAE7D,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;YACzB,CAAC;SAAA;KACJ,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { FileStorageAsyncOptions, FileStorageModuleOptions } from './types';
|
|
3
|
+
export declare class NestFileStorageModule {
|
|
4
|
+
static forRoot(options: FileStorageModuleOptions): DynamicModule;
|
|
5
|
+
static forRootAsync(options: FileStorageAsyncOptions): DynamicModule;
|
|
6
|
+
private static createAsyncProviders;
|
|
7
|
+
private static createAsyncOptionsProvider;
|
|
8
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var NestFileStorageModule_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.NestFileStorageModule = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const common_1 = require("@nestjs/common");
|
|
7
|
+
const constants_1 = require("./constants");
|
|
8
|
+
const file_storage_service_1 = require("./file-storage.service");
|
|
9
|
+
let NestFileStorageModule = NestFileStorageModule_1 = class NestFileStorageModule {
|
|
10
|
+
static forRoot(options) {
|
|
11
|
+
return {
|
|
12
|
+
module: NestFileStorageModule_1,
|
|
13
|
+
providers: [
|
|
14
|
+
{
|
|
15
|
+
provide: constants_1.FILE_STORAGE_OPTIONS,
|
|
16
|
+
useFactory: () => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
file_storage_service_1.FileStorageService.setOptions(options);
|
|
18
|
+
return options;
|
|
19
|
+
}),
|
|
20
|
+
inject: [],
|
|
21
|
+
},
|
|
22
|
+
file_storage_service_1.FileStorageService,
|
|
23
|
+
],
|
|
24
|
+
exports: [],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
static forRootAsync(options) {
|
|
28
|
+
const asyncProviders = this.createAsyncProviders(options);
|
|
29
|
+
return {
|
|
30
|
+
module: NestFileStorageModule_1,
|
|
31
|
+
imports: options.imports || [],
|
|
32
|
+
providers: [...asyncProviders, file_storage_service_1.FileStorageService],
|
|
33
|
+
exports: [],
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
static createAsyncProviders(options) {
|
|
37
|
+
if (options.useExisting || options.useFactory) {
|
|
38
|
+
return [this.createAsyncOptionsProvider(options)];
|
|
39
|
+
}
|
|
40
|
+
return [
|
|
41
|
+
this.createAsyncOptionsProvider(options),
|
|
42
|
+
{
|
|
43
|
+
provide: options.useClass,
|
|
44
|
+
useClass: options.useClass,
|
|
45
|
+
},
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
static createAsyncOptionsProvider(options) {
|
|
49
|
+
if (options.useFactory) {
|
|
50
|
+
return {
|
|
51
|
+
provide: constants_1.FILE_STORAGE_OPTIONS,
|
|
52
|
+
useFactory: (...args) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
const fileStorageOptions = yield options.useFactory(...args);
|
|
54
|
+
file_storage_service_1.FileStorageService.setOptions(fileStorageOptions);
|
|
55
|
+
return fileStorageOptions;
|
|
56
|
+
}),
|
|
57
|
+
inject: options.inject || [],
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
provide: constants_1.FILE_STORAGE_OPTIONS,
|
|
62
|
+
useFactory: (optionsFactory) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
const fileStorageOptions = yield optionsFactory.createFileStorageOptions();
|
|
64
|
+
file_storage_service_1.FileStorageService.setOptions(fileStorageOptions);
|
|
65
|
+
return fileStorageOptions;
|
|
66
|
+
}),
|
|
67
|
+
inject: [options.useExisting || options.useClass],
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
exports.NestFileStorageModule = NestFileStorageModule;
|
|
72
|
+
exports.NestFileStorageModule = NestFileStorageModule = NestFileStorageModule_1 = tslib_1.__decorate([
|
|
73
|
+
(0, common_1.Module)({})
|
|
74
|
+
], NestFileStorageModule);
|
|
75
|
+
//# sourceMappingURL=nest-file-storage.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nest-file-storage.module.js","sourceRoot":"","sources":["../../../../../packages/nest-file-storage/src/lib/nest-file-storage.module.ts"],"names":[],"mappings":";;;;;AAAA,2CAAiE;AAEjE,2CAAmD;AACnD,iEAA4D;AAKrD,IAAM,qBAAqB,6BAA3B,MAAM,qBAAqB;IAE9B,MAAM,CAAC,OAAO,CAAC,OAAiC;QAC5C,OAAO;YACH,MAAM,EAAE,uBAAqB;YAC7B,SAAS,EAAE;gBACP;oBACI,OAAO,EAAE,gCAAoB;oBAC7B,UAAU,EAAE,GAAS,EAAE;wBACnB,yCAAkB,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;wBACvC,OAAO,OAAO,CAAC;oBACnB,CAAC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AE,CAAO,cAAyC,EAAE,EAAE;gBAC5D,MAAM,kBAAkB,GAAG,MAAM,cAAc,CAAC,wBAAwB,EAAE,CAAC;gBAC3E,yCAAkB,CAAC,UAAU,CAAC,kBAAkB,CAAC,CAAC;gBAClD,OAAO,kBAAkB,CAAC;YAC9B,CAAC,CAAA;YACD,MAAM,EAAE,CAAC,OAAO,CAAC,WAAW,IAAI,OAAO,CAAC,QAAS,CAAC;SACrD,CAAC;IACN,CAAC;CAEJ,CAAA;AArEY,sDAAqB;gCAArB,qBAAqB;IADjC,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,qBAAqB,CAqEjC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BlobSASSignatureValues } from '@azure/storage-blob';
|
|
2
|
+
import { StorageEngine } from 'multer';
|
|
3
|
+
import { AzureStorageOptions, Storage, UploadedFile } from '../types';
|
|
4
|
+
export declare class AzureStorage implements StorageEngine, Storage {
|
|
5
|
+
private options;
|
|
6
|
+
private blobServiceClient;
|
|
7
|
+
private fileNameFunction;
|
|
8
|
+
private fileDistFunction;
|
|
9
|
+
constructor(options: AzureStorageOptions);
|
|
10
|
+
_handleFile(req: any, file: any, cb: (error?: any, info?: any) => void): Promise<void>;
|
|
11
|
+
_removeFile(_req: any, file: any, cb: (error: Error | null) => void): Promise<void>;
|
|
12
|
+
getUrl(key: string): string;
|
|
13
|
+
getSignedUrl(key: string, signatureValues?: Partial<Omit<BlobSASSignatureValues, 'containerName'>>): string;
|
|
14
|
+
getFile(key: string): Promise<Buffer>;
|
|
15
|
+
putFile(buffer: Buffer, key: string): Promise<UploadedFile>;
|
|
16
|
+
deleteFile(key: string): Promise<void>;
|
|
17
|
+
copyFile(oldKey: string, newKey: string): Promise<UploadedFile>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AzureStorage = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const storage_blob_1 = require("@azure/storage-blob");
|
|
6
|
+
const concat_stream_1 = tslib_1.__importDefault(require("concat-stream"));
|
|
7
|
+
const moment_1 = tslib_1.__importDefault(require("moment"));
|
|
8
|
+
const path_1 = tslib_1.__importStar(require("path"));
|
|
9
|
+
const uuid_1 = require("uuid");
|
|
10
|
+
class AzureStorage {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
this.fileNameFunction = options.fileName || ((file, _req) => {
|
|
14
|
+
return `${(0, uuid_1.v4)()}-${file.originalname}`;
|
|
15
|
+
});
|
|
16
|
+
this.fileDistFunction = options.fileDist || ((_file, _req) => {
|
|
17
|
+
return path_1.default.join('uploads', (0, moment_1.default)().format('YYYY'), (0, moment_1.default)().format('MM'), (0, moment_1.default)().format('DD'));
|
|
18
|
+
});
|
|
19
|
+
const sharedKeyCredential = new storage_blob_1.StorageSharedKeyCredential(options.account, options.accountKey);
|
|
20
|
+
this.blobServiceClient = new storage_blob_1.BlobServiceClient(`https://${options.account}.blob.core.windows.net`, sharedKeyCredential);
|
|
21
|
+
}
|
|
22
|
+
_handleFile(req, file, cb) {
|
|
23
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
try {
|
|
25
|
+
const dist = yield this.fileDistFunction(file, req);
|
|
26
|
+
const key = yield this.fileNameFunction(file, req);
|
|
27
|
+
const filePath = (0, path_1.join)(dist, key);
|
|
28
|
+
file.stream.pipe((0, concat_stream_1.default)({ encoding: 'buffer' }, (buffer) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
var _a;
|
|
30
|
+
const uploadedFile = yield this.putFile(buffer, filePath);
|
|
31
|
+
const fileInfo = Object.assign(Object.assign({}, uploadedFile), { fieldName: file.fieldname, originalName: file.originalname, mimetype: file.mimetype });
|
|
32
|
+
let transformData = fileInfo;
|
|
33
|
+
if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.transformUploadedFileObject) {
|
|
34
|
+
transformData = yield this.options.transformUploadedFileObject(fileInfo);
|
|
35
|
+
}
|
|
36
|
+
cb(null, transformData);
|
|
37
|
+
})));
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
cb(error);
|
|
41
|
+
}
|
|
42
|
+
file.stream.on('error', (err) => cb(err));
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
_removeFile(_req, file, cb) {
|
|
46
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
try {
|
|
48
|
+
const blobClient = this.blobServiceClient
|
|
49
|
+
.getContainerClient(this.options.container)
|
|
50
|
+
.getBlobClient(file.key);
|
|
51
|
+
yield blobClient.delete();
|
|
52
|
+
cb(null);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
cb(error);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
getUrl(key) {
|
|
60
|
+
return `https://${this.options.account}.blob.core.windows.net/${this.options.container}/${key}`;
|
|
61
|
+
}
|
|
62
|
+
getSignedUrl(key, signatureValues = {}) {
|
|
63
|
+
if (!key)
|
|
64
|
+
return null;
|
|
65
|
+
const cloudFrontDomain = process.env.AZURE_CDN_DOMAIN_NAME;
|
|
66
|
+
if (cloudFrontDomain) {
|
|
67
|
+
return `${cloudFrontDomain}/${key}`;
|
|
68
|
+
}
|
|
69
|
+
const containerClient = this.blobServiceClient.getContainerClient(this.options.container);
|
|
70
|
+
const blobClient = containerClient.getBlobClient(key);
|
|
71
|
+
const sharedKeyCredential = new storage_blob_1.StorageSharedKeyCredential(this.options.account, this.options.accountKey);
|
|
72
|
+
const expiresOn = new Date();
|
|
73
|
+
expiresOn.setHours(expiresOn.getHours() + 1);
|
|
74
|
+
const sasToken = (0, storage_blob_1.generateBlobSASQueryParameters)(Object.assign({ containerName: this.options.container, blobName: key, permissions: storage_blob_1.BlobSASPermissions.parse('r'), protocol: storage_blob_1.SASProtocol.Https, startsOn: new Date(), expiresOn }, signatureValues), sharedKeyCredential).toString();
|
|
75
|
+
return `${blobClient.url}?${sasToken}`;
|
|
76
|
+
}
|
|
77
|
+
getFile(key) {
|
|
78
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
const containerClient = this.blobServiceClient.getContainerClient(this.options.container);
|
|
80
|
+
const blobClient = containerClient.getBlobClient(key);
|
|
81
|
+
const downloadResponse = yield blobClient.download();
|
|
82
|
+
const stream = downloadResponse.readableStreamBody;
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
const chunks = [];
|
|
85
|
+
stream === null || stream === void 0 ? void 0 : stream.on('data', (chunk) => chunks.push(chunk));
|
|
86
|
+
stream === null || stream === void 0 ? void 0 : stream.on('end', () => resolve(Buffer.concat(chunks)));
|
|
87
|
+
stream === null || stream === void 0 ? void 0 : stream.on('error', reject);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
putFile(buffer, key) {
|
|
92
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
try {
|
|
94
|
+
const containerClient = this.blobServiceClient.getContainerClient(this.options.container);
|
|
95
|
+
const blockBlobClient = containerClient.getBlockBlobClient(key);
|
|
96
|
+
yield blockBlobClient.uploadData(buffer);
|
|
97
|
+
const fileInfo = {
|
|
98
|
+
originalName: (0, path_1.basename)(key),
|
|
99
|
+
fileName: (0, path_1.basename)(key),
|
|
100
|
+
size: buffer.length,
|
|
101
|
+
buffer,
|
|
102
|
+
key,
|
|
103
|
+
fullPath: key,
|
|
104
|
+
url: this.getUrl(key),
|
|
105
|
+
};
|
|
106
|
+
return fileInfo;
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
console.error(`Error uploading file "${key}":`, error);
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
deleteFile(key) {
|
|
115
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
116
|
+
try {
|
|
117
|
+
const blobClient = this.blobServiceClient
|
|
118
|
+
.getContainerClient(this.options.container)
|
|
119
|
+
.getBlobClient(key);
|
|
120
|
+
yield blobClient.delete();
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
console.error(`Error deleting blob "${key}":`, error);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
copyFile(oldKey, newKey) {
|
|
128
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
try {
|
|
130
|
+
const containerClient = this.blobServiceClient.getContainerClient(this.options.container);
|
|
131
|
+
const sourceBlobClient = containerClient.getBlobClient(oldKey);
|
|
132
|
+
const destinationBlobClient = containerClient.getBlobClient(newKey);
|
|
133
|
+
const copyPoller = yield destinationBlobClient.beginCopyFromURL(sourceBlobClient.url);
|
|
134
|
+
yield copyPoller.pollUntilDone();
|
|
135
|
+
const properties = yield destinationBlobClient.getProperties();
|
|
136
|
+
return {
|
|
137
|
+
originalName: (0, path_1.basename)(newKey),
|
|
138
|
+
size: properties.contentLength || 0,
|
|
139
|
+
fileName: (0, path_1.basename)(newKey),
|
|
140
|
+
key: newKey,
|
|
141
|
+
fullPath: newKey,
|
|
142
|
+
url: this.getUrl(newKey),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
console.error('Error copying file in Azure Blob Storage:', error);
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.AzureStorage = AzureStorage;
|
|
153
|
+
//# sourceMappingURL=azure.storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"azure.storage.js","sourceRoot":"","sources":["../../../../../../packages/nest-file-storage/src/lib/storage/azure.storage.ts"],"names":[],"mappings":";;;;AAAA,sDAO6B;AAC7B,0EAAmC;AACnC,4DAA4B;AAE5B,qDAA4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G,MAAM,qBAAqB,CAAC,aAAa,EAAE,CAAC;gBAE/D,OAAO;oBACH,YAAY,EAAE,IAAA,eAAQ,EAAC,MAAM,CAAC;oBAC9B,IAAI,EAAE,UAAU,CAAC,aAAa,IAAI,CAAC;oBACnC,QAAQ,EAAE,IAAA,eAAQ,EAAC,MAAM,CAAC;oBAC1B,GAAG,EAAE,MAAM;oBACX,QAAQ,EAAE,MAAM;oBAChB,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;iBAC3B,CAAC;YACN,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACb,OAAO,CAAC,KAAK,CAAC,2CAA2C,EAAE,KAAK,CAAC,CAAC;gBAClE,MAAM,KAAK,CAAC;YAChB,CAAC;QACL,CAAC;KAAA;CAGJ;AApMD,oCAoMC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { StorageEngine } from 'multer';
|
|
2
|
+
import { LocalStorageOptions, Storage, UploadedFile } from '../types';
|
|
3
|
+
export declare class LocalStorage implements StorageEngine, Storage {
|
|
4
|
+
private options;
|
|
5
|
+
private rootPath;
|
|
6
|
+
private fileNameFunction;
|
|
7
|
+
private fileDistFunction;
|
|
8
|
+
constructor(options: LocalStorageOptions);
|
|
9
|
+
_handleFile(req: any, file: Express.Multer.File, cb: (error?: any, info?: any) => void): Promise<void>;
|
|
10
|
+
_removeFile(_req: any, file: any, cb: (error: Error | null) => void): void;
|
|
11
|
+
getUrl(filePath: string): string;
|
|
12
|
+
getFile(file: string): Promise<Buffer>;
|
|
13
|
+
deleteFile(file: string): Promise<void>;
|
|
14
|
+
putFile(fileContent: Buffer, key: string): Promise<UploadedFile>;
|
|
15
|
+
path(filePath: string): string;
|
|
16
|
+
copyFile(oldKey: string, newKey: string): Promise<UploadedFile>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalStorage = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const concat_stream_1 = tslib_1.__importDefault(require("concat-stream"));
|
|
6
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
7
|
+
const moment_1 = tslib_1.__importDefault(require("moment"));
|
|
8
|
+
const path = tslib_1.__importStar(require("path"));
|
|
9
|
+
const path_1 = require("path");
|
|
10
|
+
const uuid_1 = require("uuid");
|
|
11
|
+
class LocalStorage {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.options = options;
|
|
14
|
+
this.rootPath = options.rootPath || path.join(process.cwd(), 'public');
|
|
15
|
+
this.fileNameFunction = options.fileName || ((file, _req) => {
|
|
16
|
+
return `${(0, uuid_1.v4)()}-${file.originalname}`;
|
|
17
|
+
});
|
|
18
|
+
this.fileDistFunction = options.fileDist || ((_file, _req) => {
|
|
19
|
+
return path.join(this.rootPath, (0, moment_1.default)().format('YYYY'), (0, moment_1.default)().format('MM'), (0, moment_1.default)().format('DD'));
|
|
20
|
+
});
|
|
21
|
+
if (!fs.existsSync(this.rootPath)) {
|
|
22
|
+
fs.mkdirSync(this.rootPath, { recursive: true });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
_handleFile(req, file, cb) {
|
|
26
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
try {
|
|
28
|
+
const dist = yield this.fileDistFunction(file, req);
|
|
29
|
+
const key = yield this.fileNameFunction(file, req);
|
|
30
|
+
const filePath = (0, path_1.join)(dist, key);
|
|
31
|
+
file.stream.pipe((0, concat_stream_1.default)({ encoding: 'buffer' }, (buffer) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
var _a;
|
|
33
|
+
const uploadedFile = yield this.putFile(buffer, filePath);
|
|
34
|
+
const fileInfo = Object.assign(Object.assign({}, uploadedFile), { fieldName: file.fieldname, originalName: file.originalname, mimetype: file.mimetype });
|
|
35
|
+
let transformData = fileInfo;
|
|
36
|
+
if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.transformUploadedFileObject) {
|
|
37
|
+
transformData = yield this.options.transformUploadedFileObject(fileInfo);
|
|
38
|
+
}
|
|
39
|
+
cb(null, transformData);
|
|
40
|
+
})));
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error('error', error);
|
|
44
|
+
cb(error);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
_removeFile(_req, file, cb) {
|
|
49
|
+
const filePath = file.path;
|
|
50
|
+
fs.unlink(filePath, (err) => {
|
|
51
|
+
if (err) {
|
|
52
|
+
cb(err);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
cb(null);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
getUrl(filePath) {
|
|
60
|
+
if (filePath && filePath.startsWith('http')) {
|
|
61
|
+
return filePath;
|
|
62
|
+
}
|
|
63
|
+
return filePath ? `${this.options.baseUrl}/${filePath}` : null;
|
|
64
|
+
}
|
|
65
|
+
getFile(file) {
|
|
66
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
return fs.promises.readFile(this.path(file));
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
deleteFile(file) {
|
|
71
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
return fs.promises.unlink(this.path(file));
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
putFile(fileContent, key) {
|
|
76
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
return new Promise((putFileResolve, reject) => {
|
|
78
|
+
const path = (0, path_1.join)(this.options.rootPath, key);
|
|
79
|
+
const directoryPath = (0, path_1.dirname)(path);
|
|
80
|
+
fs.mkdirSync(directoryPath, { recursive: true });
|
|
81
|
+
fs.writeFile(path, fileContent, (err) => {
|
|
82
|
+
if (err) {
|
|
83
|
+
reject(err);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const stats = fs.statSync(path);
|
|
87
|
+
const baseName = (0, path_1.basename)(key);
|
|
88
|
+
const fileInfo = {
|
|
89
|
+
originalName: baseName,
|
|
90
|
+
size: stats.size,
|
|
91
|
+
fileName: baseName,
|
|
92
|
+
key,
|
|
93
|
+
url: this.getUrl(key),
|
|
94
|
+
fullPath: path,
|
|
95
|
+
};
|
|
96
|
+
putFileResolve(fileInfo);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
path(filePath) {
|
|
102
|
+
return filePath ? `${this.options.rootPath}/${filePath}` : null;
|
|
103
|
+
}
|
|
104
|
+
copyFile(oldKey, newKey) {
|
|
105
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
106
|
+
return new Promise((resolve, reject) => {
|
|
107
|
+
const oldPath = (0, path_1.join)(this.options.rootPath, oldKey);
|
|
108
|
+
const newPath = (0, path_1.join)(this.options.rootPath, newKey);
|
|
109
|
+
const directoryPath = (0, path_1.dirname)(newPath);
|
|
110
|
+
fs.mkdirSync(directoryPath, { recursive: true });
|
|
111
|
+
fs.copyFile(oldPath, newPath, (err) => {
|
|
112
|
+
if (err) {
|
|
113
|
+
reject(err);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const stats = fs.statSync(newPath);
|
|
117
|
+
const baseName = (0, path_1.basename)(newKey);
|
|
118
|
+
const fileInfo = {
|
|
119
|
+
originalName: baseName,
|
|
120
|
+
size: stats.size,
|
|
121
|
+
fileName: baseName,
|
|
122
|
+
key: newKey,
|
|
123
|
+
fullPath: newPath,
|
|
124
|
+
url: this.getUrl(newKey),
|
|
125
|
+
};
|
|
126
|
+
resolve(fileInfo);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
exports.LocalStorage = LocalStorage;
|
|
133
|
+
//# sourceMappingURL=local.storage.js.map
|
|
@@ -0,0 +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M,CAAC,GAAG,CAAC,CAAC;wBACZ,OAAO;oBACX,CAAC;oBAED,MAAM,KAAK,GAAG,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;oBACnC,MAAM,QAAQ,GAAG,IAAA,eAAQ,EAAC,MAAM,CAAC,CAAC;oBAClC,MAAM,QAAQ,GAAiB;wBAC3B,YAAY,EAAE,QAAQ;wBACtB,IAAI,EAAE,KAAK,CAAC,IAAI;wBAChB,QAAQ,EAAE,QAAQ;wBAClB,GAAG,EAAE,MAAM;wBACX,QAAQ,EAAE,OAAO;wBACjB,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;qBAC3B,CAAC;oBACF,OAAO,CAAC,QAAQ,CAAC,CAAC;gBACtB,CAAC,CAAC,CAAC;YACP,CAAC,CAAC,CAAC;QACP,CAAC;KAAA;CAGJ;AA7JD,oCA6JC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { GetObjectCommandInput } from '@aws-sdk/client-s3';
|
|
2
|
+
import { StorageEngine } from 'multer';
|
|
3
|
+
import { S3StorageOptions, Storage, UploadedFile } from '../types';
|
|
4
|
+
export declare class S3Storage implements StorageEngine, Storage {
|
|
5
|
+
private options;
|
|
6
|
+
private s3;
|
|
7
|
+
private fileNameFunction;
|
|
8
|
+
private fileDistFunction;
|
|
9
|
+
constructor(options: S3StorageOptions);
|
|
10
|
+
_handleFile(req: any, file: Express.Multer.File, cb: (error?: any, info?: any) => void): Promise<void>;
|
|
11
|
+
_removeFile(_req: any, file: any, cb: (error: Error | null) => void): void;
|
|
12
|
+
getUrl(key: string): string;
|
|
13
|
+
getSignedUrl(key: string, objectConfig?: Partial<GetObjectCommandInput>): Promise<string>;
|
|
14
|
+
getFile(key: string): Promise<Buffer>;
|
|
15
|
+
putFile(fileContent: Buffer, key: string): Promise<any>;
|
|
16
|
+
deleteFile(key: string): Promise<void>;
|
|
17
|
+
private streamToBuffer;
|
|
18
|
+
copyFile(oldKey: string, newKey: string): Promise<UploadedFile>;
|
|
19
|
+
}
|