Skip to content
This repository has been archived by the owner on Aug 6, 2018. It is now read-only.

Commit

Permalink
File writing successful.
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgerh committed May 6, 2016
1 parent 751fdde commit 4f1795b
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions server/controllers/google/get.js
Original file line number Diff line number Diff line change
@@ -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']
}

/**
Expand All @@ -29,15 +29,15 @@ 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`, {
auth: {
bearer: self.session.google.token
},
qs: {
mimeType: exportMimeType
mimeType: fileType[0]
}
}, function (err, res, body) {
if (err) {
Expand All @@ -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'))

This comment has been minimized.

Copy link
@kvz

kvz May 6, 2016

Member

@hedgerh I think the cb should only be called after the writing is done. I think currently it is called right after we received the first chunk (?)

} else {
// Fetch non-Google files
google.get(`files/${self.query.fileId}`, {
google.get(`files/${file.id}`, {
auth: {
bearer: self.session.google.token
},
Expand All @@ -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'))
}
})
}
Expand Down

0 comments on commit 4f1795b

Please sign in to comment.