@@ -62,9 +62,11 @@ class GCSDataStore extends DataStore {
6262 }
6363
6464 /**
65- * [create description]
66- * @param {[type] } req [description]
67- * @return {[type] } [description]
65+ * Create an empty file in GCS to store the metatdata.
66+ *
67+ * @param {object } req http.incomingMessage
68+ * @param {File } file
69+ * @return {Promise }
6870 */
6971 create ( req ) {
7072 return new Promise ( ( resolve , reject ) => {
@@ -111,48 +113,64 @@ class GCSDataStore extends DataStore {
111113 }
112114
113115 /**
114- * [write description]
115- * @param {[type] } req [description]
116- * @param {[type] } file_id [description]
117- * @param {[type] } offset [description]
118- * @return {[type] } [description]
116+ * Get the file metatata from the object in GCS, then upload a new version
117+ * passing through the metadata to the new version.
118+ *
119+ * @param {object } req http.incomingMessage
120+ * @param {string } file_id Name of file
121+ * @param {integer } offset starting offset
122+ * @return {Promise }
119123 */
120124 write ( req , file_id , offset ) {
121- return new Promise ( ( resolve , reject ) => {
122- const file = this . bucket . file ( file_id ) ;
125+ // GCS Doesn't persist metadata within versions,
126+ // get that metadata first
127+ return this . getOffset ( file_id )
128+ . then ( ( data ) => {
129+ return new Promise ( ( resolve , reject ) => {
130+ const file = this . bucket . file ( file_id ) ;
131+
132+ const options = {
133+ offset,
134+ metadata : {
135+ metadata : {
136+ upload_length : data . upload_length ,
137+ tus_version : TUS_RESUMABLE ,
138+ upload_metadata : data . upload_metadata ,
139+ upload_defer_length : data . upload_defer_length ,
140+ } ,
141+ } ,
142+ } ;
123143
124- const options = {
125- offset,
126- } ;
144+ const write_stream = file . createWriteStream ( options ) ;
145+ if ( ! write_stream ) {
146+ return reject ( ERRORS . FILE_WRITE_ERROR ) ;
147+ }
127148
128- const write_stream = file . createWriteStream ( options ) ;
129- if ( ! write_stream ) {
130- return reject ( ERRORS . FILE_WRITE_ERROR ) ;
131- }
149+ let new_offset = 0 ;
150+ req . on ( 'data' , ( buffer ) => {
151+ new_offset += buffer . length ;
152+ } ) ;
132153
133- let new_offset = 0 ;
134- req . on ( 'data' , ( buffer ) => {
135- new_offset += buffer . length ;
136- } ) ;
154+ req . on ( 'end' , ( ) => {
155+ console . log ( ` ${ new_offset } bytes written` ) ;
156+ resolve ( new_offset ) ;
157+ } ) ;
137158
138- req . on ( 'end ' , ( ) => {
139- console . log ( ` ${ new_offset } bytes written` ) ;
140- resolve ( new_offset ) ;
141- } ) ;
159+ write_stream . on ( 'error ' , ( e ) => {
160+ console . log ( e ) ;
161+ reject ( ERRORS . FILE_WRITE_ERROR ) ;
162+ } ) ;
142163
143- write_stream . on ( 'error' , ( e ) => {
144- console . log ( e ) ;
145- reject ( ERRORS . FILE_WRITE_ERROR ) ;
164+ return req . pipe ( write_stream ) ;
146165 } ) ;
147-
148- return req . pipe ( write_stream ) ;
149166 } ) ;
150167 }
151168
152169 /**
153- * [getOffset description]
154- * @param {[type] } file_id [description]
155- * @return {[type] } [description]
170+ * Get file metadata from the GCS Object.
171+ *
172+ * @param {string } file_id name of the file
173+ * @return {object }
156174 */
157175 getOffset ( file_id ) {
158176 return new Promise ( ( resolve , reject ) => {
@@ -166,6 +184,7 @@ class GCSDataStore extends DataStore {
166184 console . warn ( '[GCSDataStore] getFileMetadata' , error ) ;
167185 return reject ( error ) ;
168186 }
187+
169188 const data = {
170189 size : parseInt ( metadata . size , 10 ) ,
171190 } ;
0 commit comments