Store uploaded files with FILESTORAGE
FILESTORAGE(name) is a file store: files are saved under an id (use UID()) and served back by that id. Receive the upload with @upload, save it with file.fs(storage, id, callback) and answer downloads with $.filefs(storage, id), which sends the stored file with its content-type.
The storage name comes from the config file, so it can change per environment.
fs : filesexports.install = function() {
ROUTE('POST /upload/ @upload <5mb', upload);
ROUTE('FILE /downloads/*.*', download);
};
function upload($) {
var file = $.files[0];
if (!file) {
$.invalid('No file');
return;
}
var id = UID();
file.fs(CONF.fs, id, function(err) {
if (err)
$.invalid(err);
else
$.json({ id: id, url: '/downloads/' + id + '.' + file.extension, filename: file.filename, size: file.size });
});
}
function download($) {
// /downloads/<id>.<extension>
var name = $.split[1];
var id = name.substring(0, name.lastIndexOf('.'));
$.filefs(CONF.fs, id);
}