@flusys/nestjs-storage 6.0.1 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/controllers/file-manager.controller.spec.js +135 -0
- package/cjs/controllers/folder.controller.spec.js +86 -0
- package/cjs/controllers/storage-config.controller.spec.js +85 -0
- package/cjs/controllers/upload.controller.spec.js +132 -0
- package/cjs/docs/storage-swagger.config.spec.js +72 -0
- package/cjs/entities/index.spec.js +18 -0
- package/cjs/middlewares/file-serve.middleware.spec.js +211 -0
- package/cjs/modules/storage.module.spec.js +184 -0
- package/cjs/providers/azure-provider.optional.spec.js +228 -0
- package/cjs/providers/local-provider.spec.js +239 -0
- package/cjs/providers/s3-provider.optional.spec.js +243 -0
- package/cjs/providers/sftp-provider.optional.spec.js +217 -0
- package/cjs/providers/storage-factory.service.spec.js +253 -0
- package/cjs/providers/storage-provider.registry.spec.js +67 -0
- package/cjs/services/file-manager.service.spec.js +549 -0
- package/cjs/services/folder.service.spec.js +163 -0
- package/cjs/services/storage-config.service.spec.js +257 -0
- package/cjs/services/storage-datasource.provider.spec.js +217 -0
- package/cjs/services/storage-provider-config.service.spec.js +243 -0
- package/cjs/services/upload.service.spec.js +372 -0
- package/cjs/utils/file-validator.util.spec.js +259 -0
- package/cjs/utils/image-compressor.util.spec.js +166 -0
- package/fesm/controllers/file-manager.controller.spec.js +131 -0
- package/fesm/controllers/folder.controller.spec.js +82 -0
- package/fesm/controllers/storage-config.controller.spec.js +81 -0
- package/fesm/controllers/upload.controller.spec.js +128 -0
- package/fesm/docs/storage-swagger.config.spec.js +68 -0
- package/fesm/entities/index.spec.js +14 -0
- package/fesm/middlewares/file-serve.middleware.spec.js +207 -0
- package/fesm/modules/storage.module.spec.js +180 -0
- package/fesm/providers/azure-provider.optional.spec.js +224 -0
- package/fesm/providers/local-provider.spec.js +194 -0
- package/fesm/providers/s3-provider.optional.spec.js +239 -0
- package/fesm/providers/sftp-provider.optional.spec.js +213 -0
- package/fesm/providers/storage-factory.service.spec.js +249 -0
- package/fesm/providers/storage-provider.registry.spec.js +63 -0
- package/fesm/services/file-manager.service.spec.js +545 -0
- package/fesm/services/folder.service.spec.js +159 -0
- package/fesm/services/storage-config.service.spec.js +253 -0
- package/fesm/services/storage-datasource.provider.spec.js +172 -0
- package/fesm/services/storage-provider-config.service.spec.js +239 -0
- package/fesm/services/upload.service.spec.js +368 -0
- package/fesm/utils/file-validator.util.spec.js +255 -0
- package/fesm/utils/image-compressor.util.spec.js +162 -0
- package/package.json +3 -3
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
import { Test } from '@nestjs/testing';
|
|
2
|
+
import { UtilsService } from '@flusys/nestjs-shared/modules';
|
|
3
|
+
import { createMockRepository } from '@test-utils/mocks/repository.mock';
|
|
4
|
+
import { buildMockUser } from '@test-utils/mocks/logged-user.mock';
|
|
5
|
+
import { FileManagerService } from './file-manager.service';
|
|
6
|
+
import { StorageConfigService } from './storage-config.service';
|
|
7
|
+
import { StorageDataSourceProvider } from './storage-datasource.provider';
|
|
8
|
+
import { UploadService } from './upload.service';
|
|
9
|
+
import { FileManager, FileManagerWithCompany, Folder, FolderWithCompany, StorageConfig, StorageConfigWithCompany } from '../entities';
|
|
10
|
+
import { FileLocationEnum } from '../enums/file-location.enum';
|
|
11
|
+
function buildFile(overrides = {}) {
|
|
12
|
+
return {
|
|
13
|
+
id: 'file-uuid-1',
|
|
14
|
+
name: 'photo.jpg',
|
|
15
|
+
contentType: 'image/jpeg',
|
|
16
|
+
size: '1024',
|
|
17
|
+
key: 'uploads/photo.jpg',
|
|
18
|
+
url: null,
|
|
19
|
+
location: FileLocationEnum.LOCAL,
|
|
20
|
+
storageConfigId: null,
|
|
21
|
+
expiresAt: null,
|
|
22
|
+
isPrivate: false,
|
|
23
|
+
folder: null,
|
|
24
|
+
createdAt: new Date(),
|
|
25
|
+
updatedAt: new Date(),
|
|
26
|
+
deletedAt: null,
|
|
27
|
+
createdById: null,
|
|
28
|
+
updatedById: null,
|
|
29
|
+
deletedById: null,
|
|
30
|
+
...overrides
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
describe('FileManagerService', ()=>{
|
|
34
|
+
let service;
|
|
35
|
+
let mockFileRepo;
|
|
36
|
+
let mockFolderRepo;
|
|
37
|
+
let mockStorageConfigRepo;
|
|
38
|
+
let mockStorageConfig;
|
|
39
|
+
let mockUploadService;
|
|
40
|
+
let mockCacheManager;
|
|
41
|
+
beforeEach(async ()=>{
|
|
42
|
+
mockFileRepo = createMockRepository();
|
|
43
|
+
mockFileRepo.findBy = jest.fn();
|
|
44
|
+
mockFolderRepo = createMockRepository();
|
|
45
|
+
mockStorageConfigRepo = createMockRepository();
|
|
46
|
+
const mockDataSourceProvider = {
|
|
47
|
+
getRepository: jest.fn((entity)=>{
|
|
48
|
+
if (entity === Folder || entity === FolderWithCompany) {
|
|
49
|
+
return Promise.resolve(mockFolderRepo);
|
|
50
|
+
}
|
|
51
|
+
if (entity === StorageConfig || entity === StorageConfigWithCompany) {
|
|
52
|
+
return Promise.resolve(mockStorageConfigRepo);
|
|
53
|
+
}
|
|
54
|
+
return Promise.resolve(mockFileRepo);
|
|
55
|
+
}),
|
|
56
|
+
getDataSource: jest.fn()
|
|
57
|
+
};
|
|
58
|
+
mockStorageConfig = {
|
|
59
|
+
isCompanyFeatureEnabled: jest.fn().mockReturnValue(false),
|
|
60
|
+
getAppUrl: jest.fn().mockReturnValue('http://localhost:2002')
|
|
61
|
+
};
|
|
62
|
+
mockUploadService = {
|
|
63
|
+
deleteMultipleFile: jest.fn().mockResolvedValue(true),
|
|
64
|
+
makeFileUrl: jest.fn().mockResolvedValue('https://signed.example.com/photo.jpg')
|
|
65
|
+
};
|
|
66
|
+
mockCacheManager = {
|
|
67
|
+
get: jest.fn(),
|
|
68
|
+
set: jest.fn()
|
|
69
|
+
};
|
|
70
|
+
const module = await Test.createTestingModule({
|
|
71
|
+
providers: [
|
|
72
|
+
FileManagerService,
|
|
73
|
+
{
|
|
74
|
+
provide: 'CACHE_INSTANCE',
|
|
75
|
+
useValue: mockCacheManager
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
provide: UtilsService,
|
|
79
|
+
useValue: {}
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
provide: UploadService,
|
|
83
|
+
useValue: mockUploadService
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
provide: StorageConfigService,
|
|
87
|
+
useValue: mockStorageConfig
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
provide: StorageDataSourceProvider,
|
|
91
|
+
useValue: mockDataSourceProvider
|
|
92
|
+
}
|
|
93
|
+
]
|
|
94
|
+
}).compile();
|
|
95
|
+
service = await module.resolve(FileManagerService);
|
|
96
|
+
await service.ensureDataSourceRepository();
|
|
97
|
+
});
|
|
98
|
+
describe('resolveEntity', ()=>{
|
|
99
|
+
it('resolves to the core FileManager entity when company feature is disabled', ()=>{
|
|
100
|
+
expect(service.resolveEntity()).toBe(FileManager);
|
|
101
|
+
});
|
|
102
|
+
it('resolves to FileManagerWithCompany when company feature is enabled', ()=>{
|
|
103
|
+
mockStorageConfig.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
104
|
+
expect(service.resolveEntity()).toBe(FileManagerWithCompany);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
describe('convertSingleDtoToEntity', ()=>{
|
|
108
|
+
it('creates a new entity with location LOCAL by default when no folder/storageConfig given', async ()=>{
|
|
109
|
+
const entity = await service.convertSingleDtoToEntity({
|
|
110
|
+
name: 'a.jpg',
|
|
111
|
+
key: 'k',
|
|
112
|
+
size: '10',
|
|
113
|
+
contentType: 'image/jpeg',
|
|
114
|
+
isPrivate: false
|
|
115
|
+
}, buildMockUser());
|
|
116
|
+
expect(entity.location).toBe(FileLocationEnum.LOCAL);
|
|
117
|
+
expect(entity.folder).toBeNull();
|
|
118
|
+
});
|
|
119
|
+
it('preserves an explicitly provided location when no storageConfigId is given', async ()=>{
|
|
120
|
+
const entity = await service.convertSingleDtoToEntity({
|
|
121
|
+
name: 'a.jpg',
|
|
122
|
+
key: 'k',
|
|
123
|
+
size: '10',
|
|
124
|
+
contentType: 'image/jpeg',
|
|
125
|
+
isPrivate: false,
|
|
126
|
+
location: FileLocationEnum.AWS
|
|
127
|
+
}, buildMockUser());
|
|
128
|
+
expect(entity.location).toBe(FileLocationEnum.AWS);
|
|
129
|
+
});
|
|
130
|
+
it('resolves the storage location from the referenced storage config', async ()=>{
|
|
131
|
+
mockStorageConfigRepo.findOne.mockResolvedValue({
|
|
132
|
+
id: 'cfg-1',
|
|
133
|
+
storage: 'aws'
|
|
134
|
+
});
|
|
135
|
+
const entity = await service.convertSingleDtoToEntity({
|
|
136
|
+
name: 'a.jpg',
|
|
137
|
+
key: 'k',
|
|
138
|
+
size: '10',
|
|
139
|
+
contentType: 'image/jpeg',
|
|
140
|
+
isPrivate: false,
|
|
141
|
+
storageConfigId: 'cfg-1'
|
|
142
|
+
}, buildMockUser());
|
|
143
|
+
expect(entity.location).toBe('aws');
|
|
144
|
+
});
|
|
145
|
+
it('falls back to dto.location when the storage config lookup throws', async ()=>{
|
|
146
|
+
mockStorageConfigRepo.findOne.mockRejectedValue(new Error('db down'));
|
|
147
|
+
const entity = await service.convertSingleDtoToEntity({
|
|
148
|
+
name: 'a.jpg',
|
|
149
|
+
key: 'k',
|
|
150
|
+
size: '10',
|
|
151
|
+
contentType: 'image/jpeg',
|
|
152
|
+
isPrivate: false,
|
|
153
|
+
storageConfigId: 'cfg-1',
|
|
154
|
+
location: FileLocationEnum.AZURE
|
|
155
|
+
}, buildMockUser());
|
|
156
|
+
expect(entity.location).toBe(FileLocationEnum.AZURE);
|
|
157
|
+
});
|
|
158
|
+
it('attaches the validated folder when folderId is provided', async ()=>{
|
|
159
|
+
const folder = {
|
|
160
|
+
id: 'folder-1',
|
|
161
|
+
name: 'Docs'
|
|
162
|
+
};
|
|
163
|
+
mockFolderRepo.findOne.mockResolvedValue(folder);
|
|
164
|
+
const entity = await service.convertSingleDtoToEntity({
|
|
165
|
+
name: 'a.jpg',
|
|
166
|
+
key: 'k',
|
|
167
|
+
size: '10',
|
|
168
|
+
contentType: 'image/jpeg',
|
|
169
|
+
isPrivate: false,
|
|
170
|
+
folderId: 'folder-1'
|
|
171
|
+
}, buildMockUser());
|
|
172
|
+
expect(entity.folder).toEqual(folder);
|
|
173
|
+
});
|
|
174
|
+
it('throws BadRequestException when the referenced folder does not exist or access is denied', async ()=>{
|
|
175
|
+
mockFolderRepo.findOne.mockResolvedValue(null);
|
|
176
|
+
await expect(service.convertSingleDtoToEntity({
|
|
177
|
+
name: 'a.jpg',
|
|
178
|
+
key: 'k',
|
|
179
|
+
size: '10',
|
|
180
|
+
contentType: 'image/jpeg',
|
|
181
|
+
isPrivate: false,
|
|
182
|
+
folderId: 'missing-folder'
|
|
183
|
+
}, buildMockUser())).rejects.toThrow();
|
|
184
|
+
});
|
|
185
|
+
it('scopes folder lookup by companyId when company feature is enabled', async ()=>{
|
|
186
|
+
mockStorageConfig.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
187
|
+
mockFolderRepo.findOne.mockResolvedValue({
|
|
188
|
+
id: 'folder-1'
|
|
189
|
+
});
|
|
190
|
+
await service.convertSingleDtoToEntity({
|
|
191
|
+
name: 'a.jpg',
|
|
192
|
+
key: 'k',
|
|
193
|
+
size: '10',
|
|
194
|
+
contentType: 'image/jpeg',
|
|
195
|
+
isPrivate: false,
|
|
196
|
+
folderId: 'folder-1'
|
|
197
|
+
}, buildMockUser({
|
|
198
|
+
companyId: 'company-9'
|
|
199
|
+
}));
|
|
200
|
+
expect(mockFolderRepo.findOne).toHaveBeenCalledWith({
|
|
201
|
+
where: {
|
|
202
|
+
id: 'folder-1',
|
|
203
|
+
companyId: 'company-9'
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
it('attaches companyId to the entity when company feature is enabled', async ()=>{
|
|
208
|
+
mockStorageConfig.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
209
|
+
const entity = await service.convertSingleDtoToEntity({
|
|
210
|
+
name: 'a.jpg',
|
|
211
|
+
key: 'k',
|
|
212
|
+
size: '10',
|
|
213
|
+
contentType: 'image/jpeg',
|
|
214
|
+
isPrivate: false
|
|
215
|
+
}, buildMockUser({
|
|
216
|
+
companyId: 'company-9'
|
|
217
|
+
}));
|
|
218
|
+
expect(entity.companyId).toBe('company-9');
|
|
219
|
+
});
|
|
220
|
+
it('loads and merges the existing entity when updating (dto has an id)', async ()=>{
|
|
221
|
+
const existing = buildFile({
|
|
222
|
+
id: 'file-1',
|
|
223
|
+
name: 'old.jpg'
|
|
224
|
+
});
|
|
225
|
+
mockFileRepo.findOne.mockResolvedValue(existing);
|
|
226
|
+
const entity = await service.convertSingleDtoToEntity({
|
|
227
|
+
id: 'file-1',
|
|
228
|
+
name: 'new.jpg'
|
|
229
|
+
}, buildMockUser());
|
|
230
|
+
expect(entity.name).toBe('new.jpg');
|
|
231
|
+
});
|
|
232
|
+
it('throws NotFoundException when updating a file that does not exist', async ()=>{
|
|
233
|
+
mockFileRepo.findOne.mockResolvedValue(null);
|
|
234
|
+
await expect(service.convertSingleDtoToEntity({
|
|
235
|
+
id: 'missing-id',
|
|
236
|
+
name: 'x'
|
|
237
|
+
}, buildMockUser())).rejects.toThrow();
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
describe('getSelectQuery', ()=>{
|
|
241
|
+
it('selects the default field set and left-joins the folder relation', async ()=>{
|
|
242
|
+
const query = {
|
|
243
|
+
select: jest.fn().mockReturnThis(),
|
|
244
|
+
leftJoinAndSelect: jest.fn().mockReturnThis()
|
|
245
|
+
};
|
|
246
|
+
await service.getSelectQuery(query, buildMockUser());
|
|
247
|
+
expect(query.leftJoinAndSelect).toHaveBeenCalledWith('file_manager.folder', 'folder');
|
|
248
|
+
const selectArg = query.select.mock.calls[0][0];
|
|
249
|
+
expect(selectArg).toEqual(expect.arrayContaining([
|
|
250
|
+
'file_manager.id',
|
|
251
|
+
'file_manager.name',
|
|
252
|
+
'file_manager.key',
|
|
253
|
+
'folder.id',
|
|
254
|
+
'folder.name'
|
|
255
|
+
]));
|
|
256
|
+
});
|
|
257
|
+
it('appends companyId to the field set when company feature is enabled', async ()=>{
|
|
258
|
+
mockStorageConfig.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
259
|
+
const query = {
|
|
260
|
+
select: jest.fn().mockReturnThis(),
|
|
261
|
+
leftJoinAndSelect: jest.fn().mockReturnThis()
|
|
262
|
+
};
|
|
263
|
+
await service.getSelectQuery(query, buildMockUser());
|
|
264
|
+
const selectArg = query.select.mock.calls[0][0];
|
|
265
|
+
expect(selectArg).toContain('file_manager.companyId');
|
|
266
|
+
});
|
|
267
|
+
it('uses a custom select list when explicitly provided', async ()=>{
|
|
268
|
+
const query = {
|
|
269
|
+
select: jest.fn().mockReturnThis(),
|
|
270
|
+
leftJoinAndSelect: jest.fn().mockReturnThis()
|
|
271
|
+
};
|
|
272
|
+
await service.getSelectQuery(query, buildMockUser(), [
|
|
273
|
+
'id',
|
|
274
|
+
'name'
|
|
275
|
+
]);
|
|
276
|
+
const selectArg = query.select.mock.calls[0][0];
|
|
277
|
+
expect(selectArg).toEqual(expect.arrayContaining([
|
|
278
|
+
'file_manager.id',
|
|
279
|
+
'file_manager.name'
|
|
280
|
+
]));
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
describe('getFilterQuery', ()=>{
|
|
284
|
+
it('applies a LIKE-based bracket filter for contentType with wildcard support', async ()=>{
|
|
285
|
+
const query = {
|
|
286
|
+
where: jest.fn().mockReturnThis(),
|
|
287
|
+
andWhere: jest.fn().mockReturnThis()
|
|
288
|
+
};
|
|
289
|
+
await service.getFilterQuery(query, {
|
|
290
|
+
contentType: 'image/*,application/pdf'
|
|
291
|
+
}, buildMockUser());
|
|
292
|
+
expect(query.where).toHaveBeenCalled();
|
|
293
|
+
});
|
|
294
|
+
it('applies a plain equality filter for non-contentType fields', async ()=>{
|
|
295
|
+
const query = {
|
|
296
|
+
where: jest.fn().mockReturnThis(),
|
|
297
|
+
andWhere: jest.fn().mockReturnThis()
|
|
298
|
+
};
|
|
299
|
+
await service.getFilterQuery(query, {
|
|
300
|
+
isPrivate: true
|
|
301
|
+
}, buildMockUser());
|
|
302
|
+
expect(query.andWhere).toHaveBeenCalledWith('file_manager.isPrivate = :value', {
|
|
303
|
+
value: true
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
describe('getExtraManipulateQuery', ()=>{
|
|
308
|
+
it('applies the company filter when company feature is enabled', async ()=>{
|
|
309
|
+
mockStorageConfig.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
310
|
+
const query = {
|
|
311
|
+
andWhere: jest.fn().mockReturnThis()
|
|
312
|
+
};
|
|
313
|
+
await service.getExtraManipulateQuery(query, {}, buildMockUser({
|
|
314
|
+
companyId: 'company-3'
|
|
315
|
+
}));
|
|
316
|
+
expect(query.andWhere).toHaveBeenCalledWith('file_manager.companyId = :companyId', {
|
|
317
|
+
companyId: 'company-3'
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
it('hides private files from anonymous (unauthenticated) requests', async ()=>{
|
|
321
|
+
const query = {
|
|
322
|
+
andWhere: jest.fn().mockReturnThis()
|
|
323
|
+
};
|
|
324
|
+
await service.getExtraManipulateQuery(query, {}, null);
|
|
325
|
+
expect(query.andWhere).toHaveBeenCalledWith('file_manager.isPrivate = :isPrivate', {
|
|
326
|
+
isPrivate: false
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
it('hides private files from an authenticated user lacking the viewPrivate permission', async ()=>{
|
|
330
|
+
mockCacheManager.get.mockResolvedValue([
|
|
331
|
+
{
|
|
332
|
+
url: 'storage.file.someOtherAction'
|
|
333
|
+
}
|
|
334
|
+
]);
|
|
335
|
+
const query = {
|
|
336
|
+
andWhere: jest.fn().mockReturnThis()
|
|
337
|
+
};
|
|
338
|
+
await service.getExtraManipulateQuery(query, {}, buildMockUser());
|
|
339
|
+
expect(query.andWhere).toHaveBeenCalledWith('file_manager.isPrivate = :isPrivate', {
|
|
340
|
+
isPrivate: false
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
it('does not restrict private files for a user holding the viewPrivate permission', async ()=>{
|
|
344
|
+
mockCacheManager.get.mockResolvedValue([
|
|
345
|
+
{
|
|
346
|
+
url: 'storage.file.viewPrivate'
|
|
347
|
+
}
|
|
348
|
+
]);
|
|
349
|
+
const query = {
|
|
350
|
+
andWhere: jest.fn().mockReturnThis()
|
|
351
|
+
};
|
|
352
|
+
await service.getExtraManipulateQuery(query, {}, buildMockUser());
|
|
353
|
+
expect(query.andWhere).not.toHaveBeenCalledWith('file_manager.isPrivate = :isPrivate', expect.anything());
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
describe('beforeDeleteOperation', ()=>{
|
|
357
|
+
it('does nothing for a soft delete (type=delete)', async ()=>{
|
|
358
|
+
await service.beforeDeleteOperation({
|
|
359
|
+
id: [
|
|
360
|
+
'file-1'
|
|
361
|
+
],
|
|
362
|
+
type: 'delete'
|
|
363
|
+
}, buildMockUser(), {});
|
|
364
|
+
expect(mockUploadService.deleteMultipleFile).not.toHaveBeenCalled();
|
|
365
|
+
});
|
|
366
|
+
it('deletes underlying storage objects when permanently deleting files', async ()=>{
|
|
367
|
+
const file = buildFile({
|
|
368
|
+
id: 'file-1',
|
|
369
|
+
key: 'uploads/photo.jpg',
|
|
370
|
+
storageConfigId: null,
|
|
371
|
+
location: FileLocationEnum.LOCAL
|
|
372
|
+
});
|
|
373
|
+
mockFileRepo.findBy.mockResolvedValue([
|
|
374
|
+
file
|
|
375
|
+
]);
|
|
376
|
+
await service.beforeDeleteOperation({
|
|
377
|
+
id: [
|
|
378
|
+
'file-1'
|
|
379
|
+
],
|
|
380
|
+
type: 'permanent'
|
|
381
|
+
}, buildMockUser(), {});
|
|
382
|
+
expect(mockUploadService.deleteMultipleFile).toHaveBeenCalledWith([
|
|
383
|
+
'uploads/photo.jpg'
|
|
384
|
+
], undefined, buildMockUser(), FileLocationEnum.LOCAL);
|
|
385
|
+
});
|
|
386
|
+
it('groups files by storageConfigId before issuing delete calls', async ()=>{
|
|
387
|
+
const fileA = buildFile({
|
|
388
|
+
id: 'a',
|
|
389
|
+
key: 'a.jpg',
|
|
390
|
+
storageConfigId: 'cfg-1',
|
|
391
|
+
location: FileLocationEnum.AWS
|
|
392
|
+
});
|
|
393
|
+
const fileB = buildFile({
|
|
394
|
+
id: 'b',
|
|
395
|
+
key: 'b.jpg',
|
|
396
|
+
storageConfigId: 'cfg-2',
|
|
397
|
+
location: FileLocationEnum.AZURE
|
|
398
|
+
});
|
|
399
|
+
mockFileRepo.findBy.mockResolvedValue([
|
|
400
|
+
fileA,
|
|
401
|
+
fileB
|
|
402
|
+
]);
|
|
403
|
+
await service.beforeDeleteOperation({
|
|
404
|
+
id: [
|
|
405
|
+
'a',
|
|
406
|
+
'b'
|
|
407
|
+
],
|
|
408
|
+
type: 'permanent'
|
|
409
|
+
}, buildMockUser(), {});
|
|
410
|
+
expect(mockUploadService.deleteMultipleFile).toHaveBeenCalledTimes(2);
|
|
411
|
+
expect(mockUploadService.deleteMultipleFile).toHaveBeenCalledWith([
|
|
412
|
+
'a.jpg'
|
|
413
|
+
], 'cfg-1', buildMockUser(), FileLocationEnum.AWS);
|
|
414
|
+
expect(mockUploadService.deleteMultipleFile).toHaveBeenCalledWith([
|
|
415
|
+
'b.jpg'
|
|
416
|
+
], 'cfg-2', buildMockUser(), FileLocationEnum.AZURE);
|
|
417
|
+
});
|
|
418
|
+
it('prefixes local keys with the resolved storage config basePath when not already nested', async ()=>{
|
|
419
|
+
const file = buildFile({
|
|
420
|
+
id: 'a',
|
|
421
|
+
key: 'plain-key.jpg',
|
|
422
|
+
storageConfigId: 'cfg-1',
|
|
423
|
+
location: FileLocationEnum.LOCAL
|
|
424
|
+
});
|
|
425
|
+
mockFileRepo.findBy.mockResolvedValue([
|
|
426
|
+
file
|
|
427
|
+
]);
|
|
428
|
+
mockStorageConfigRepo.findOne.mockResolvedValue({
|
|
429
|
+
id: 'cfg-1',
|
|
430
|
+
config: {
|
|
431
|
+
basePath: './custom-uploads'
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
await service.beforeDeleteOperation({
|
|
435
|
+
id: [
|
|
436
|
+
'a'
|
|
437
|
+
],
|
|
438
|
+
type: 'permanent'
|
|
439
|
+
}, buildMockUser(), {});
|
|
440
|
+
expect(mockUploadService.deleteMultipleFile).toHaveBeenCalledWith([
|
|
441
|
+
'custom-uploads/plain-key.jpg'
|
|
442
|
+
], 'cfg-1', buildMockUser(), FileLocationEnum.LOCAL);
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
describe('getFiles', ()=>{
|
|
446
|
+
it('throws BadRequestException when no valid file ids are provided', async ()=>{
|
|
447
|
+
await expect(service.getFiles([], 'http', 'localhost:2002')).rejects.toThrow();
|
|
448
|
+
});
|
|
449
|
+
it('throws BadRequestException when all provided ids are falsy', async ()=>{
|
|
450
|
+
await expect(service.getFiles([
|
|
451
|
+
{
|
|
452
|
+
id: ''
|
|
453
|
+
},
|
|
454
|
+
{
|
|
455
|
+
id: undefined
|
|
456
|
+
}
|
|
457
|
+
], 'http', 'localhost:2002')).rejects.toThrow();
|
|
458
|
+
});
|
|
459
|
+
it('returns file metadata for the requested ids', async ()=>{
|
|
460
|
+
const file = buildFile({
|
|
461
|
+
id: 'file-1',
|
|
462
|
+
url: 'http://localhost:2002/storage/upload/file/uploads/photo.jpg'
|
|
463
|
+
});
|
|
464
|
+
mockFileRepo.findBy.mockResolvedValue([
|
|
465
|
+
file
|
|
466
|
+
]);
|
|
467
|
+
const result = await service.getFiles([
|
|
468
|
+
{
|
|
469
|
+
id: 'file-1'
|
|
470
|
+
}
|
|
471
|
+
], 'http', 'localhost:2002');
|
|
472
|
+
expect(result).toHaveLength(1);
|
|
473
|
+
expect(result[0].id).toBe('file-1');
|
|
474
|
+
expect(result[0].name).toBe('photo.jpg');
|
|
475
|
+
});
|
|
476
|
+
it('refreshes and persists file URLs that changed during the request', async ()=>{
|
|
477
|
+
const file = buildFile({
|
|
478
|
+
id: 'file-1',
|
|
479
|
+
url: null,
|
|
480
|
+
storageConfigId: 'cfg-1'
|
|
481
|
+
});
|
|
482
|
+
mockFileRepo.findBy.mockResolvedValue([
|
|
483
|
+
file
|
|
484
|
+
]);
|
|
485
|
+
mockUploadService.makeFileUrl.mockResolvedValue('https://cdn.example.com/photo.jpg');
|
|
486
|
+
await service.getFiles([
|
|
487
|
+
{
|
|
488
|
+
id: 'file-1'
|
|
489
|
+
}
|
|
490
|
+
], 'http', 'localhost:2002');
|
|
491
|
+
expect(mockFileRepo.save).toHaveBeenCalled();
|
|
492
|
+
});
|
|
493
|
+
it('does not persist when no file URL needed refreshing', async ()=>{
|
|
494
|
+
const file = buildFile({
|
|
495
|
+
id: 'file-1',
|
|
496
|
+
url: 'http://localhost:2002/storage/upload/file/uploads/photo.jpg',
|
|
497
|
+
location: FileLocationEnum.LOCAL,
|
|
498
|
+
storageConfigId: null
|
|
499
|
+
});
|
|
500
|
+
mockFileRepo.findBy.mockResolvedValue([
|
|
501
|
+
file
|
|
502
|
+
]);
|
|
503
|
+
await service.getFiles([
|
|
504
|
+
{
|
|
505
|
+
id: 'file-1'
|
|
506
|
+
}
|
|
507
|
+
], 'http', 'localhost:2002');
|
|
508
|
+
expect(mockFileRepo.save).not.toHaveBeenCalled();
|
|
509
|
+
});
|
|
510
|
+
it('silently continues if persisting refreshed URLs fails (non-critical)', async ()=>{
|
|
511
|
+
const file = buildFile({
|
|
512
|
+
id: 'file-1',
|
|
513
|
+
url: null,
|
|
514
|
+
storageConfigId: 'cfg-1'
|
|
515
|
+
});
|
|
516
|
+
mockFileRepo.findBy.mockResolvedValue([
|
|
517
|
+
file
|
|
518
|
+
]);
|
|
519
|
+
mockFileRepo.save.mockRejectedValue(new Error('db write failed'));
|
|
520
|
+
await expect(service.getFiles([
|
|
521
|
+
{
|
|
522
|
+
id: 'file-1'
|
|
523
|
+
}
|
|
524
|
+
], 'http', 'localhost:2002')).resolves.toBeDefined();
|
|
525
|
+
});
|
|
526
|
+
it('falls back to the direct file-serve URL when makeFileUrl throws (e.g. missing storage config)', async ()=>{
|
|
527
|
+
const file = buildFile({
|
|
528
|
+
id: 'file-1',
|
|
529
|
+
url: null,
|
|
530
|
+
storageConfigId: 'cfg-missing',
|
|
531
|
+
location: FileLocationEnum.AWS
|
|
532
|
+
});
|
|
533
|
+
mockFileRepo.findBy.mockResolvedValue([
|
|
534
|
+
file
|
|
535
|
+
]);
|
|
536
|
+
mockUploadService.makeFileUrl.mockRejectedValue(new Error('config not found'));
|
|
537
|
+
const result = await service.getFiles([
|
|
538
|
+
{
|
|
539
|
+
id: 'file-1'
|
|
540
|
+
}
|
|
541
|
+
], 'http', 'localhost:2002');
|
|
542
|
+
expect(result[0].url).toContain('/storage/upload/file/');
|
|
543
|
+
});
|
|
544
|
+
});
|
|
545
|
+
});
|