Skip to content

Commit 5d27391

Browse files
author
Ben Stahl
committed
GCS doesn't keep metadata for new object versions
1 parent a97d81e commit 5d27391

File tree

2 files changed

+55
-42
lines changed

2 files changed

+55
-42
lines changed

lib/stores/GCSDataStore.js

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

test/Test-EndToEnd.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ describe('EndToEnd', () => {
7070
it('should 404 file ids that dont exist', (done) => {
7171
agent.head(`${STORE_PATH}/${file_id}`)
7272
.set('Tus-Resumable', TUS_RESUMABLE)
73-
.set('Upload-Length', TEST_FILE_SIZE)
74-
.set('Upload-Metadata', TEST_METADATA)
75-
.set('Tus-Resumable', TUS_RESUMABLE)
7673
.expect(404)
7774
.expect('Tus-Resumable', TUS_RESUMABLE)
7875
.end(done);
@@ -246,9 +243,6 @@ describe('EndToEnd', () => {
246243
it('should 404 file ids that dont exist', (done) => {
247244
agent.head(`${STORE_PATH}/${file_id}`)
248245
.set('Tus-Resumable', TUS_RESUMABLE)
249-
.set('Upload-Length', TEST_FILE_SIZE)
250-
.set('Upload-Metadata', TEST_METADATA)
251-
.set('Tus-Resumable', TUS_RESUMABLE)
252246
.expect(404)
253247
.expect('Tus-Resumable', TUS_RESUMABLE)
254248
.end(done);
@@ -284,7 +278,7 @@ describe('EndToEnd', () => {
284278
assert.equal(res.headers['tus-resumable'], TUS_RESUMABLE);
285279
// Save the id for subsequent tests
286280
file_id = res.headers.location.split('/').pop();
287-
files_created.push(file_id.split('&upload_id')[0])
281+
files_created.push(file_id.split('&upload_id')[0]);
288282
done();
289283
});
290284
});
@@ -301,7 +295,7 @@ describe('EndToEnd', () => {
301295
assert.equal(res.headers['tus-resumable'], TUS_RESUMABLE);
302296
// Save the id for subsequent tests
303297
deferred_file_id = res.headers.location.split('/').pop();
304-
files_created.push(deferred_file_id.split('&upload_id')[0])
298+
files_created.push(deferred_file_id.split('&upload_id')[0]);
305299
done();
306300
});
307301
});
@@ -387,7 +381,7 @@ describe('EndToEnd', () => {
387381
this.timeout(0);
388382

389383
// GCS need a few seconds before it can show the changes
390-
const TIMEOUT = 10000;
384+
const TIMEOUT = 5000;
391385
console.log(`Pausing for ${TIMEOUT / 1000} seconds while GCS updates...`);
392386
setTimeout(() => {
393387
done();

0 commit comments

Comments
 (0)