@ackplus/nest-file-storage 1.1.11 → 1.1.13-beta.1
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/LICENSE +22 -0
- package/README.md +658 -6
- package/{src → dist}/index.d.ts +0 -1
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/{src → dist}/lib/constants.d.ts +0 -1
- package/{src → dist}/lib/constants.js +1 -0
- package/dist/lib/constants.js.map +1 -0
- package/{src → dist}/lib/file-storage.service.d.ts +0 -1
- package/{src → dist}/lib/file-storage.service.js +3 -3
- package/dist/lib/file-storage.service.js.map +1 -0
- package/{src → dist}/lib/index.d.ts +0 -1
- package/dist/lib/index.js +22 -0
- package/dist/lib/index.js.map +1 -0
- package/{src → dist}/lib/interceptor/file-storage.interceptor.d.ts +0 -4
- package/{src → dist}/lib/interceptor/file-storage.interceptor.js +5 -17
- package/dist/lib/interceptor/file-storage.interceptor.js.map +1 -0
- package/{src → dist}/lib/nest-file-storage.module.d.ts +0 -1
- package/{src → dist}/lib/nest-file-storage.module.js +9 -3
- package/dist/lib/nest-file-storage.module.js.map +1 -0
- package/{src → dist}/lib/storage/azure.storage.d.ts +3 -4
- package/{src → dist}/lib/storage/azure.storage.js +97 -46
- package/dist/lib/storage/azure.storage.js.map +1 -0
- package/{src → dist}/lib/storage/local.storage.d.ts +2 -17
- package/{src → dist}/lib/storage/local.storage.js +81 -58
- package/dist/lib/storage/local.storage.js.map +1 -0
- package/{src → dist}/lib/storage/s3.storage.d.ts +3 -4
- package/{src → dist}/lib/storage/s3.storage.js +76 -32
- package/dist/lib/storage/s3.storage.js.map +1 -0
- package/{src → dist}/lib/storage.factory.d.ts +0 -1
- package/dist/lib/storage.factory.js +46 -0
- package/dist/lib/storage.factory.js.map +1 -0
- package/{src → dist}/lib/types.d.ts +1 -15
- package/{src → dist}/lib/types.js +1 -0
- package/dist/lib/types.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/examples/1-basic-local-storage.example.ts +22 -0
- package/examples/10-testing.example.ts +233 -0
- package/examples/2-s3-storage.example.ts +40 -0
- package/examples/3-azure-storage.example.ts +35 -0
- package/examples/4-upload-controller.example.ts +117 -0
- package/examples/5-custom-configuration.example.ts +75 -0
- package/examples/6-file-service.example.ts +124 -0
- package/examples/7-user-avatar.example.ts +139 -0
- package/examples/8-document-management.example.ts +265 -0
- package/examples/9-dynamic-storage.example.ts +175 -0
- package/examples/README.md +122 -0
- package/package.json +84 -5
- package/src/index.d.ts.map +0 -1
- package/src/index.js +0 -7
- package/src/lib/constants.d.ts.map +0 -1
- package/src/lib/file-storage.service.d.ts.map +0 -1
- package/src/lib/index.d.ts.map +0 -1
- package/src/lib/index.js +0 -8
- package/src/lib/interceptor/file-storage.interceptor.d.ts.map +0 -1
- package/src/lib/nest-file-storage.module.d.ts.map +0 -1
- package/src/lib/storage/azure.storage.d.ts.map +0 -1
- package/src/lib/storage/local.storage.d.ts.map +0 -1
- package/src/lib/storage/s3.storage.d.ts.map +0 -1
- package/src/lib/storage.factory.d.ts.map +0 -1
- package/src/lib/storage.factory.js +0 -81
- package/src/lib/types.d.ts.map +0 -1
|
@@ -1,52 +1,73 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
39
|
exports.LocalStorage = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const moment_1 = tslib_1.__importDefault(require("moment"));
|
|
40
|
+
const concat_stream_1 = __importDefault(require("concat-stream"));
|
|
41
|
+
const fs = __importStar(require("fs"));
|
|
42
|
+
const moment_1 = __importDefault(require("moment"));
|
|
8
43
|
const path_1 = require("path");
|
|
9
44
|
const uuid_1 = require("uuid");
|
|
10
45
|
class LocalStorage {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
* Unix: /uploads/2024/01/file.jpg -> 2024/01/file.jpg
|
|
16
|
-
*/
|
|
46
|
+
options;
|
|
47
|
+
rootPath;
|
|
48
|
+
fileNameFunction;
|
|
49
|
+
fileDistFunction;
|
|
17
50
|
pathToUrl(filePath) {
|
|
18
51
|
if (!filePath)
|
|
19
52
|
return '';
|
|
20
|
-
// Remove rootPath if present
|
|
21
53
|
let relativePath = filePath;
|
|
22
54
|
if (filePath.startsWith(this.rootPath)) {
|
|
23
55
|
relativePath = filePath.substring(this.rootPath.length);
|
|
24
56
|
}
|
|
25
|
-
// Convert backslashes to forward slashes and remove leading slash
|
|
26
57
|
return relativePath.replace(/\\/g, '/').replace(/^\/+/, '');
|
|
27
58
|
}
|
|
28
|
-
/**
|
|
29
|
-
* Convert URL-friendly key to OS-specific file path
|
|
30
|
-
* This converts stored keys back to valid file system paths
|
|
31
|
-
* 2024/01/file.jpg -> Windows: 2024\01\file.jpg, Unix: 2024/01/file.jpg
|
|
32
|
-
*/
|
|
33
59
|
urlToPath(urlKey) {
|
|
34
60
|
if (!urlKey)
|
|
35
61
|
return '';
|
|
36
|
-
// Split by forward slashes and rejoin with OS-specific separator
|
|
37
62
|
const parts = urlKey.split('/').filter(part => part.length > 0);
|
|
38
63
|
return parts.join(path_1.sep);
|
|
39
64
|
}
|
|
40
|
-
/**
|
|
41
|
-
* Get full file system path from URL-friendly key
|
|
42
|
-
*/
|
|
43
65
|
getFullPath(urlKey) {
|
|
44
66
|
const osPath = this.urlToPath(urlKey);
|
|
45
67
|
return (0, path_1.join)(this.rootPath, osPath);
|
|
46
68
|
}
|
|
47
69
|
constructor(options) {
|
|
48
70
|
this.options = options;
|
|
49
|
-
// Normalize path for the current OS
|
|
50
71
|
this.rootPath = (0, path_1.normalize)(options.rootPath || (0, path_1.join)(process.cwd(), 'public'));
|
|
51
72
|
this.fileNameFunction = options.fileName || ((file, _req) => {
|
|
52
73
|
return `${(0, uuid_1.v4)()}-${file.originalname}`;
|
|
@@ -54,37 +75,45 @@ class LocalStorage {
|
|
|
54
75
|
this.fileDistFunction = options.fileDist || ((_file, _req) => {
|
|
55
76
|
return (0, path_1.join)(this.rootPath, (0, moment_1.default)().format('YYYY'), (0, moment_1.default)().format('MM'), (0, moment_1.default)().format('DD'));
|
|
56
77
|
});
|
|
57
|
-
// Ensure the rootPath directory exists
|
|
58
78
|
if (!fs.existsSync(this.rootPath)) {
|
|
59
79
|
fs.mkdirSync(this.rootPath, { recursive: true });
|
|
60
80
|
}
|
|
61
81
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
82
|
+
_handleFile(req, file, cb) {
|
|
83
|
+
const run = async () => {
|
|
84
|
+
try {
|
|
85
|
+
const dist = await this.fileDistFunction(file, req);
|
|
86
|
+
const fileName = await this.fileNameFunction(file, req);
|
|
87
|
+
const filePath = (0, path_1.join)(dist, fileName);
|
|
88
|
+
const urlKey = this.pathToUrl(filePath);
|
|
89
|
+
file.stream.pipe((0, concat_stream_1.default)({ encoding: 'buffer' }, (buffer) => {
|
|
90
|
+
void (async () => {
|
|
91
|
+
try {
|
|
92
|
+
const uploadedFile = await this.putFile(buffer, urlKey);
|
|
93
|
+
const fileInfo = {
|
|
94
|
+
...uploadedFile,
|
|
95
|
+
fieldName: file.fieldname,
|
|
96
|
+
originalName: file.originalname,
|
|
97
|
+
mimetype: file.mimetype,
|
|
98
|
+
};
|
|
99
|
+
let transformData = fileInfo;
|
|
100
|
+
if (this.options?.transformUploadedFileObject) {
|
|
101
|
+
transformData = await this.options.transformUploadedFileObject(fileInfo);
|
|
102
|
+
}
|
|
103
|
+
cb(null, transformData);
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
cb(error);
|
|
107
|
+
}
|
|
108
|
+
})();
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
console.error('error', error);
|
|
113
|
+
cb(error);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
run().catch(cb);
|
|
88
117
|
}
|
|
89
118
|
_removeFile(_req, file, cb) {
|
|
90
119
|
const filePath = file.path;
|
|
@@ -104,28 +133,22 @@ class LocalStorage {
|
|
|
104
133
|
if (!urlKey) {
|
|
105
134
|
return '';
|
|
106
135
|
}
|
|
107
|
-
// Key is already in URL format (forward slashes)
|
|
108
|
-
// Ensure baseUrl doesn't end with slash and key doesn't start with slash
|
|
109
136
|
const baseUrl = this.options.baseUrl.replace(/\/$/, '');
|
|
110
137
|
const cleanKey = urlKey.replace(/^\//, '');
|
|
111
138
|
return `${baseUrl}/${cleanKey}`;
|
|
112
139
|
}
|
|
113
140
|
async getFile(urlKey) {
|
|
114
|
-
// Convert URL key to OS-specific path
|
|
115
141
|
const fullPath = this.getFullPath(urlKey);
|
|
116
142
|
return fs.promises.readFile(fullPath);
|
|
117
143
|
}
|
|
118
144
|
async deleteFile(urlKey) {
|
|
119
|
-
// Convert URL key to OS-specific path
|
|
120
145
|
const fullPath = this.getFullPath(urlKey);
|
|
121
146
|
return fs.promises.unlink(fullPath);
|
|
122
147
|
}
|
|
123
|
-
async putFile(fileContent, urlKey) {
|
|
148
|
+
async putFile(fileContent, urlKey, options) {
|
|
124
149
|
return new Promise((putFileResolve, reject) => {
|
|
125
|
-
// Convert URL key to OS-specific path
|
|
126
150
|
const filePath = this.getFullPath(urlKey);
|
|
127
151
|
const directoryPath = (0, path_1.dirname)(filePath);
|
|
128
|
-
// Create the directory if it doesn't exist
|
|
129
152
|
fs.mkdirSync(directoryPath, { recursive: true });
|
|
130
153
|
fs.writeFile(filePath, fileContent, (err) => {
|
|
131
154
|
if (err) {
|
|
@@ -141,6 +164,7 @@ class LocalStorage {
|
|
|
141
164
|
key: urlKey,
|
|
142
165
|
url: this.getUrl(urlKey),
|
|
143
166
|
fullPath: filePath,
|
|
167
|
+
...options,
|
|
144
168
|
};
|
|
145
169
|
putFileResolve(fileInfo);
|
|
146
170
|
});
|
|
@@ -150,12 +174,10 @@ class LocalStorage {
|
|
|
150
174
|
if (!urlKey) {
|
|
151
175
|
return '';
|
|
152
176
|
}
|
|
153
|
-
// Convert URL key to full OS-specific path
|
|
154
177
|
return this.getFullPath(urlKey);
|
|
155
178
|
}
|
|
156
179
|
async copyFile(oldUrlKey, newUrlKey) {
|
|
157
180
|
return new Promise((resolve, reject) => {
|
|
158
|
-
// Convert URL keys to OS-specific paths
|
|
159
181
|
const oldPath = this.getFullPath(oldUrlKey);
|
|
160
182
|
const newPath = this.getFullPath(newUrlKey);
|
|
161
183
|
const directoryPath = (0, path_1.dirname)(newPath);
|
|
@@ -181,3 +203,4 @@ class LocalStorage {
|
|
|
181
203
|
}
|
|
182
204
|
}
|
|
183
205
|
exports.LocalStorage = LocalStorage;
|
|
206
|
+
//# sourceMappingURL=local.storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local.storage.js","sourceRoot":"","sources":["../../../src/lib/storage/local.storage.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kEAAmC;AACnC,uCAAyB;AACzB,oDAA4B;AAE5B,+BAAqD;AACrD,+BAAoC;AAKpC,MAAa,YAAY;IA8CD;IA5CZ,QAAQ,CAAS;IACjB,gBAAgB,CAAqE;IACrF,gBAAgB,CAAqE;IAQrF,SAAS,CAAC,QAAgB;QAC9B,IAAI,CAAC,QAAQ;YAAE,OAAO,EAAE,CAAC;QAGzB,IAAI,YAAY,GAAG,QAAQ,CAAC;QAC5B,IAAI,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;YACrC,YAAY,GAAG,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC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iB;oBAC3B,YAAY,EAAE,QAAQ;oBACtB,IAAI,EAAE,KAAK,CAAC,IAAI;oBAChB,QAAQ,EAAE,QAAQ;oBAClB,GAAG,EAAE,SAAS;oBACd,QAAQ,EAAE,OAAO;oBACjB,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC;iBAC9B,CAAC;gBACF,OAAO,CAAC,QAAQ,CAAC,CAAC;YACtB,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;IACP,CAAC;CAGJ;AAxOD,oCAwOC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GetObjectCommandInput } from '@aws-sdk/client-s3';
|
|
1
|
+
import { GetObjectCommandInput, PutObjectCommandInput } from '@aws-sdk/client-s3';
|
|
2
2
|
import { StorageEngine } from 'multer';
|
|
3
3
|
import { S3StorageOptions, Storage, UploadedFile } from '../types';
|
|
4
4
|
export declare class S3Storage implements StorageEngine, Storage {
|
|
@@ -7,14 +7,13 @@ export declare class S3Storage implements StorageEngine, Storage {
|
|
|
7
7
|
private fileNameFunction;
|
|
8
8
|
private fileDistFunction;
|
|
9
9
|
constructor(options: S3StorageOptions);
|
|
10
|
-
_handleFile(req: any, file: Express.Multer.File, cb: (error?: any, info?: any) => void):
|
|
10
|
+
_handleFile(req: any, file: Express.Multer.File, cb: (error?: any, info?: any) => void): void;
|
|
11
11
|
_removeFile(_req: any, file: any, cb: (error: Error | null) => void): void;
|
|
12
12
|
getUrl(key: string): string;
|
|
13
13
|
getSignedUrl(key: string, objectConfig?: Partial<GetObjectCommandInput>): Promise<string>;
|
|
14
14
|
getFile(key: string): Promise<Buffer>;
|
|
15
|
-
putFile(fileContent: Buffer, key: string): Promise<any>;
|
|
15
|
+
putFile(fileContent: Buffer, key: string, options?: Partial<PutObjectCommandInput>): Promise<any>;
|
|
16
16
|
deleteFile(key: string): Promise<void>;
|
|
17
17
|
private streamToBuffer;
|
|
18
18
|
copyFile(oldKey: string, newKey: string): Promise<UploadedFile>;
|
|
19
19
|
}
|
|
20
|
-
//# sourceMappingURL=s3.storage.d.ts.map
|
|
@@ -1,15 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
39
|
exports.S3Storage = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
40
|
const client_s3_1 = require("@aws-sdk/client-s3");
|
|
6
41
|
const client_s3_2 = require("@aws-sdk/client-s3");
|
|
7
42
|
const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
|
|
8
|
-
const moment_1 =
|
|
9
|
-
const path_1 =
|
|
43
|
+
const moment_1 = __importDefault(require("moment"));
|
|
44
|
+
const path_1 = __importStar(require("path"));
|
|
10
45
|
const stream_1 = require("stream");
|
|
11
46
|
const uuid_1 = require("uuid");
|
|
12
47
|
class S3Storage {
|
|
48
|
+
options;
|
|
49
|
+
s3;
|
|
50
|
+
fileNameFunction;
|
|
51
|
+
fileDistFunction;
|
|
13
52
|
constructor(options) {
|
|
14
53
|
this.options = options;
|
|
15
54
|
this.fileNameFunction = options.fileName || ((file, _req) => {
|
|
@@ -27,32 +66,35 @@ class S3Storage {
|
|
|
27
66
|
},
|
|
28
67
|
});
|
|
29
68
|
}
|
|
30
|
-
|
|
31
|
-
// Collect file chunks to determine size
|
|
69
|
+
_handleFile(req, file, cb) {
|
|
32
70
|
const chunks = [];
|
|
33
71
|
file.stream.on('data', (chunk) => chunks.push(chunk));
|
|
34
|
-
file.stream.on('end',
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
72
|
+
file.stream.on('end', () => {
|
|
73
|
+
(async () => {
|
|
74
|
+
const buffer = Buffer.concat(chunks);
|
|
75
|
+
try {
|
|
76
|
+
const dist = await this.fileDistFunction(file, req);
|
|
77
|
+
const key = await this.fileNameFunction(file, req);
|
|
78
|
+
const filePath = (0, path_1.join)(dist, key);
|
|
79
|
+
const uploadedFile = await this.putFile(buffer, filePath, {
|
|
80
|
+
ContentType: file?.mimetype
|
|
81
|
+
});
|
|
82
|
+
const fileInfo = {
|
|
83
|
+
...uploadedFile,
|
|
84
|
+
fieldName: file.fieldname,
|
|
85
|
+
originalName: file.originalname,
|
|
86
|
+
mimetype: file.mimetype,
|
|
87
|
+
};
|
|
88
|
+
let transformData = fileInfo;
|
|
89
|
+
if (this.options?.transformUploadedFileObject) {
|
|
90
|
+
transformData = await this.options.transformUploadedFileObject(fileInfo);
|
|
91
|
+
}
|
|
92
|
+
cb(null, transformData);
|
|
50
93
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
cb(err);
|
|
96
|
+
}
|
|
97
|
+
})().catch((err) => cb(err));
|
|
56
98
|
});
|
|
57
99
|
file.stream.on('error', (err) => cb(err));
|
|
58
100
|
}
|
|
@@ -96,7 +138,6 @@ class S3Storage {
|
|
|
96
138
|
if (!data.Body) {
|
|
97
139
|
throw new Error('Empty response received from S3.');
|
|
98
140
|
}
|
|
99
|
-
// Handle both Buffer and Readable Stream responses
|
|
100
141
|
if (data.Body instanceof stream_1.Readable) {
|
|
101
142
|
return await this.streamToBuffer(data.Body);
|
|
102
143
|
}
|
|
@@ -107,26 +148,28 @@ class S3Storage {
|
|
|
107
148
|
throw error;
|
|
108
149
|
}
|
|
109
150
|
}
|
|
110
|
-
async putFile(fileContent, key) {
|
|
151
|
+
async putFile(fileContent, key, options) {
|
|
111
152
|
try {
|
|
112
153
|
const fileName = (0, path_1.basename)(key);
|
|
113
154
|
const fileKey = key || (Math.random() + 1).toString(36).substring(12);
|
|
114
|
-
|
|
155
|
+
const safeAscii = fileName
|
|
156
|
+
.replace(/[^\x20-\x7E]/g, '_')
|
|
157
|
+
.replace(/["\\]/g, '_');
|
|
158
|
+
const encodedFileName = encodeURIComponent(fileName);
|
|
115
159
|
const putParams = {
|
|
116
160
|
Bucket: this.options.bucket,
|
|
117
161
|
Key: fileKey,
|
|
118
162
|
Body: fileContent,
|
|
119
|
-
ContentDisposition: `inline; ${
|
|
163
|
+
ContentDisposition: `inline; filename="${safeAscii}"; filename*=UTF-8''${encodedFileName}`,
|
|
164
|
+
...options,
|
|
120
165
|
};
|
|
121
166
|
await this.s3.send(new client_s3_2.PutObjectCommand(putParams));
|
|
122
|
-
// Fetch file metadata to get size
|
|
123
167
|
const headParams = {
|
|
124
168
|
Bucket: this.options.bucket,
|
|
125
169
|
Key: fileKey,
|
|
126
170
|
};
|
|
127
171
|
const headObject = await this.s3.send(new client_s3_2.HeadObjectCommand(headParams));
|
|
128
172
|
const fileSize = headObject.ContentLength || 0;
|
|
129
|
-
// Construct response object
|
|
130
173
|
const fileData = {
|
|
131
174
|
originalName: fileName,
|
|
132
175
|
fileName: fileName,
|
|
@@ -193,3 +236,4 @@ class S3Storage {
|
|
|
193
236
|
}
|
|
194
237
|
}
|
|
195
238
|
exports.S3Storage = S3Storage;
|
|
239
|
+
//# sourceMappingURL=s3.storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"s3.storage.js","sourceRoot":"","sources":["../../../src/lib/storage/s3.storage.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAAyG;AACzG,kDAAwI;AACxI,wEAA6D;AAC7D,oDAA4B;AAE5B,6CAA4C;AAC5C,mCAAkC;AAClC,+BAAoC;AAMpC,MAAa,SAAS;IAME;IAJZ,EAAE,CAAK;IACP,gBAAgB,CAAqE;IACrF,gBAAgB,CAAqE;IAE7F,YAAoB,OAAyB;QAAzB,YAAO,GAAP,OAAO,CAAkB;QACzC,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,QAAQ,IAAI,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE;YACxD,OAAO,GAAG,IAAA,SAAM,GAAE,IAA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AC;YAClD,MAAM,KAAK,CAAC;QAChB,CAAC;IACL,CAAC;CAGJ;AAlPD,8BAkPC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StorageFactory = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
class StorageFactory {
|
|
6
|
+
static storageInstances = new Map();
|
|
7
|
+
static async createStorage(storageType, options) {
|
|
8
|
+
const cacheKey = `${storageType}-${JSON.stringify(options)}`;
|
|
9
|
+
let storageInstance;
|
|
10
|
+
switch (storageType) {
|
|
11
|
+
case types_1.FileStorageEnum.LOCAL:
|
|
12
|
+
const { LocalStorage } = await import('./storage/local.storage.js');
|
|
13
|
+
storageInstance = new LocalStorage(options);
|
|
14
|
+
break;
|
|
15
|
+
case types_1.FileStorageEnum.AZURE:
|
|
16
|
+
try {
|
|
17
|
+
const { AzureStorage } = await import('./storage/azure.storage.js');
|
|
18
|
+
storageInstance = new AzureStorage(options);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
throw new Error('Azure Storage SDK (@azure/storage-blob) is required when using AzureStorage. ' +
|
|
22
|
+
'Please install it: npm install @azure/storage-blob');
|
|
23
|
+
}
|
|
24
|
+
break;
|
|
25
|
+
case types_1.FileStorageEnum.S3:
|
|
26
|
+
try {
|
|
27
|
+
const { S3Storage } = await import('./storage/s3.storage.js');
|
|
28
|
+
storageInstance = new S3Storage(options);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
throw new Error('AWS SDK (@aws-sdk/client-s3 and @aws-sdk/s3-request-presigner) is required when using S3Storage. ' +
|
|
32
|
+
'Please install them: npm install @aws-sdk/client-s3 @aws-sdk/s3-request-presigner');
|
|
33
|
+
}
|
|
34
|
+
break;
|
|
35
|
+
default:
|
|
36
|
+
throw new Error(`Unsupported storage type: ${storageType}`);
|
|
37
|
+
}
|
|
38
|
+
this.storageInstances.set(cacheKey, storageInstance);
|
|
39
|
+
return storageInstance;
|
|
40
|
+
}
|
|
41
|
+
static clearCache() {
|
|
42
|
+
this.storageInstances.clear();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.StorageFactory = StorageFactory;
|
|
46
|
+
//# sourceMappingURL=storage.factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.factory.js","sourceRoot":"","sources":["../../src/lib/storage.factory.ts"],"names":[],"mappings":";;;AAAA,mCAAsH;AAItH,MAAa,cAAc;IACf,MAAM,CAAC,gBAAgB,GAAG,IAAI,GAAG,EAAmC,CAAC;IAE7E,MAAM,CAAC,KAAK,CAAC,aAAa,CAAC,WAA4B,EAAE,OAAuB;QAC5E,MAAM,QAAQ,GAAG,GAAG,WAAW,IAAI,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;QAM7D,IAAI,eAAwC,CAAC;QAE7C,QAAQ,WAAW,EAAE,CAAC;YAClB,KAAK,uBAAe,CAAC,KAAK;gBACtB,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,MAAM,CAAC,4BAA4B,CAAC,CAAC;gBACpE,eAAe,GAAG,IAAI,YAAY,CAAC,OAA8B,CAAC,CAAC;gBACnE,MAAM;YAEV,KAAK,uBAAe,CAAC,KAAK;gBACtB,IAAI,CAAC;oBACD,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,MAAM,CAAC,4BAA4B,CAAC,CAAC;oBACpE,eAAe,GAAG,IAAI,YAAY,CAAC,OAA8B,CAAC,CAAC;gBACvE,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACb,MAAM,IAAI,KAAK,CACX,+EAA+E;wBAC/E,oDAAoD,CACvD,CAAC;gBACN,CAAC;gBACD,MAAM;YAEV,KAAK,uBAAe,CAAC,EAAE;gBACnB,IAAI,CAAC;oBACD,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,MAAM,CAAC,yBAAyB,CAAC,CAAC;oBAC9D,eAAe,GAAG,IAAI,SAAS,CAAC,OAA2B,CAAC,CAAC;gBACjE,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACb,MAAM,IAAI,KAAK,CACX,mGAAmG;wBACnG,mFAAmF,CACtF,CAAC;gBACN,CAAC;gBACD,MAAM;YAEV;gBACI,MAAM,IAAI,KAAK,CAAC,6BAA6B,WAAW,EAAE,CAAC,CAAC;QACpE,CAAC;QAED,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,QAAQ,EAAE,eAAe,CAAC,CAAC;QACrD,OAAO,eAAe,CAAC;IAC3B,CAAC;IAED,MAAM,CAAC,UAAU;QACb,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC;IAClC,CAAC;;AApDL,wCAqDC"}
|
|
@@ -45,22 +45,9 @@ export interface FileStorageClassOptions {
|
|
|
45
45
|
}
|
|
46
46
|
export type FileStorageModuleOptions = FileStorageConfigOptions | FileStorageClassOptions;
|
|
47
47
|
export interface FileStorageAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
48
|
-
/**
|
|
49
|
-
* The `useExisting` syntax allows you to create aliases for existing providers.
|
|
50
|
-
*/
|
|
51
48
|
useExisting?: Type<FileStorageOptionsFactory>;
|
|
52
|
-
/**
|
|
53
|
-
* The `useClass` syntax allows you to dynamically determine a class
|
|
54
|
-
* that a token should resolve to.
|
|
55
|
-
*/
|
|
56
49
|
useClass?: Type<FileStorageOptionsFactory>;
|
|
57
|
-
/**
|
|
58
|
-
* The `useFactory` syntax allows for creating providers dynamically.
|
|
59
|
-
*/
|
|
60
50
|
useFactory?: (...args: any[]) => Promise<FileStorageModuleOptions> | FileStorageModuleOptions;
|
|
61
|
-
/**
|
|
62
|
-
* Optional list of providers to be injected into the context of the Factory function.
|
|
63
|
-
*/
|
|
64
51
|
inject?: any[];
|
|
65
52
|
}
|
|
66
53
|
export interface FileStorageOptionsFactory {
|
|
@@ -82,10 +69,9 @@ export interface UploadedFile {
|
|
|
82
69
|
export interface Storage {
|
|
83
70
|
getFile(key: string): Promise<Buffer> | Buffer;
|
|
84
71
|
deleteFile(key: string): Promise<void> | void;
|
|
85
|
-
putFile(fileContent: Buffer, key: string): Promise<UploadedFile> | UploadedFile;
|
|
72
|
+
putFile(fileContent: Buffer, key: string, options?: any): Promise<UploadedFile> | UploadedFile;
|
|
86
73
|
path?(filePath: string): Promise<string> | string;
|
|
87
74
|
getUrl(key: string): Promise<string> | string;
|
|
88
75
|
getSignedUrl?(key: string, options: any): Promise<string> | string;
|
|
89
76
|
copyFile(oldKey: string, newKey: string): Promise<UploadedFile>;
|
|
90
77
|
}
|
|
91
|
-
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":";;;AAIA,IAAY,eAIX;AAJD,WAAY,eAAe;IACvB,kCAAe,CAAA;IACf,4BAAS,CAAA;IACT,kCAAe,CAAA;AACnB,CAAC,EAJW,eAAe,+BAAf,eAAe,QAI1B"}
|