@@ -26,12 +26,22 @@ export class GCSStorage implements StorageInterface {
2626 }
2727
2828 async listDirectories ( directory : string ) : Promise < string [ ] > {
29+ // GCS returns file names as fully qualified paths
2930 const [ files ] = await this . storage . bucket ( this . bucketName ) . getFiles ( {
3031 prefix : directory ,
31- delimiter : '/' ,
32+ delimiter : directory . charAt ( directory . length - 1 ) === '/' ? '' : '/' ,
3233 } ) ;
3334
34- return files . map ( ( file : any ) => file . name . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) ) ;
35+ // remove directory path from the file name in result
36+ //only return the first folder name in resulting path
37+ const innerFolders = files . map ( ( file : any ) => {
38+ return file . name
39+ . replace ( directory , '' )
40+ . replace ( / ^ \/ + | \/ + $ / g, '' )
41+ . split ( '/' ) [ 0 ] ;
42+ } ) ;
43+
44+ return innerFolders . filter ( ( value , index , array ) => array . indexOf ( value ) === index ) ;
3545 }
3646
3747 async uploadFile ( path : string , file : Buffer ) : Promise < string > {
@@ -62,12 +72,14 @@ export class GCSStorage implements StorageInterface {
6272 metadata : { size : number ; mimetype : string } ;
6373 } [ ]
6474 > {
75+ // GCS returns file names as fully qualified paths
6576 const [ files ] = await this . storage . bucket ( this . bucketName ) . getFiles ( {
6677 prefix : directory ,
6778 } ) ;
6879
6980 return files . map ( ( file : any ) => ( {
70- name : file . name ,
81+ // remove directory path from the file name in result
82+ name : file . name . split ( '/' ) . pop ( ) ,
7183 updated_at : file . metadata . updated ,
7284 created_at : file . metadata . timeCreated ,
7385 metadata : {
0 commit comments