11import * as fs from 'fs' ;
22import { 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' ;
49import { MemoryRepository , MemoryRepositoryRecord } from '../support' ;
510import { Uploads } from './Uploads' ;
611import { UploadMeta } from '../types' ;
712
813const readFileFromLocalFilesystem = promisify ( fs . readFile ) ;
914const 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+ } ) ;
0 commit comments