diff --git a/server/controllers/google/get.js b/server/controllers/google/get.js index f0ab56c..8c94cc2 100644 --- a/server/controllers/google/get.js +++ b/server/controllers/google/get.js @@ -1,9 +1,9 @@ var fs = require('fs') - +var path = require('path') var fileTypes = { - 'document': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', - 'presentation': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', - 'spreadsheet': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' + 'document': ['application/vnd.openxmlformats-officedocument.wordprocessingml.document', '.docx'], + 'presentation': ['application/vnd.openxmlformats-officedocument.presentationml.presentation', '.pptx'], + 'spreadsheet': ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', '.xlsx'] } /** @@ -29,7 +29,7 @@ module.exports = function * (next) { } // If file is Google document, need to download exported Office doc if (file.mimeType.indexOf('application/vnd.google-apps.') !== -1) { - var exportMimeType = fileTypes[file.mimeType.replace('application/vnd.google-apps.', '')] + var fileType = fileTypes[file.mimeType.replace('application/vnd.google-apps.', '')] // Pass mimeType of desired file type to export google.get(`files/${self.query.fileId}/export`, { @@ -37,7 +37,7 @@ module.exports = function * (next) { bearer: self.session.google.token }, qs: { - mimeType: exportMimeType + mimeType: fileType[0] } }, function (err, res, body) { if (err) { @@ -46,19 +46,13 @@ module.exports = function * (next) { return cb() } - fs.writeFile(`./output/${self.query.fileId}`, body, {encoding: 'binary'}, function (err, res) { - if (err) { - console.log(err) - self.body = err - } - self.body = 'ok' - self.status = 200 - cb() - }) - }) + self.body = 'ok' + self.status = 200 + cb() + }).pipe(fs.createWriteStream('./output/' + file.title + fileType[1] || 'cat.png')) } else { // Fetch non-Google files - google.get(`files/${self.query.fileId}`, { + google.get(`files/${file.id}`, { auth: { bearer: self.session.google.token }, @@ -71,17 +65,12 @@ module.exports = function * (next) { self.body = 'Error: ' + err return cb() } - fs.writeFile(`./output/${file.title}`, body, {encoding: 'binary'}, function (err, res) { - if (err) { - console.log(err) - self.body = err - } - console.log('we did it') - self.body = 'ok' - self.status = 200 - cb() - }) - }) + + console.log('we did it') + self.body = 'ok' + self.status = 200 + cb() + }).pipe(fs.createWriteStream('./output/' + file.title || 'cat.png')) } }) }