|
| 1 | +import * as fs from 'fs'; |
| 2 | +import { promisify } from 'util'; |
1 | 3 | import { DiskDriver, DiskManager } from '@carimus/node-disks'; |
2 | 4 | import { MemoryRepository } from '../support'; |
3 | 5 | import { Uploads } from './Uploads'; |
4 | 6 | import { UploadMeta } from '../types'; |
5 | 7 |
|
| 8 | +const readFileFromLocalFilesystem = promisify(fs.readFile); |
| 9 | +const deleteFromLocalFilesystem = promisify(fs.unlink); |
| 10 | + |
6 | 11 | const disks = { |
7 | 12 | default: 'memory', |
8 | 13 | memory: { |
@@ -46,7 +51,18 @@ const files: { |
46 | 51 | weirdName: { |
47 | 52 | uploadedAs: '.~my~cool~data~&^%$*(¶•ª•.csv', |
48 | 53 | data: Buffer.from('a,b,c\nfoo,bar,baz\n1,2,3\n', 'utf8'), |
49 | | - meta: { context: 'test', isFoo: false, isImage: true }, |
| 54 | + meta: { context: 'test', isFoo: false, isImage: false }, |
| 55 | + }, |
| 56 | + longName: { |
| 57 | + uploadedAs: |
| 58 | + '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.csv', |
| 59 | + data: Buffer.from('a,b,c\nfoo,bar,baz\n1,2,3\n', 'utf8'), |
| 60 | + meta: { |
| 61 | + context: 'test', |
| 62 | + isFoo: false, |
| 63 | + isImage: false, |
| 64 | + isSuperLong: true, |
| 65 | + }, |
50 | 66 | }, |
51 | 67 | }; |
52 | 68 |
|
@@ -258,3 +274,44 @@ test('Uploads service can delete only the file', async () => { |
258 | 274 | diskManager.getDisk(fileInfo.disk).read(fileInfo.path), |
259 | 275 | ).rejects.toBeTruthy(); |
260 | 276 | }); |
| 277 | + |
| 278 | +test('Uploads service can create temp files for local manipulation from uploads', async () => { |
| 279 | + const { diskManager, repository, uploads } = setup(); |
| 280 | + |
| 281 | + // Upload a file |
| 282 | + const upload = await uploads.upload( |
| 283 | + files.longName.data, |
| 284 | + files.longName.uploadedAs, |
| 285 | + files.longName.meta, |
| 286 | + ); |
| 287 | + const fileInfo = await repository.getUploadedFileInfo(upload); |
| 288 | + const uploadedFileData = await diskManager |
| 289 | + .getDisk(fileInfo.disk) |
| 290 | + .read(fileInfo.path); |
| 291 | + |
| 292 | + // Get the temp file for it and check to make sure their contents match |
| 293 | + const tempPath = await uploads.withTempFile(upload, async (path) => { |
| 294 | + const tempFileData = await readFileFromLocalFilesystem(path); |
| 295 | + expect(tempFileData.toString('base64')).toBe( |
| 296 | + uploadedFileData.toString('base64'), |
| 297 | + ); |
| 298 | + }); |
| 299 | + |
| 300 | + // Ensure that once the callback is completed, the file doesn't exist since we didn't tell it not to cleanup |
| 301 | + expect(tempPath).toBeTruthy(); |
| 302 | + await expect(readFileFromLocalFilesystem(tempPath)).rejects.toBeTruthy(); |
| 303 | + |
| 304 | + // Do the same stuff again but using the bypass cleanup approach to take cleanup into our own hands |
| 305 | + const persistentTempPath = await uploads.withTempFile(upload); |
| 306 | + expect(persistentTempPath).toBeTruthy(); |
| 307 | + const persistentTempFileData = await readFileFromLocalFilesystem( |
| 308 | + persistentTempPath, |
| 309 | + ); |
| 310 | + expect(persistentTempFileData.toString('base64')).toBe( |
| 311 | + uploadedFileData.toString('base64'), |
| 312 | + ); |
| 313 | + // Note that we use `.resolves.toBeUndefined()` to verify the file is deleted (unlink resolves with void/undefined) |
| 314 | + expect( |
| 315 | + deleteFromLocalFilesystem(persistentTempPath), |
| 316 | + ).resolves.toBeUndefined(); |
| 317 | +}); |
0 commit comments