Created
January 23, 2015 16:28
-
-
Save paulklemm/36c9b7b1701016ddc03d to your computer and use it in GitHub Desktop.
Flow.js Node Server renames the uploaded file but cannot emit the changed file name to the Javascript client.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.env.TMPDIR = 'tmp'; // to avoid the EXDEV rename error, see http://stackoverflow.com/q/21071303/76173 | |
var UPLOAD_DIR = 'uploads/'; | |
var express = require('express'); | |
var multipart = require('connect-multiparty'); | |
var multipartMiddleware = multipart(); | |
var flow = require('./flow-node.js')('tmp'); | |
var fs = require('fs'); | |
var crypto = require('crypto'); | |
var path = require('path'); | |
var app = express(); | |
// Configure access control allow origin header stuff | |
var ACCESS_CONTROLL_ALLOW_ORIGIN = false; | |
// Host most stuff in the public folder | |
app.use(express.static(__dirname + '/../prototype')); | |
app.use(express.static(__dirname + '/uploads')); | |
var getHashFromFile = function(filepath, filename, callback) { | |
var completePath = filepath + filename; | |
// Read the file again and create a hash for it | |
var shasum = crypto.createHash('sha256'); | |
var reader = fs.ReadStream(completePath); | |
reader.on('data', function(hash) { | |
shasum.update(hash); | |
}); | |
reader.on('end', function() { | |
var hash = shasum.digest('hex'); | |
callback(hash); | |
}); | |
} | |
app.post('/upload', multipartMiddleware, function(req, res) { | |
flow.post(req, function(status, filename, original_filename, identifier) { | |
if (status == 'done') { | |
var filepath = UPLOAD_DIR + filename; | |
var stream = fs.createWriteStream(filepath); | |
console.log(identifier); | |
flow.write(identifier, stream); | |
flow.clean(identifier); | |
// Here I calculate a hash based on the uploaded file and rename it accordingly. | |
getHashFromFile(UPLOAD_DIR, filename, function(hash){ | |
var extension = path.extname(filename); | |
fs.rename(UPLOAD_DIR + filename, UPLOAD_DIR + hash + extension, function(err) { | |
if ( err ) console.log('ERROR: ' + err); | |
}); | |
}); | |
} | |
if (ACCESS_CONTROLL_ALLOW_ORIGIN) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
} | |
res.status(status).send(); | |
}); | |
}); | |
app.options('/upload', function(req, res){ | |
console.log('OPTIONS'); | |
if (ACCESS_CONTROLL_ALLOW_ORIGIN) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
} | |
res.status(200).send(); | |
}); | |
// Handle status checks on chunks through Flow.js | |
app.get('/upload', function(req, res) { | |
flow.get(req, function(status, filename, original_filename, identifier) { | |
console.log('GET', status); | |
console.log('GET', filename); | |
console.log('GET', original_filename); | |
if (ACCESS_CONTROLL_ALLOW_ORIGIN) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
} | |
if (status == 'found') { | |
status = 200; | |
} else { | |
status = 404; | |
} | |
res.status(status).send(); | |
}); | |
}); | |
app.get('/download/:identifier', function(req, res) { | |
flow.write(req.params.identifier, res); | |
}); | |
var port = 8888; | |
console.log("Started listening on Port " + port); | |
app.listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment