@ackplus/nest-file-storage 0.0.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.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +409 -0
  3. package/dist/index.d.ts +5 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +21 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/lib/constants.d.ts +2 -0
  8. package/dist/lib/constants.d.ts.map +1 -0
  9. package/dist/lib/constants.js +5 -0
  10. package/dist/lib/constants.js.map +1 -0
  11. package/dist/lib/file-storage.service.d.ts +8 -0
  12. package/dist/lib/file-storage.service.d.ts.map +1 -0
  13. package/dist/lib/file-storage.service.js +30 -0
  14. package/dist/lib/file-storage.service.js.map +1 -0
  15. package/dist/lib/index.d.ts +6 -0
  16. package/dist/lib/index.d.ts.map +1 -0
  17. package/dist/lib/index.js +22 -0
  18. package/dist/lib/index.js.map +1 -0
  19. package/dist/lib/interceptor/file-storage.interceptor.d.ts +25 -0
  20. package/dist/lib/interceptor/file-storage.interceptor.d.ts.map +1 -0
  21. package/dist/lib/interceptor/file-storage.interceptor.js +141 -0
  22. package/dist/lib/interceptor/file-storage.interceptor.js.map +1 -0
  23. package/dist/lib/nest-file-storage.module.d.ts +9 -0
  24. package/dist/lib/nest-file-storage.module.d.ts.map +1 -0
  25. package/dist/lib/nest-file-storage.module.js +80 -0
  26. package/dist/lib/nest-file-storage.module.js.map +1 -0
  27. package/dist/lib/storage/azure.storage.d.ts +19 -0
  28. package/dist/lib/storage/azure.storage.d.ts.map +1 -0
  29. package/dist/lib/storage/azure.storage.js +189 -0
  30. package/dist/lib/storage/azure.storage.js.map +1 -0
  31. package/dist/lib/storage/local.storage.d.ts +35 -0
  32. package/dist/lib/storage/local.storage.d.ts.map +1 -0
  33. package/dist/lib/storage/local.storage.js +219 -0
  34. package/dist/lib/storage/local.storage.js.map +1 -0
  35. package/dist/lib/storage/s3.storage.d.ts +20 -0
  36. package/dist/lib/storage/s3.storage.d.ts.map +1 -0
  37. package/dist/lib/storage/s3.storage.js +231 -0
  38. package/dist/lib/storage/s3.storage.js.map +1 -0
  39. package/dist/lib/storage.factory.d.ts +9 -0
  40. package/dist/lib/storage.factory.d.ts.map +1 -0
  41. package/dist/lib/storage.factory.js +82 -0
  42. package/dist/lib/storage.factory.js.map +1 -0
  43. package/dist/lib/types.d.ts +91 -0
  44. package/dist/lib/types.d.ts.map +1 -0
  45. package/dist/lib/types.js +10 -0
  46. package/dist/lib/types.js.map +1 -0
  47. package/package.json +67 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Ack Solutions
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,409 @@
1
+ # @ackplus/nest-file-storage
2
+
3
+ A flexible file storage library for NestJS that supports multiple storage providers with simple file mapping capabilities.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @ackplus/nest-file-storage
9
+ ```
10
+
11
+ ## Storage Providers
12
+
13
+ This library supports multiple storage providers. You only need to install the dependencies for the storage providers you plan to use.
14
+
15
+ ### Local Storage
16
+
17
+ No additional dependencies required. Works out of the box.
18
+
19
+ ### Azure Blob Storage
20
+
21
+ To use Azure Blob Storage, install the Azure SDK:
22
+
23
+ ```bash
24
+ npm install @azure/storage-blob
25
+ ```
26
+
27
+ ### AWS S3 Storage
28
+
29
+ To use AWS S3 Storage, install the AWS SDK:
30
+
31
+ ```bash
32
+ npm install @aws-sdk/client-s3 @aws-sdk/s3-request-presigner
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ The library supports two approaches: **Configuration-based** and **Class Factory**.
38
+
39
+ ### Configuration-based Approach (Recommended)
40
+
41
+ This approach uses lazy loading and only loads the storage provider when needed.
42
+
43
+ #### Local Storage
44
+
45
+ ```typescript
46
+ import { NestFileStorageModule, FileStorageEnum } from '@ackplus/nest-file-storage';
47
+
48
+ @Module({
49
+ imports: [
50
+ NestFileStorageModule.forRoot({
51
+ storage: FileStorageEnum.LOCAL,
52
+ localConfig: {
53
+ destination: './uploads'
54
+ }
55
+ })
56
+ ]
57
+ })
58
+ export class AppModule {}
59
+ ```
60
+
61
+ #### Azure Blob Storage
62
+
63
+ ```typescript
64
+ import { NestFileStorageModule, FileStorageEnum } from '@ackplus/nest-file-storage';
65
+
66
+ @Module({
67
+ imports: [
68
+ NestFileStorageModule.forRoot({
69
+ storage: FileStorageEnum.AZURE,
70
+ azureConfig: {
71
+ account: 'your-storage-account',
72
+ accountKey: 'your-account-key',
73
+ container: 'your-container'
74
+ }
75
+ })
76
+ ]
77
+ })
78
+ export class AppModule {}
79
+ ```
80
+
81
+ #### AWS S3 Storage
82
+
83
+ ```typescript
84
+ import { NestFileStorageModule, FileStorageEnum } from '@ackplus/nest-file-storage';
85
+
86
+ @Module({
87
+ imports: [
88
+ NestFileStorageModule.forRoot({
89
+ storage: FileStorageEnum.S3,
90
+ s3Config: {
91
+ region: 'us-east-1',
92
+ bucket: 'your-bucket',
93
+ accessKeyId: 'your-access-key',
94
+ secretAccessKey: 'your-secret-key'
95
+ }
96
+ })
97
+ ]
98
+ })
99
+ export class AppModule {}
100
+ ```
101
+
102
+ ### Class Factory Approach
103
+
104
+ This approach allows you to provide your own storage class directly.
105
+
106
+ ```typescript
107
+ import { NestFileStorageModule } from '@ackplus/nest-file-storage';
108
+
109
+ @Module({
110
+ imports: [
111
+ NestFileStorageModule.forRoot({
112
+ storageFactory: async () => {
113
+ // Lazy load the storage class
114
+ const { AzureStorage } = await import('@ackplus/nest-file-storage/lib/storage/azure.storage');
115
+ return AzureStorage;
116
+ },
117
+ options: {
118
+ account: 'your-storage-account',
119
+ accountKey: 'your-account-key',
120
+ container: 'your-container'
121
+ }
122
+ })
123
+ ]
124
+ })
125
+ export class AppModule {}
126
+ ```
127
+
128
+ #### Custom Storage Class
129
+
130
+ You can also provide your own custom storage implementation:
131
+
132
+ ```typescript
133
+ import { NestFileStorageModule, Storage } from '@ackplus/nest-file-storage';
134
+
135
+ class CustomStorage implements Storage {
136
+ // Implement your custom storage logic
137
+ async getFile(key: string): Promise<Buffer> { /* ... */ }
138
+ async deleteFile(key: string): Promise<void> { /* ... */ }
139
+ async putFile(fileContent: Buffer, key: string): Promise<UploadedFile> { /* ... */ }
140
+ getUrl(key: string): string { /* ... */ }
141
+ async copyFile(oldKey: string, newKey: string): Promise<UploadedFile> { /* ... */ }
142
+ }
143
+
144
+ @Module({
145
+ imports: [
146
+ NestFileStorageModule.forRoot({
147
+ storageFactory: () => CustomStorage,
148
+ options: {
149
+ // Your custom options
150
+ }
151
+ })
152
+ ]
153
+ })
154
+ export class AppModule {}
155
+ ```
156
+
157
+ ### When to Use Each Approach
158
+
159
+ **Configuration-based Approach:**
160
+ - ✅ Recommended for most use cases
161
+ - ✅ Automatic lazy loading
162
+ - ✅ Built-in error handling for missing dependencies
163
+ - ✅ Simpler configuration
164
+ - ✅ Better for switching between providers
165
+ - ✅ Supports async configuration with dependency injection
166
+
167
+ **Class Factory Approach:**
168
+ - ✅ More control over storage instantiation
169
+ - ✅ Custom storage implementations
170
+ - ✅ Advanced use cases
171
+ - ✅ Still supports lazy loading if implemented correctly
172
+ - ⚠️ More complex configuration
173
+ - ⚠️ Manual dependency management
174
+
175
+ **Async Configuration (forRootAsync):**
176
+ - ✅ Full dependency injection support
177
+ - ✅ `useFactory`, `useClass`, and `useExisting` patterns
178
+ - ✅ Integration with ConfigService and other providers
179
+ - ✅ Environment-based configuration
180
+ - ✅ Perfect for complex application setups
181
+
182
+ ### Async Configuration (forRootAsync)
183
+
184
+ For more advanced scenarios, you can use `forRootAsync` with dependency injection:
185
+
186
+ #### Using useFactory
187
+
188
+ ```typescript
189
+ import { NestFileStorageModule, FileStorageEnum } from '@ackplus/nest-file-storage';
190
+ import { ConfigModule, ConfigService } from '@nestjs/config';
191
+
192
+ @Module({
193
+ imports: [
194
+ ConfigModule,
195
+ NestFileStorageModule.forRootAsync({
196
+ imports: [ConfigModule],
197
+ useFactory: async (configService: ConfigService) => ({
198
+ storage: FileStorageEnum.S3,
199
+ s3Config: {
200
+ region: configService.get('AWS_REGION'),
201
+ bucket: configService.get('AWS_BUCKET'),
202
+ accessKeyId: configService.get('AWS_ACCESS_KEY_ID'),
203
+ secretAccessKey: configService.get('AWS_SECRET_ACCESS_KEY'),
204
+ }
205
+ }),
206
+ inject: [ConfigService],
207
+ })
208
+ ]
209
+ })
210
+ export class AppModule {}
211
+ ```
212
+
213
+ #### Using useClass
214
+
215
+ ```typescript
216
+ import { Injectable } from '@nestjs/common';
217
+ import { NestFileStorageModule, FileStorageOptionsFactory, FileStorageModuleOptions, FileStorageEnum } from '@ackplus/nest-file-storage';
218
+ import { ConfigService } from '@nestjs/config';
219
+
220
+ @Injectable()
221
+ export class FileStorageConfigService implements FileStorageOptionsFactory {
222
+ constructor(private configService: ConfigService) {}
223
+
224
+ createFileStorageOptions(): FileStorageModuleOptions {
225
+ return {
226
+ storage: FileStorageEnum.AZURE,
227
+ azureConfig: {
228
+ account: this.configService.get('AZURE_STORAGE_ACCOUNT'),
229
+ accountKey: this.configService.get('AZURE_STORAGE_KEY'),
230
+ container: this.configService.get('AZURE_STORAGE_CONTAINER'),
231
+ }
232
+ };
233
+ }
234
+ }
235
+
236
+ @Module({
237
+ imports: [
238
+ ConfigModule,
239
+ NestFileStorageModule.forRootAsync({
240
+ imports: [ConfigModule],
241
+ useClass: FileStorageConfigService,
242
+ })
243
+ ]
244
+ })
245
+ export class AppModule {}
246
+ ```
247
+
248
+ #### Using useExisting
249
+
250
+ ```typescript
251
+ @Module({
252
+ imports: [
253
+ ConfigModule,
254
+ NestFileStorageModule.forRootAsync({
255
+ imports: [ConfigModule],
256
+ useExisting: FileStorageConfigService,
257
+ })
258
+ ],
259
+ providers: [FileStorageConfigService]
260
+ })
261
+ export class AppModule {}
262
+ ```
263
+
264
+ ### Direct Usage with Storage Factory
265
+
266
+ You can also use the storage factory directly for more control:
267
+
268
+ ```typescript
269
+ import { StorageFactory, FileStorageEnum } from '@ackplus/nest-file-storage';
270
+
271
+ // This will lazy load only the Azure storage provider
272
+ const azureStorage = await StorageFactory.createStorage(FileStorageEnum.AZURE, {
273
+ account: 'your-storage-account',
274
+ accountKey: 'your-account-key',
275
+ container: 'your-container'
276
+ });
277
+
278
+ // Use the storage instance
279
+ const uploadedFile = await azureStorage.putFile(buffer, 'path/to/file.jpg');
280
+ ```
281
+
282
+ ## Flexible File Mapping
283
+
284
+ The library includes flexible file mapping functionality that allows you to define custom logic for how uploaded files are transformed and stored in the request body.
285
+
286
+ **Default Behavior**: Without any configuration, uploaded files are automatically mapped to their storage key (path) in the request body.
287
+
288
+ ### Default Behavior (No Configuration)
289
+
290
+ ```typescript
291
+ import { FileStorageInterceptor } from '@ackplus/nest-file-storage';
292
+
293
+ @Controller('upload')
294
+ export class UploadController {
295
+ @Post('document')
296
+ @UseInterceptors(FileStorageInterceptor('document'))
297
+ async uploadDocument(@Body() body: any) {
298
+ // body.document will contain the file key (storage path)
299
+ console.log(body.document); // "documents/2024/1/1234567890-resume.pdf"
300
+ return { message: 'Document uploaded', data: body };
301
+ }
302
+ }
303
+ ```
304
+
305
+ ### Return File URL String (Custom Logic)
306
+
307
+ ```typescript
308
+ @Post('avatar')
309
+ @UseInterceptors(FileStorageInterceptor('avatar', {
310
+ mapToRequestBody: (file, fieldName, req) => {
311
+ // Return just the URL string for direct DB storage
312
+ return file.url;
313
+ }
314
+ }))
315
+ async uploadAvatar(@Body() body: any) {
316
+ // body.avatar will be a string URL
317
+ console.log(body.avatar); // "http://localhost:3000/uploads/images/avatar.jpg"
318
+ return { message: 'Avatar uploaded', data: body };
319
+ }
320
+ ```
321
+
322
+ ### Return Custom Object with Business Logic
323
+
324
+ ```typescript
325
+ @Post('document')
326
+ @UseInterceptors(FileStorageInterceptor('document', {
327
+ mapToRequestBody: (file, fieldName, req) => {
328
+ // Return custom object with business logic
329
+ return {
330
+ fileId: `file_${Date.now()}`,
331
+ url: file.url,
332
+ originalName: file.originalName,
333
+ uploadedBy: req.user?.id || 'anonymous',
334
+ uploadedAt: new Date().toISOString(),
335
+ isPublic: req.body.isPublic === 'true'
336
+ };
337
+ }
338
+ }))
339
+ async uploadDocument(@Body() body: any) {
340
+ // body.document will be a custom object
341
+ console.log(body.document);
342
+ return { message: 'Document uploaded', data: body };
343
+ }
344
+ ```
345
+
346
+ ### Multiple Files with Custom Logic
347
+
348
+ ```typescript
349
+ @Post('gallery')
350
+ @UseInterceptors(FileStorageInterceptor({
351
+ type: 'array',
352
+ fieldName: 'images',
353
+ maxCount: 5
354
+ }, {
355
+ mapToRequestBody: (files, fieldName, req) => {
356
+ // Return array of custom objects
357
+ return files.map((file, index) => ({
358
+ id: `img_${Date.now()}_${index}`,
359
+ url: file.url,
360
+ isPrimary: index === 0,
361
+ alt: req.body.imageAlts?.[index] || `Image ${index + 1}`
362
+ }));
363
+ }
364
+ }))
365
+ async uploadGallery(@Body() body: any) {
366
+ // body.images will be an array of custom objects
367
+ console.log(body.images);
368
+ return { message: 'Gallery uploaded', data: body };
369
+ }
370
+ ```
371
+
372
+ **Key Features**:
373
+ - **Return anything**: String, object, array, or custom structure
374
+ - **Access request data**: Use `req` parameter for user info, body data, etc.
375
+ - **Field-specific logic**: Use `fieldName` to handle different fields differently
376
+ - **Business logic**: Implement any custom transformation you need
377
+
378
+ For detailed examples, see [FILE_KEY_MAPPING.md](./docs/FILE_KEY_MAPPING.md).
379
+
380
+ ## Error Handling
381
+
382
+ If you try to use a storage provider without installing its required dependencies, you'll get a clear error message:
383
+
384
+ - **Azure Storage**: `Azure Storage SDK (@azure/storage-blob) is required when using AzureStorage. Please install it: npm install @azure/storage-blob`
385
+ - **S3 Storage**: `AWS SDK (@aws-sdk/client-s3 and @aws-sdk/s3-request-presigner) is required when using S3Storage. Please install them: npm install @aws-sdk/client-s3 @aws-sdk/s3-request-presigner`
386
+
387
+ ## How Lazy Loading Works
388
+
389
+ The library uses dynamic imports to load storage providers only when they're actually needed:
390
+
391
+ 1. **At build time**: Only the storage factory and types are bundled
392
+ 2. **At runtime**: When you use a storage provider, it's dynamically imported
393
+ 3. **Caching**: Once loaded, storage instances are cached for reuse
394
+ 4. **Error handling**: Missing dependencies are caught and provide clear error messages
395
+
396
+ This means:
397
+ - If you only use local storage, Azure/S3 code is never loaded
398
+ - If you only use Azure storage, S3 code is never loaded
399
+ - Your bundle size stays minimal regardless of how many storage providers are available
400
+
401
+ ## Benefits
402
+
403
+ - **Lazy Loading**: Storage providers are only loaded when actually used
404
+ - **Smaller bundle size**: Only install the dependencies you need
405
+ - **Flexible**: Switch between storage providers easily
406
+ - **Clear error messages**: Know exactly what to install if a dependency is missing
407
+ - **TypeScript support**: Full type safety for all storage providers
408
+ - **Caching**: Storage instances are cached to avoid repeated initialization
409
+ - **Zero overhead**: Unused storage providers don't affect your application
@@ -0,0 +1,5 @@
1
+ export * from './lib/nest-file-storage.module';
2
+ export * from './lib/file-storage.service';
3
+ export * from './lib/interceptor/file-storage.interceptor';
4
+ export * from './lib/types';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4CAA4C,CAAC;AAC3D,cAAc,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,21 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./lib/nest-file-storage.module"), exports);
18
+ __exportStar(require("./lib/file-storage.service"), exports);
19
+ __exportStar(require("./lib/interceptor/file-storage.interceptor"), exports);
20
+ __exportStar(require("./lib/types"), exports);
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iEAA+C;AAC/C,6DAA2C;AAC3C,6EAA2D;AAC3D,8CAA4B"}
@@ -0,0 +1,2 @@
1
+ export declare const FILE_STORAGE_OPTIONS = "FileStorageOptions";
2
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/lib/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,uBAAuB,CAAC"}
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FILE_STORAGE_OPTIONS = void 0;
4
+ exports.FILE_STORAGE_OPTIONS = 'FileStorageOptions';
5
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/lib/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,oBAAoB,GAAG,oBAAoB,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { FileStorageEnum, FileStorageModuleOptions } from './types';
2
+ export declare class FileStorageService {
3
+ private static options;
4
+ static setOptions(options: FileStorageModuleOptions): void;
5
+ static getOptions(): FileStorageModuleOptions;
6
+ static getStorage(storageType?: FileStorageEnum): Promise<import("./types").Storage>;
7
+ }
8
+ //# sourceMappingURL=file-storage.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-storage.service.d.ts","sourceRoot":"","sources":["../../src/lib/file-storage.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,wBAAwB,EAAqD,MAAM,SAAS,CAAC;AAGvH,qBAAa,kBAAkB;IAE3B,OAAO,CAAC,MAAM,CAAC,OAAO,CAA2B;IAEjD,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,wBAAwB;IAInD,MAAM,CAAC,UAAU,IAAI,wBAAwB;WAIhC,UAAU,CAAC,WAAW,CAAC,EAAE,eAAe;CAmBxD"}
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FileStorageService = void 0;
4
+ const storage_factory_1 = require("./storage.factory");
5
+ class FileStorageService {
6
+ static setOptions(options) {
7
+ FileStorageService.options = options;
8
+ }
9
+ static getOptions() {
10
+ return FileStorageService.options;
11
+ }
12
+ static async getStorage(storageType) {
13
+ const options = this.getOptions();
14
+ // Check if it's a class factory approach
15
+ if ('storageFactory' in options) {
16
+ const classOptions = options;
17
+ const StorageClass = await classOptions.storageFactory();
18
+ return new StorageClass(classOptions.options);
19
+ }
20
+ // Configuration-based approach
21
+ const configOptions = options;
22
+ if (!storageType) {
23
+ storageType = configOptions.storage;
24
+ }
25
+ const config = configOptions[`${storageType}Config`];
26
+ return await storage_factory_1.StorageFactory.createStorage(storageType, config);
27
+ }
28
+ }
29
+ exports.FileStorageService = FileStorageService;
30
+ //# sourceMappingURL=file-storage.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-storage.service.js","sourceRoot":"","sources":["../../src/lib/file-storage.service.ts"],"names":[],"mappings":";;;AAAA,uDAAmD;AAInD,MAAa,kBAAkB;IAI3B,MAAM,CAAC,UAAU,CAAC,OAAiC;QAC/C,kBAAkB,CAAC,OAAO,GAAG,OAAO,CAAC;IACzC,CAAC;IAED,MAAM,CAAC,UAAU;QACb,OAAO,kBAAkB,CAAC,OAAO,CAAC;IACtC,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,WAA6B;QACjD,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC;QAElC,yCAAyC;QACzC,IAAI,gBAAgB,IAAI,OAAO,EAAE,CAAC;YAC9B,MAAM,YAAY,GAAG,OAAkC,CAAC;YACxD,MAAM,YAAY,GAAG,MAAM,YAAY,CAAC,cAAc,EAAE,CAAC;YACzD,OAAO,IAAI,YAAY,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;QAClD,CAAC;QAED,+BAA+B;QAC/B,MAAM,aAAa,GAAG,OAAmC,CAAC;QAC1D,IAAI,CAAC,WAAW,EAAE,CAAC;YACf,WAAW,GAAG,aAAa,CAAC,OAAO,CAAC;QACxC,CAAC;QACD,MAAM,MAAM,GAAG,aAAa,CAAC,GAAG,WAAW,QAAQ,CAAC,CAAC;QACrD,OAAO,MAAM,gCAAc,CAAC,aAAa,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;IACnE,CAAC;CAEJ;AA/BD,gDA+BC"}
@@ -0,0 +1,6 @@
1
+ export * from './nest-file-storage.module';
2
+ export * from './types';
3
+ export * from './storage.factory';
4
+ export * from './interceptor/file-storage.interceptor';
5
+ export * from './file-storage.service';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,SAAS,CAAC;AACxB,cAAc,mBAAmB,CAAC;AAClC,cAAc,wCAAwC,CAAC;AACvD,cAAc,wBAAwB,CAAC"}
@@ -0,0 +1,22 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./nest-file-storage.module"), exports);
18
+ __exportStar(require("./types"), exports);
19
+ __exportStar(require("./storage.factory"), exports);
20
+ __exportStar(require("./interceptor/file-storage.interceptor"), exports);
21
+ __exportStar(require("./file-storage.service"), exports);
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6DAA2C;AAC3C,0CAAwB;AACxB,oDAAkC;AAClC,yEAAuD;AACvD,yDAAuC"}
@@ -0,0 +1,25 @@
1
+ import { NestInterceptor } from '@nestjs/common';
2
+ import { Request } from 'express';
3
+ import { FileStorageEnum, StorageOptions } from '../types';
4
+ export type FileUploadConfig = {
5
+ type: 'single' | 'array' | 'fields';
6
+ fieldName?: string;
7
+ maxCount?: number;
8
+ fields?: {
9
+ name: string;
10
+ maxCount?: number;
11
+ }[];
12
+ };
13
+ export type FileStorageInterceptorOptions = {
14
+ fileName?: (file: any, req?: Request) => string;
15
+ fileDist?: (file: any, req?: Request) => string;
16
+ prefix?: string;
17
+ storageType?: FileStorageEnum;
18
+ storageOptions?: StorageOptions;
19
+ mapToRequestBody?: (file: any, fieldName: string, req?: Request) => any;
20
+ };
21
+ /**
22
+ * Function-based interceptor that accepts storage options dynamically.
23
+ */
24
+ export declare function FileStorageInterceptor(fileConfig: FileUploadConfig | string, interceptorOptions?: FileStorageInterceptorOptions): NestInterceptor;
25
+ //# sourceMappingURL=file-storage.interceptor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-storage.interceptor.d.ts","sourceRoot":"","sources":["../../../src/lib/interceptor/file-storage.interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAiC,MAAM,gBAAgB,CAAC;AAChF,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAMlC,OAAO,EAAE,eAAe,EAAE,cAAc,EAA0C,MAAM,UAAU,CAAC;AAGnG,MAAM,MAAM,gBAAgB,GAAG;IAC3B,IAAI,EAAE,QAAQ,GAAG,OAAO,GAAG,QAAQ,CAAC;IACpC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CAClD,CAAC;AAEF,MAAM,MAAM,6BAA6B,GAAG;IACxC,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,OAAO,KAAK,MAAM,CAAC;IAChD,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,OAAO,KAAK,MAAM,CAAC;IAChD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,eAAe,CAAC;IAC9B,cAAc,CAAC,EAAE,cAAc,CAAC;IAGhC,gBAAgB,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,KAAK,GAAG,CAAC;CAC3E,CAAC;AA2DF;;GAEG;AACH,wBAAgB,sBAAsB,CAClC,UAAU,EAAE,gBAAgB,GAAG,MAAM,EACrC,kBAAkB,CAAC,EAAE,6BAA6B,GACnD,eAAe,CAkFjB"}