@@ -39,17 +39,8 @@ const findUploadedFiles = async (manifest: Manifest, storageBucket: any) => {
39
39
return filesToUpload ;
40
40
} ;
41
41
42
- export async function uploadManifest (
43
- bucket : string ,
44
- manifest : Manifest ,
45
- force ?: boolean ,
46
- ttl ?: Date
47
- ) {
48
- bucket = bucket || DEFAULT_BUCKET ; // If bucket is blank.
49
- console . log ( `Using storage: ${ bucket } /${ getBlobPath ( manifest . site , '' ) } ` ) ;
50
-
51
- const storage = new Storage ( ) ;
52
- const bar = new cliProgress . SingleBar (
42
+ function createProgressBar ( ) {
43
+ return new cliProgress . SingleBar (
53
44
{
54
45
format :
55
46
'Uploading ({value}/{total}): ' +
@@ -58,15 +49,25 @@ export async function uploadManifest(
58
49
} ,
59
50
cliProgress . Presets . shades_classic
60
51
) ;
52
+ }
61
53
62
- const storageBucket = storage . bucket ( bucket ) ;
63
-
64
- // Check whether files exist prior to uploading. Existing files can be skipped.
65
- let filesToUpload = await findUploadedFiles ( manifest , storageBucket ) ;
66
- if ( force ) {
67
- filesToUpload = manifest . files ;
68
- }
69
-
54
+ export async function uploadManifest (
55
+ bucket : string ,
56
+ manifest : Manifest ,
57
+ force ?: boolean ,
58
+ ttl ?: Date
59
+ ) {
60
+ bucket = bucket || DEFAULT_BUCKET ;
61
+ console . log ( `Using storage: ${ bucket } /${ getBlobPath ( manifest . site , '' ) } ` ) ;
62
+ const storageBucket = new Storage ( ) . bucket ( bucket ) ;
63
+ const bar = createProgressBar ( ) ;
64
+
65
+ // Check for files that have already been uploaded. Files already uploaded do
66
+ // not need to be uploaded again, as the GCS path is keyed by the file's
67
+ // contents.
68
+ const filesToUpload = force
69
+ ? manifest . files
70
+ : await findUploadedFiles ( manifest , storageBucket ) ;
70
71
const numFiles = filesToUpload . length ;
71
72
console . log (
72
73
`Found new ${ filesToUpload . length } files out of ${ manifest . files . length } total...`
@@ -80,14 +81,12 @@ export async function uploadManifest(
80
81
speed : 0 ,
81
82
} ) ;
82
83
83
- // Only upload new files.
84
84
mapLimit (
85
85
filesToUpload ,
86
86
NUM_CONCURRENT_UPLOADS ,
87
- ( manifestFile , callback ) => {
87
+ asyncify ( async ( manifestFile : ManifestFile ) => {
88
88
const remotePath = getBlobPath ( manifest . site , manifestFile . hash ) ;
89
89
90
- // console.log(`Uploading ${manifestFile.cleanPath} -> ${bucket}/${remotePath}`);
91
90
// NOTE: This was causing stale responses, even when rewritten by the client-server: 'public, max-age=31536000',
92
91
// https://cloud.google.com/storage/docs/gsutil/addlhelp/WorkingWithObjectMetadata#cache-control
93
92
// NOTE: In order for GCS to respond extremely fast, it requires a longer cache expiry time.
@@ -100,25 +99,23 @@ export async function uploadManifest(
100
99
} ,
101
100
} ;
102
101
103
- // TODO: Handle upload errors and retries.
104
- storageBucket
105
- . upload ( manifestFile . path , {
106
- //gzip: true, // gzip: true must *not* be set here, as it interferes with the proxied GCS response.
107
- destination : remotePath ,
108
- metadata : metadata ,
109
- } )
110
- . then ( ( resp : any ) => {
111
- totalTransferred += parseInt ( resp [ 1 ] . size ) ;
112
- const elapsed = Math . floor ( Date . now ( ) / 1000 ) - startTime ;
113
- bar . update ( ( numProcessed += 1 ) , {
114
- speed : ( totalTransferred / elapsed / ( 1024 * 1024 ) ) . toFixed ( 2 ) ,
115
- } ) ;
116
- if ( numProcessed == numFiles ) {
117
- bar . stop ( ) ;
118
- }
119
- callback ( ) ;
120
- } ) ;
121
- }
102
+ // TODO: Veryify this correctly handles errors and retry attempts.
103
+ const resp = await storageBucket . upload ( manifestFile . path , {
104
+ // NOTE: `gzip: true` must *not* be set here. Doing so interferes
105
+ // with the proxied GCS response. Despite not setting `gzip: true`,
106
+ // the response remains gzipped from the proxy server.
107
+ destination : remotePath ,
108
+ metadata : metadata ,
109
+ } ) ;
110
+ totalTransferred += parseInt ( resp [ 1 ] . size ) ;
111
+ const elapsed = Math . floor ( Date . now ( ) / 1000 ) - startTime ;
112
+ bar . update ( ( numProcessed += 1 ) , {
113
+ speed : ( totalTransferred / elapsed / ( 1024 * 1024 ) ) . toFixed ( 2 ) ,
114
+ } ) ;
115
+ if ( numProcessed === numFiles ) {
116
+ bar . stop ( ) ;
117
+ }
118
+ } )
122
119
) ;
123
120
124
121
// @ts -ignore
0 commit comments