@@ -23,7 +23,11 @@ export class StorageService{
2323 private getFileName ( sum : string ) : string {
2424 if ( this . s3Client )
2525 return `${ sum . substring ( 0 , 2 ) } /${ sum } .webp` ;
26- return `.storage/${ sum . substring ( 0 , 2 ) } /${ sum } .webp` ;
26+ return `images/${ sum . substring ( 0 , 2 ) } /${ sum } .webp` ;
27+ }
28+
29+ isS3 ( ) : boolean {
30+ return ! ! this . s3Client ;
2731 }
2832
2933 async uploadBuffer ( data : Buffer ) : Promise < string > {
@@ -68,33 +72,41 @@ export class StorageService{
6872 }
6973
7074 async listFiles ( take : number = Infinity , skip : number = 0 ) : Promise < ( BunFile | S3File ) [ ] > {
71- const files : ( BunFile | S3File ) [ ] = [ ] ;
72- if ( this . s3Client ) {
73- let continuationToken : string | undefined ;
74- let remainingItems : number = take ;
75- let totalSkipped : number = 0 ;
76- do {
77- const s3Files : S3ListObjectsResponse = await this . s3Client . list ( {
78- maxKeys : Math . min ( remainingItems + Math . max ( 0 , skip - totalSkipped ) , 1000 ) ,
79- continuationToken,
80- } ) ;
81- let itemsToProcess = s3Files . contents || [ ] ;
82- if ( totalSkipped < skip ) {
83- const skipInThisBatch : number = Math . min ( skip - totalSkipped , itemsToProcess . length ) ;
84- itemsToProcess = itemsToProcess . slice ( skipInThisBatch ) ;
85- totalSkipped += skipInThisBatch ;
86- }
87- const itemsToTake : number = Math . min ( remainingItems , itemsToProcess . length ) ;
88- for ( const file of itemsToProcess . slice ( 0 , itemsToTake ) )
89- files . push ( this . s3Client . file ( file . key ) ) ;
90- remainingItems -= itemsToTake ;
91- continuationToken = s3Files . nextContinuationToken ;
92- } while ( remainingItems > 0 && continuationToken ) ;
93- return files ;
94- }
75+ if ( this . s3Client )
76+ return this . listS3Files ( take , skip ) ;
77+ return this . listLocalFiles ( take , skip ) ;
78+ }
79+
80+ async listLocalFiles ( take : number = Infinity , skip : number = 0 ) : Promise < BunFile [ ] > {
81+ const files : BunFile [ ] = [ ] ;
9582 const glob = new Glob ( "**/*" ) ;
96- for ( const filePath of glob . scanSync ( "./.storage " ) )
97- files . push ( Bun . file ( ".storage /" + filePath ) ) ;
83+ for ( const filePath of glob . scanSync ( "images " ) )
84+ files . push ( Bun . file ( "images /" + filePath ) ) ;
9885 return files . slice ( skip , take === Infinity ? undefined : take + skip ) ;
9986 }
87+
88+ async listS3Files ( take : number = Infinity , skip : number = 0 ) : Promise < S3File [ ] > {
89+ const files : S3File [ ] = [ ] ;
90+ let continuationToken : string | undefined ;
91+ let remainingItems : number = take ;
92+ let totalSkipped : number = 0 ;
93+ do {
94+ const s3Files : S3ListObjectsResponse = await this . s3Client . list ( {
95+ maxKeys : Math . min ( remainingItems + Math . max ( 0 , skip - totalSkipped ) , 1000 ) ,
96+ continuationToken,
97+ } ) ;
98+ let itemsToProcess = s3Files . contents || [ ] ;
99+ if ( totalSkipped < skip ) {
100+ const skipInThisBatch : number = Math . min ( skip - totalSkipped , itemsToProcess . length ) ;
101+ itemsToProcess = itemsToProcess . slice ( skipInThisBatch ) ;
102+ totalSkipped += skipInThisBatch ;
103+ }
104+ const itemsToTake : number = Math . min ( remainingItems , itemsToProcess . length ) ;
105+ for ( const file of itemsToProcess . slice ( 0 , itemsToTake ) )
106+ files . push ( this . s3Client . file ( file . key ) ) ;
107+ remainingItems -= itemsToTake ;
108+ continuationToken = s3Files . nextContinuationToken ;
109+ } while ( remainingItems > 0 && continuationToken ) ;
110+ return files ;
111+ }
100112}
0 commit comments