|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const DataStore = require('./DataStore'); |
| 4 | +const File = require('../models/File'); |
| 5 | +const gcloud = require('gcloud'); |
| 6 | +const assign = require('object-assign'); |
| 7 | +const stream = require('stream'); |
| 8 | +const ERRORS = require('../constants').ERRORS; |
| 9 | +const TUS_RESUMABLE = require('../constants').TUS_RESUMABLE; |
| 10 | +const DEFAULT_CONFIG = { |
| 11 | + scopes: ['https://www.googleapis.com/auth/devstorage.full_control'], |
| 12 | +}; |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * @fileOverview |
| 17 | + * Store using local filesystem. |
| 18 | + * |
| 19 | + * @author Ben Stahl <[email protected]> |
| 20 | + */ |
| 21 | + |
| 22 | +class GCSDataStore extends DataStore { |
| 23 | + constructor(options) { |
| 24 | + super(options); |
| 25 | + this.extensions = ['creation', 'creation-defer-length']; |
| 26 | + |
| 27 | + if (!options.bucket) { |
| 28 | + throw new Error('GCSDataStore must have a bucket'); |
| 29 | + } |
| 30 | + this.bucket_name = options.bucket; |
| 31 | + this.gcs = gcloud.storage({ |
| 32 | + projectId: options.projectId, |
| 33 | + keyFilename: options.keyFilename, |
| 34 | + }); |
| 35 | + this.bucket = this._getBucket(); |
| 36 | + |
| 37 | + this.authConfig = assign(DEFAULT_CONFIG, { |
| 38 | + keyFilename: options.keyFilename, |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Check the bucket exists in GCS. |
| 44 | + * |
| 45 | + * @return {[type]} [description] |
| 46 | + */ |
| 47 | + _getBucket() { |
| 48 | + const bucket = this.gcs.bucket(this.bucket_name); |
| 49 | + bucket.exists((error, exists) => { |
| 50 | + if (error) { |
| 51 | + console.warn(error); |
| 52 | + throw new Error(`[GCSDataStore] _getBucket: ${error.message}`); |
| 53 | + } |
| 54 | + |
| 55 | + if (!exists) { |
| 56 | + throw new Error(`[GCSDataStore] _getBucket: ${this.bucket_name} bucket does not exist`); |
| 57 | + } |
| 58 | + |
| 59 | + }); |
| 60 | + |
| 61 | + return bucket; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 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} |
| 70 | + */ |
| 71 | + create(req) { |
| 72 | + return new Promise((resolve, reject) => { |
| 73 | + const upload_length = req.headers['upload-length']; |
| 74 | + const upload_defer_length = req.headers['upload-defer-length']; |
| 75 | + const upload_metadata = req.headers['upload-metadata']; |
| 76 | + |
| 77 | + if (upload_length === undefined && upload_defer_length === undefined) { |
| 78 | + reject(ERRORS.INVALID_LENGTH); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + let file_id; |
| 83 | + try { |
| 84 | + file_id = this.generateFileName(req); |
| 85 | + } |
| 86 | + catch (generateError) { |
| 87 | + console.warn('[FileStore] create: check your namingFunction. Error', generateError); |
| 88 | + reject(ERRORS.FILE_WRITE_ERROR); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const file = new File(file_id, upload_length, upload_defer_length, upload_metadata); |
| 93 | + const gcs_file = this.bucket.file(file.id); |
| 94 | + const options = { |
| 95 | + metadata: { |
| 96 | + metadata: { |
| 97 | + upload_length: file.upload_length, |
| 98 | + tus_version: TUS_RESUMABLE, |
| 99 | + upload_metadata, |
| 100 | + upload_defer_length, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }; |
| 104 | + |
| 105 | + const fake_stream = new stream.PassThrough(); |
| 106 | + fake_stream.end(); |
| 107 | + fake_stream.pipe(gcs_file.createWriteStream(options)) |
| 108 | + .on('error', reject) |
| 109 | + .on('finish', () => { |
| 110 | + resolve(file); |
| 111 | + }); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 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} |
| 123 | + */ |
| 124 | + write(req, file_id, offset) { |
| 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 | + }; |
| 143 | + |
| 144 | + const write_stream = file.createWriteStream(options); |
| 145 | + if (!write_stream) { |
| 146 | + return reject(ERRORS.FILE_WRITE_ERROR); |
| 147 | + } |
| 148 | + |
| 149 | + let new_offset = 0; |
| 150 | + req.on('data', (buffer) => { |
| 151 | + new_offset += buffer.length; |
| 152 | + }); |
| 153 | + |
| 154 | + req.on('end', () => { |
| 155 | + console.log(`${new_offset} bytes written`); |
| 156 | + resolve(new_offset); |
| 157 | + }); |
| 158 | + |
| 159 | + write_stream.on('error', (e) => { |
| 160 | + console.log(e); |
| 161 | + reject(ERRORS.FILE_WRITE_ERROR); |
| 162 | + }); |
| 163 | + |
| 164 | + return req.pipe(write_stream); |
| 165 | + }); |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Get file metadata from the GCS Object. |
| 171 | + * |
| 172 | + * @param {string} file_id name of the file |
| 173 | + * @return {object} |
| 174 | + */ |
| 175 | + getOffset(file_id) { |
| 176 | + return new Promise((resolve, reject) => { |
| 177 | + const file = this.bucket.file(file_id); |
| 178 | + file.getMetadata((error, metadata, apiResponse) => { |
| 179 | + if (error && error.message === 'Not Found') { |
| 180 | + return reject(ERRORS.FILE_NOT_FOUND); |
| 181 | + } |
| 182 | + |
| 183 | + if (error) { |
| 184 | + console.warn('[GCSDataStore] getFileMetadata', error); |
| 185 | + return reject(error); |
| 186 | + } |
| 187 | + |
| 188 | + const data = { |
| 189 | + size: parseInt(metadata.size, 10), |
| 190 | + }; |
| 191 | + |
| 192 | + if (!('metadata' in metadata)) { |
| 193 | + return resolve(data); |
| 194 | + } |
| 195 | + |
| 196 | + if (metadata.metadata.upload_length) { |
| 197 | + data.upload_length = parseInt(metadata.metadata.upload_length, 10); |
| 198 | + } |
| 199 | + |
| 200 | + if (metadata.metadata.upload_defer_length) { |
| 201 | + data.upload_defer_length = parseInt(metadata.metadata.upload_defer_length, 10); |
| 202 | + } |
| 203 | + |
| 204 | + if (metadata.metadata.upload_metadata) { |
| 205 | + data.upload_metadata = metadata.metadata.upload_metadata; |
| 206 | + } |
| 207 | + |
| 208 | + return resolve(data); |
| 209 | + }); |
| 210 | + }); |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +module.exports = GCSDataStore; |
0 commit comments