Skip to content

Instantly share code, notes, and snippets.

@kmaher9
Created July 3, 2019 20:45
Show Gist options
  • Save kmaher9/d6d86e0a6e31a0d9fc96ea7f32bb978e to your computer and use it in GitHub Desktop.
Save kmaher9/d6d86e0a6e31a0d9fc96ea7f32bb978e to your computer and use it in GitHub Desktop.
const File = require('../models/file')
const Busboy = require('busboy')
const fs = require('fs')
exports.newFile = function (request, response, next) {
var busboy = new Busboy({ headers: request.headers })
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
if (fs.existsSync(`../datum/${filename}`)) {
return response.status(400).json({
"data": {
"error": "file already exists"
}
})
} else {
File.create({
"name": filename,
"location": `../datum/${filename}`,
mime: mimetype
})
const storeLocation = `../datum/${filename}`
const stream = fs.createWriteStream(storeLocation)
file.pipe(stream)
}
})
busboy.on('finish', function() {
response.status(200).json({
"data": {
"message": "upload successful"
}
})
})
request.pipe(busboy)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment