11import { Storage , FilesTransform , ListResult } from 'storage-core'
2+ import pathModule from 'path'
3+
24import {
35 Storage as GoogleStorage ,
46 Bucket as GoogleBucket ,
@@ -11,6 +13,17 @@ import {
1113import { GcpBucketStorageOptions } from './GcpBucketStorageOptions'
1214import { Readable , Writable } from 'stream'
1315
16+ const formatFile = ( f : File ) => ( {
17+ name : pathModule . basename ( f . name ) ,
18+ path : f . name ,
19+ size : f . metadata . size ,
20+ md5Hash : f . metadata . md5Hash ,
21+ createdAt : new Date ( f . metadata . timeCreated ) ,
22+ updatedAt : new Date ( f . metadata . updated ) ,
23+ isFile : true ,
24+ raw : f . metadata
25+ } )
26+
1427export class GcpBucketStorage extends Storage < GcpBucketStorageOptions > {
1528 googleStorage : GoogleStorage
1629 bucket : GoogleBucket
@@ -32,8 +45,29 @@ export class GcpBucketStorage extends Storage<GcpBucketStorageOptions> {
3245 this . bucket = this . googleStorage . bucket ( bucket )
3346 }
3447
35- getTopLevel ( _ ?: string ) : Promise < ListResult [ ] > {
36- throw new Error ( 'Method not implemented.' )
48+ getTopLevel ( path ?: string ) : Promise < ListResult [ ] > {
49+ return new Promise ( ( resolve , reject ) => {
50+ this . bucket . getFiles (
51+ {
52+ autoPaginate : false ,
53+ directory : path ,
54+ delimiter : '/'
55+ } ,
56+ ( err , files , _ , { prefixes } = { } ) => {
57+ if ( err ) reject ( err )
58+
59+ const dirs : ListResult [ ] = ( prefixes || [ ] ) . map ( ( d : string ) => ( {
60+ name : pathModule . basename ( d ) ,
61+ path : d . slice ( 0 , - 1 ) ,
62+ isFile : false ,
63+ raw : d
64+ } ) )
65+ const f : ListResult [ ] = ( files || [ ] ) . map ( formatFile )
66+
67+ resolve ( dirs . concat ( f ) )
68+ }
69+ )
70+ } )
3771 }
3872
3973 async getFiles ( path ?: string ) : Promise < string [ ] > {
@@ -42,15 +76,7 @@ export class GcpBucketStorage extends Storage<GcpBucketStorageOptions> {
4276 }
4377
4478 getFilesStream ( path ?: string ) : Readable {
45- const trans = new FilesTransform ( ( f : File ) => ( {
46- name : f . name ,
47- path : f . name ,
48- size : f . metadata . size ,
49- md5Hash : f . metadata . md5Hash ,
50- createdAt : new Date ( f . metadata . timeCreated ) ,
51- updatedAt : new Date ( f . metadata . updated ) ,
52- raw : f . metadata
53- } ) )
79+ const trans = new FilesTransform ( formatFile )
5480 return this . bucket . getFilesStream ( { directory : path } ) . pipe ( trans )
5581 }
5682
0 commit comments