@ackplus/nest-file-storage 1.1.1 → 1.1.2
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/package.json +2 -2
- package/src/{index.ts → index.d.ts} +1 -0
- package/src/index.d.ts.map +1 -0
- package/src/lib/constants.d.ts +2 -0
- package/src/lib/constants.d.ts.map +1 -0
- package/src/lib/file-storage.service.d.ts +8 -0
- package/src/lib/file-storage.service.d.ts.map +1 -0
- package/src/lib/{index.ts → index.d.ts} +1 -0
- package/src/lib/index.d.ts.map +1 -0
- package/src/lib/interceptor/file-storage.interceptor.d.ts +25 -0
- package/src/lib/interceptor/file-storage.interceptor.d.ts.map +1 -0
- package/src/lib/nest-file-storage.module.d.ts +9 -0
- package/src/lib/nest-file-storage.module.d.ts.map +1 -0
- package/src/lib/storage/azure.storage.d.ts +19 -0
- package/src/lib/storage/azure.storage.d.ts.map +1 -0
- package/src/lib/storage/local.storage.d.ts +35 -0
- package/src/lib/storage/local.storage.d.ts.map +1 -0
- package/src/lib/storage/s3.storage.d.ts +20 -0
- package/src/lib/storage/s3.storage.d.ts.map +1 -0
- package/src/lib/storage.factory.d.ts +9 -0
- package/src/lib/storage.factory.d.ts.map +1 -0
- package/src/lib/{types.ts → types.d.ts} +23 -35
- package/src/lib/types.d.ts.map +1 -0
- package/eslint.config.mjs +0 -22
- package/jest.config.ts +0 -10
- package/project.json +0 -38
- package/src/lib/constants.ts +0 -1
- package/src/lib/file-storage.service.ts +0 -36
- package/src/lib/interceptor/file-storage.interceptor.ts +0 -174
- package/src/lib/nest-file-storage.module.ts +0 -78
- package/src/lib/storage/azure.storage.ts +0 -214
- package/src/lib/storage/local.storage.ts +0 -233
- package/src/lib/storage/s3.storage.ts +0 -242
- package/src/lib/storage.factory.ts +0 -58
- package/tsconfig.json +0 -17
- package/tsconfig.lib.json +0 -14
- package/tsconfig.spec.json +0 -15
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
import { CopyObjectCommand, GetObjectCommandInput, S3 } from '@aws-sdk/client-s3';
|
|
2
|
-
import { DeleteObjectCommand, GetObjectCommand, GetObjectCommandOutput, HeadObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';
|
|
3
|
-
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
4
|
-
import moment from 'moment';
|
|
5
|
-
import { StorageEngine } from 'multer';
|
|
6
|
-
import path, { basename, join } from 'path';
|
|
7
|
-
import { Readable } from 'stream';
|
|
8
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import { S3StorageOptions, Storage, UploadedFile } from '../types';
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export class S3Storage implements StorageEngine, Storage {
|
|
15
|
-
|
|
16
|
-
private s3: S3;
|
|
17
|
-
private fileNameFunction: (file: Express.Multer.File, req?: any) => string | Promise<string>;
|
|
18
|
-
private fileDistFunction: (file: Express.Multer.File, req?: any) => string | Promise<string>;
|
|
19
|
-
|
|
20
|
-
constructor(private options: S3StorageOptions) {
|
|
21
|
-
this.fileNameFunction = options.fileName || ((file, _req) => {
|
|
22
|
-
return `${uuidv4()}-${file.originalname}`;
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
this.fileDistFunction = options.fileDist || ((_file, _req) => {
|
|
26
|
-
return path.join('uploads', moment().format('YYYY'), moment().format('MM'), moment().format('DD'));
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
this.s3 = new S3({
|
|
30
|
-
...(this.options.endpoint ? { endpoint: this.options.endpoint } : {}),
|
|
31
|
-
region: this.options.region,
|
|
32
|
-
credentials: {
|
|
33
|
-
accessKeyId: this.options.accessKeyId,
|
|
34
|
-
secretAccessKey: this.options.secretAccessKey,
|
|
35
|
-
},
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async _handleFile(
|
|
40
|
-
req: any,
|
|
41
|
-
file: Express.Multer.File,
|
|
42
|
-
cb: (error?: any, info?: any) => void,
|
|
43
|
-
): Promise<void> {
|
|
44
|
-
// Collect file chunks to determine size
|
|
45
|
-
const chunks: Uint8Array[] = [];
|
|
46
|
-
file.stream.on('data', (chunk) => chunks.push(chunk));
|
|
47
|
-
file.stream.on('end', async () => {
|
|
48
|
-
const buffer = Buffer.concat(chunks);
|
|
49
|
-
|
|
50
|
-
try {
|
|
51
|
-
const dist = await this.fileDistFunction(file, req);
|
|
52
|
-
const key = await this.fileNameFunction(file, req);
|
|
53
|
-
const filePath = join(dist, key);
|
|
54
|
-
|
|
55
|
-
const uploadedFile = await this.putFile(buffer, filePath);
|
|
56
|
-
|
|
57
|
-
const fileInfo: UploadedFile = {
|
|
58
|
-
...uploadedFile,
|
|
59
|
-
fieldName: file.fieldname,
|
|
60
|
-
originalName: file.originalname,
|
|
61
|
-
mimetype: file.mimetype,
|
|
62
|
-
};
|
|
63
|
-
let transformData = fileInfo;
|
|
64
|
-
|
|
65
|
-
if (this.options?.transformUploadedFileObject) {
|
|
66
|
-
transformData = await this.options.transformUploadedFileObject(fileInfo);
|
|
67
|
-
}
|
|
68
|
-
cb(null, transformData);
|
|
69
|
-
} catch (err) {
|
|
70
|
-
cb(err);
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
file.stream.on('error', (err) => cb(err));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
_removeFile(
|
|
78
|
-
_req: any,
|
|
79
|
-
file: any,
|
|
80
|
-
cb: (error: Error | null) => void,
|
|
81
|
-
): void {
|
|
82
|
-
const params = {
|
|
83
|
-
Bucket: this.options.bucket,
|
|
84
|
-
Key: file.key,
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
this.s3
|
|
88
|
-
.deleteObject(params)
|
|
89
|
-
.then(() => cb(null))
|
|
90
|
-
.catch((err) => cb(err));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
getUrl(key: string) {
|
|
94
|
-
if (this.options?.cloudFrontUrl) {
|
|
95
|
-
return `${this.options.cloudFrontUrl}/${key}`;
|
|
96
|
-
}
|
|
97
|
-
return `https://${this.options.bucket}.s3.amazonaws.com/${key}`;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async getSignedUrl(key: string, objectConfig?: Partial<GetObjectCommandInput>): Promise<string> {
|
|
101
|
-
if (key) {
|
|
102
|
-
const url = await getSignedUrl(
|
|
103
|
-
this.s3 as any,
|
|
104
|
-
new GetObjectCommand({
|
|
105
|
-
Bucket: this.options.bucket,
|
|
106
|
-
Key: key,
|
|
107
|
-
...(objectConfig && { ...objectConfig }),
|
|
108
|
-
}),
|
|
109
|
-
);
|
|
110
|
-
return url;
|
|
111
|
-
}
|
|
112
|
-
return '';
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
async getFile(key: string): Promise<Buffer> {
|
|
116
|
-
if (!key) {
|
|
117
|
-
throw new Error('Key is required to fetch the file from S3.');
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
try {
|
|
121
|
-
const command = new GetObjectCommand({
|
|
122
|
-
Bucket: this.options.bucket,
|
|
123
|
-
Key: key,
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
const data: GetObjectCommandOutput = await this.s3.send(command);
|
|
127
|
-
|
|
128
|
-
if (!data.Body) {
|
|
129
|
-
throw new Error('Empty response received from S3.');
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// Handle both Buffer and Readable Stream responses
|
|
133
|
-
if (data.Body instanceof Readable) {
|
|
134
|
-
return await this.streamToBuffer(data.Body);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
throw new Error('Unexpected data.Body type received from S3.');
|
|
138
|
-
} catch (error) {
|
|
139
|
-
console.error(`Error fetching file (${key}) from S3:`, error);
|
|
140
|
-
throw error;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
async putFile(fileContent: Buffer, key: string): Promise<any> {
|
|
146
|
-
try {
|
|
147
|
-
const fileName = basename(key);
|
|
148
|
-
const fileKey = key || (Math.random() + 1).toString(36).substring(12);
|
|
149
|
-
|
|
150
|
-
// Upload the file
|
|
151
|
-
const putParams = {
|
|
152
|
-
Bucket: this.options.bucket,
|
|
153
|
-
Key: fileKey,
|
|
154
|
-
Body: fileContent,
|
|
155
|
-
ContentDisposition: `inline; ${fileName}`,
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
await this.s3.send(new PutObjectCommand(putParams));
|
|
159
|
-
|
|
160
|
-
// Fetch file metadata to get size
|
|
161
|
-
const headParams = {
|
|
162
|
-
Bucket: this.options.bucket,
|
|
163
|
-
Key: fileKey,
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
const headObject = await this.s3.send(new HeadObjectCommand(headParams));
|
|
167
|
-
const fileSize = headObject.ContentLength || 0;
|
|
168
|
-
|
|
169
|
-
// Construct response object
|
|
170
|
-
const fileData: UploadedFile = {
|
|
171
|
-
originalName: fileName,
|
|
172
|
-
fileName: fileName,
|
|
173
|
-
size: fileSize,
|
|
174
|
-
buffer: fileContent,
|
|
175
|
-
fullPath: fileKey,
|
|
176
|
-
key: fileKey,
|
|
177
|
-
url: this.getUrl(fileKey),
|
|
178
|
-
};
|
|
179
|
-
return fileData;
|
|
180
|
-
} catch (error) {
|
|
181
|
-
console.error('Error uploading file to S3:', error);
|
|
182
|
-
throw error;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
async deleteFile(key: string): Promise<void> {
|
|
188
|
-
if (!key) {
|
|
189
|
-
throw new Error('File key is required for deletion.');
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
try {
|
|
193
|
-
const deleteParams = {
|
|
194
|
-
Bucket: this.options.bucket,
|
|
195
|
-
Key: key,
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
await this.s3.send(new DeleteObjectCommand(deleteParams));
|
|
199
|
-
} catch (error) {
|
|
200
|
-
console.error(`Error deleting file (${key}) from S3:`, error);
|
|
201
|
-
throw error;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
private async streamToBuffer(stream: Readable): Promise<Buffer> {
|
|
207
|
-
const chunks: Uint8Array[] = [];
|
|
208
|
-
for await (const chunk of stream) {
|
|
209
|
-
chunks.push(chunk);
|
|
210
|
-
}
|
|
211
|
-
return Buffer.concat(chunks);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
async copyFile(oldKey: string, newKey: string): Promise<UploadedFile> {
|
|
215
|
-
try {
|
|
216
|
-
await this.s3.send(new CopyObjectCommand({
|
|
217
|
-
Bucket: this.options.bucket,
|
|
218
|
-
CopySource: `/${this.options.bucket}/${oldKey}`,
|
|
219
|
-
Key: newKey,
|
|
220
|
-
}));
|
|
221
|
-
|
|
222
|
-
const headObject = await this.s3.send(new HeadObjectCommand({
|
|
223
|
-
Bucket: this.options.bucket,
|
|
224
|
-
Key: newKey,
|
|
225
|
-
}));
|
|
226
|
-
|
|
227
|
-
return {
|
|
228
|
-
originalName: basename(newKey),
|
|
229
|
-
size: headObject.ContentLength || 0,
|
|
230
|
-
fileName: basename(newKey),
|
|
231
|
-
key: newKey,
|
|
232
|
-
fullPath: newKey,
|
|
233
|
-
url: this.getUrl(newKey),
|
|
234
|
-
};
|
|
235
|
-
} catch (error) {
|
|
236
|
-
console.error('Error copying file in S3:', error);
|
|
237
|
-
throw error;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { AzureStorageOptions, FileStorageEnum, LocalStorageOptions, S3StorageOptions, StorageOptions } from './types';
|
|
2
|
-
import { StorageEngine } from 'multer';
|
|
3
|
-
import { Storage } from './types';
|
|
4
|
-
|
|
5
|
-
export class StorageFactory {
|
|
6
|
-
private static storageInstances = new Map<string, StorageEngine & Storage>();
|
|
7
|
-
|
|
8
|
-
static async createStorage(storageType: FileStorageEnum, options: StorageOptions): Promise<StorageEngine & Storage> {
|
|
9
|
-
const cacheKey = `${storageType}-${JSON.stringify(options)}`;
|
|
10
|
-
|
|
11
|
-
// if (this.storageInstances.has(cacheKey)) {
|
|
12
|
-
// return this.storageInstances.get(cacheKey)!;
|
|
13
|
-
// }
|
|
14
|
-
|
|
15
|
-
let storageInstance: StorageEngine & Storage;
|
|
16
|
-
|
|
17
|
-
switch (storageType) {
|
|
18
|
-
case FileStorageEnum.LOCAL:
|
|
19
|
-
const { LocalStorage } = await import('./storage/local.storage');
|
|
20
|
-
storageInstance = new LocalStorage(options as LocalStorageOptions);
|
|
21
|
-
break;
|
|
22
|
-
|
|
23
|
-
case FileStorageEnum.AZURE:
|
|
24
|
-
try {
|
|
25
|
-
const { AzureStorage } = await import('./storage/azure.storage');
|
|
26
|
-
storageInstance = new AzureStorage(options as AzureStorageOptions);
|
|
27
|
-
} catch (error) {
|
|
28
|
-
throw new Error(
|
|
29
|
-
'Azure Storage SDK (@azure/storage-blob) is required when using AzureStorage. ' +
|
|
30
|
-
'Please install it: npm install @azure/storage-blob'
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
break;
|
|
34
|
-
|
|
35
|
-
case FileStorageEnum.S3:
|
|
36
|
-
try {
|
|
37
|
-
const { S3Storage } = await import('./storage/s3.storage');
|
|
38
|
-
storageInstance = new S3Storage(options as S3StorageOptions);
|
|
39
|
-
} catch (error) {
|
|
40
|
-
throw new Error(
|
|
41
|
-
'AWS SDK (@aws-sdk/client-s3 and @aws-sdk/s3-request-presigner) is required when using S3Storage. ' +
|
|
42
|
-
'Please install them: npm install @aws-sdk/client-s3 @aws-sdk/s3-request-presigner'
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
break;
|
|
46
|
-
|
|
47
|
-
default:
|
|
48
|
-
throw new Error(`Unsupported storage type: ${storageType}`);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
this.storageInstances.set(cacheKey, storageInstance);
|
|
52
|
-
return storageInstance;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
static clearCache() {
|
|
56
|
-
this.storageInstances.clear();
|
|
57
|
-
}
|
|
58
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.base.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"module": "commonjs",
|
|
5
|
-
"forceConsistentCasingInFileNames": true,
|
|
6
|
-
},
|
|
7
|
-
"files": [],
|
|
8
|
-
"include": [],
|
|
9
|
-
"references": [
|
|
10
|
-
{
|
|
11
|
-
"path": "./tsconfig.lib.json"
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
"path": "./tsconfig.spec.json"
|
|
15
|
-
}
|
|
16
|
-
]
|
|
17
|
-
}
|
package/tsconfig.lib.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "../../dist/out-tsc",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"types": ["node"],
|
|
7
|
-
"target": "es2021",
|
|
8
|
-
"experimentalDecorators": true,
|
|
9
|
-
"emitDecoratorMetadata": true,
|
|
10
|
-
"forceConsistentCasingInFileNames": true
|
|
11
|
-
},
|
|
12
|
-
"include": ["src/**/*.ts"],
|
|
13
|
-
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
|
|
14
|
-
}
|
package/tsconfig.spec.json
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "../../dist/out-tsc",
|
|
5
|
-
"module": "commonjs",
|
|
6
|
-
"moduleResolution": "node10",
|
|
7
|
-
"types": ["jest", "node"]
|
|
8
|
-
},
|
|
9
|
-
"include": [
|
|
10
|
-
"jest.config.ts",
|
|
11
|
-
"src/**/*.test.ts",
|
|
12
|
-
"src/**/*.spec.ts",
|
|
13
|
-
"src/**/*.d.ts"
|
|
14
|
-
]
|
|
15
|
-
}
|