ForgeTotal.js 5
en

Total.js 5

Store uploaded files with FILESTORAGE

Total.js 5 · total5@0.0.19 filesuploadstorage Executed against Total.js 5

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.

config
fs : files
controllers/files.js
exports.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);
}