Created
August 8, 2014 19:08
-
-
Save kingsumos/87a6a3fb2f917854d354 to your computer and use it in GitHub Desktop.
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
/* | |
The MIT License (MIT) | |
Copyright (c) 2014 Evandro Luiz Hauenstein | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
var restify = require('restify'), | |
server = restify.createServer({ name: 'file-cache-api' }); | |
var fs = require('fs'); | |
var mime = require('mime'); | |
var glob = require('glob'); | |
var util = require('util'); | |
fs.mkdir('./files/', function(){}); | |
var EXPIRES = 60*60*24*3; // 3 days | |
var Log = function() { | |
var date = new Date(); | |
process.stdout.write(date.toISOString() + ' ' + util.format.apply(this, arguments) + '\n'); | |
} | |
var process_req = function (req, res, next) { | |
Log('REQUEST: ' + req.method + ', ID: ' + req.params.id); | |
glob('./files/' + req.params.id + '-*', {}, function(err, filelist) { | |
if (err) throw err; | |
if (filelist[0]) { | |
// just in case we got more than one result, pick the latest one (based on expire time) | |
filelist.sort(); | |
var idx = filelist.length-1; | |
// parsing (get id, expire and filename) | |
var match = /.*\/(.*)-([0-9]+)-(.*)/.exec(filelist[idx]); | |
if (match) { | |
var id = match[1]; | |
var expire = match[2]; | |
var filename = match[3]; | |
var mimetype = mime.lookup(filelist[idx]); | |
if (expire > (Math.floor(Date.now() / 1000))) { | |
res.setHeader('Content-disposition', 'attachment; filename=' + filename); | |
res.setHeader('Content-type', mimetype); | |
var filestream = fs.createReadStream(filelist[idx]); | |
filestream.pipe(res); | |
return; | |
} | |
Log('the requested file is expired', filelist[idx]); | |
res.send(404); | |
return; | |
} | |
} | |
Log('the requested file is not available'); | |
res.send(404); | |
}); | |
} | |
server.get('/get/:id', process_req); | |
server.head('/get/:id', process_req); | |
server.post('set/:id', restify.bodyParser({uploadDir: './files'}), function (req, res, next) { | |
Log('REQUEST: ' + req.method + ', ID: ' + req.params.id + ', FILE: ' + req.files.data.name); | |
if( req.files && req.files.data && req.files.data.path ) { | |
fs.rename(req.files.data.path, './files/' + req.params.id + '-' + (Math.floor(Date.now() / 1000) + EXPIRES) + '-' + req.files.data.name, function (err) { | |
if (err) throw err; | |
}); | |
} | |
res.end(); | |
}) | |
setInterval(function() { | |
glob('./files/*', {}, function(err, filelist) { | |
if (err) throw err; | |
if (filelist[0]) { | |
filelist.sort(); | |
for(idx in filelist) { | |
var match = /.*\/(.*)-([0-9]+)-(.*)/.exec(filelist[idx]); | |
if (match) { | |
var id = match[1]; | |
var expire = match[2]; | |
var filename = match[3]; | |
if (expire <= (Math.floor(Date.now() / 1000))) { | |
Log('EXPIRED', filelist[idx]); | |
fs.unlink(filelist[idx]); | |
} | |
} | |
} | |
} | |
}); | |
}, 300000) // 5 minutes | |
server.listen(3000, '0.0.0.0', function () { | |
Log('%s listening at %s', server.name, server.url) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Node.js + restify upload/download API example.
Demonstration of how to upload files to restify service, files will saved using the following format: "fileid-timestamp-filename" (timestamps are used for the expiration mechanism, files are deleted past 3 days)
To upload files use http://127.0.0.1:3000/set/[fileid], to download use: http://127.0.0.1:3000/get/[fileid]
For instance, uploading files (e.g. test.bin) using curl:
curl -v -include --form data=@test.bin 'http://127.0.0.1:3000/set/12345' --noproxy 127.0.0.1
downloading:
wget --content-disposition 'http://127.0.0.1:3000/get/12345' --no-proxy