Skip to content

Commit d25fa6f

Browse files
committed
feat(core): getUrl and getTemporaryUrl support
1 parent 4503bfd commit d25fa6f

File tree

4 files changed

+87
-9
lines changed

4 files changed

+87
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
]
9494
},
9595
"dependencies": {
96-
"@carimus/node-disks": "^1.8.0",
96+
"@carimus/node-disks": "^1.9.1",
9797
"tmp": "^0.1.0"
9898
}
9999
}

src/lib/Uploads.test.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
import * as fs from 'fs';
22
import { promisify } from 'util';
3-
import { DiskDriver, DiskManager, streamToBuffer } from '@carimus/node-disks';
3+
import {
4+
DiskDriver,
5+
DiskManager,
6+
DiskManagerConfig,
7+
streamToBuffer,
8+
} from '@carimus/node-disks';
49
import { MemoryRepository, MemoryRepositoryRecord } from '../support';
510
import { Uploads } from './Uploads';
611
import { UploadMeta } from '../types';
712

813
const readFileFromLocalFilesystem = promisify(fs.readFile);
914
const deleteFromLocalFilesystem = promisify(fs.unlink);
1015

11-
const disks = {
16+
const disks: DiskManagerConfig = {
1217
default: 'memory',
1318
memory: {
1419
driver: DiskDriver.Memory,
1520
},
1621
nonDefaultMemory: {
1722
driver: DiskDriver.Memory,
1823
},
24+
memoryWithUrls: {
25+
driver: DiskDriver.Memory,
26+
config: {
27+
url: 'http://localhost',
28+
temporaryUrlFallback: true,
29+
},
30+
},
1931
};
2032

21-
function setup(): {
33+
function setup(
34+
defaultDisk = 'default',
35+
): {
2236
diskManager: DiskManager;
2337
repository: MemoryRepository;
2438
uploads: Uploads<MemoryRepositoryRecord>;
@@ -29,7 +43,7 @@ function setup(): {
2943
diskManager,
3044
repository,
3145
uploads: new Uploads<MemoryRepositoryRecord>({
32-
defaultDisk: 'default',
46+
defaultDisk,
3347
disks: diskManager,
3448
repository,
3549
}),
@@ -450,3 +464,31 @@ test('Uploads can read and createReadStream for uploads', async () => {
450464
uploadsReadStreamFileData.toString('base64'),
451465
);
452466
});
467+
468+
test('Uploads can generate urls and temp urls for uploads on disks that supports URLs', async () => {
469+
const { uploads } = setup('memoryWithUrls');
470+
471+
// Upload a file
472+
const upload = await uploads.upload(
473+
files.longName.data,
474+
files.longName.uploadedAs,
475+
files.longName.meta,
476+
);
477+
478+
// Check the URLs generated
479+
const url = await uploads.getUrl(upload);
480+
const tempUrl = await uploads.getTemporaryUrl(upload);
481+
expect(url).toBeTruthy();
482+
expect(typeof url).toBe('string');
483+
expect((url as string).indexOf('http://localhost')).toBe(0);
484+
expect(tempUrl).toBeTruthy();
485+
expect(typeof tempUrl).toBe('string');
486+
expect((tempUrl as string).indexOf('http://localhost')).toBe(0);
487+
488+
// Transfer the upload to a disk that doesn't support URLs
489+
await uploads.transfer(upload, 'default');
490+
491+
// Check to ensure the url and tempUrl are null for this upload now.
492+
expect(await uploads.getUrl(upload)).toBeNull();
493+
expect(await uploads.getTemporaryUrl(upload)).toBeNull();
494+
});

src/lib/Uploads.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,41 @@ export class Uploads<Upload> {
336336
return this.disks.getDisk(file.disk).createReadStream(file.path);
337337
}
338338

339+
/**
340+
* Get a URL from the disk for an upload. Will be null if the disk that the upload is stored on does not support
341+
* URLs or is not configured for them.
342+
*
343+
* @param upload
344+
*/
345+
public async getUrl(upload: Upload): Promise<string | null> {
346+
// Ask the repository for info on where and how the upload file is stored.
347+
const file = await this.repository.getUploadedFileInfo(upload);
348+
// Read the data into memory in a Buffer and resolve with it.
349+
return this.disks.getDisk(file.disk).getUrl(file.path);
350+
}
351+
352+
/**
353+
* Get a temporary URL from the disk for an upload. Will be null if the disk that the upload is stored on does not
354+
* support temporary URLs or is not configured for them.
355+
*
356+
* @param upload
357+
* @param expires The number of seconds the URL should expire at. Default is configured at the disk level.
358+
* @param fallback Whether or not to fallback to permanent URLs if the disk doesn't support temporary URLs. Defualt
359+
* is generally false but is configured at the disk level.
360+
*/
361+
public async getTemporaryUrl(
362+
upload: Upload,
363+
expires?: number,
364+
fallback?: boolean,
365+
): Promise<string | null> {
366+
// Ask the repository for info on where and how the upload file is stored.
367+
const file = await this.repository.getUploadedFileInfo(upload);
368+
// Read the data into memory in a Buffer and resolve with it.
369+
return this.disks
370+
.getDisk(file.disk)
371+
.getTemporaryUrl(file.path, expires, fallback);
372+
}
373+
339374
/**
340375
* Download the file to the local disk as a temporary file for operations that require local data manipuation
341376
* and which can't handle Buffers, i.e. operations expected to be performed on large files where it's easier to

yarn.lock

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,16 @@
138138
lodash "^4.17.11"
139139
to-fast-properties "^2.0.0"
140140

141-
"@carimus/node-disks@^1.8.0":
142-
version "1.8.0"
143-
resolved "https://registry.yarnpkg.com/@carimus/node-disks/-/node-disks-1.8.0.tgz#d5d12300dfaf244889570f5309e354b8df18ef3b"
144-
integrity sha512-zdi7euqzn76Z2hyMBN5hUsr9beSDRB8fCktRz4xuUTXNAqmL15kCKruSV3WHB7tz4nUDn+Hr2yCYM3U+S3jgyw==
141+
"@carimus/node-disks@^1.9.1":
142+
version "1.9.1"
143+
resolved "https://registry.yarnpkg.com/@carimus/node-disks/-/node-disks-1.9.1.tgz#4a0f8bb54a07fcb59cb5dd2c43cb169e542e1c1b"
144+
integrity sha512-yvsLRemMfB4wT+R0uMOvXgfZb3O7PGizVGBcSWMdkCM8PfdKgJW/ZvPSmxZwDDXrQK20HTScgUTsVcyTviDRfg==
145145
dependencies:
146146
aws-sdk "^2.431.0"
147147
fs-extra "^7.0.1"
148148
ramda "0.25.0"
149149
stream-to-array "^2.3.0"
150+
url-join "^4.0.0"
150151
verror "^1.10.0"
151152

152153
"@cnakazawa/watch@^1.0.3":

0 commit comments

Comments
 (0)