Own uploaded-file cleanup and build query links
Uploaded temporary files normally disappear when the request is released. $.autoclear(false) keeps them for work that outlives the response: your application must then move or remove them. $.clear() schedules removal of removable uploads and empties $.files; disk deletion is asynchronous. Never store arbitrary paths supplied by a client for later deletion.
In 0.0.19 build query links with the public QUERIFY() helper and a copy of the query, as this recipe shows. It produces a query string, not a redirect or an absolute URL.
The retained-file map and cleanup endpoints below are a local fixture; an application should use scoped ownership and durable storage rather than expose these endpoints.
var paths = Object.create(null);
exports.install = function() {
ROUTE('POST /upload/{mode}/ @upload <1mb', function($) {
var file = $.files[0];
if (!file) return $.invalid(400);
var id = UID();
paths[id] = file.path;
if ($.params.mode === 'retain') $.autoclear(false);
if ($.params.mode === 'clear') $.clear();
$.json({ id: id, files: $.files.length });
});
ROUTE('GET /exists/{id}/', $ => $.json({ exists: !!paths[$.params.id] && F.Fs.existsSync(paths[$.params.id]) }));
ROUTE('POST /cleanup/{id}/', function($) {
var path = paths[$.params.id];
if (path) { F.Fs.rmSync(path, { force: true }); delete paths[$.params.id]; }
$.success();
});
ROUTE('GET /link/', function($) {
var broken = false;
try { $.href('page', '2'); } catch (e) { broken = e instanceof TypeError; }
$.json({ broken: broken, link: QUERIFY('', Object.assign({}, $.query, { page: '2 & more' })) });
});
};