Files
wekan/server/routes/importUpload.js
T
Lauri Ojansivu 32c24ed84e Import page uploads stream to the server's disk, checked against the free space and an Admin Panel limit.
A file chosen on the Import page for the generalized importer's sources
is POSTed to /api/import/upload/:source and written to
WRITABLE_PATH/import-uploads a part at a time, instead of being read in
the browser and sent whole, as text or base64, through importBoard. The
size the browser announces is checked before the first byte against the
largest import set in Admin Panel / Settings / Security (none: no limit)
and against the free space of that disk less a reserve, and the space is
checked again every 64 MB while it is written; a refused or interrupted
upload leaves no file behind. importBoard then reads the file by its
id: a .zip export (Slack, Google Takeout, Vikunja, Notion, Plane) is
opened from the disk, its entries inflating up to the limit or the free
space, and a mailbox is read a message at a time, also inside a Takeout
zip. The upload is removed once read, or after an hour.

The HTML-only mail text is read through the parsed document rather than
stripped with a pattern. TODO.md says what is left of large files, and
which locale files hold another language.

Thanks to xet7 !
2026-11-20 05:42:27 +02:00

45 lines
2.3 KiB
JavaScript

// POST /api/import/upload/:source - the Import page's chosen file, streamed to
// the local disk a part at a time (server/lib/importUpload.js) instead of
// being sent whole, as text or base64, through the importBoard method. The
// answer is { uploadId, size }; importBoard then reads the file by that id
// (data.uploadId). The body is the file's bytes (application/octet-stream),
// which the JSON body parser leaves alone.
import { WebApp } from 'meteor/webapp';
import { Meteor } from 'meteor/meteor';
import { getUserIdFromRequest } from '/server/lib/requestUser';
import { assertImportEnabled } from '/models/lib/importExportSecurity';
import { importSource } from '/models/lib/importSources';
const { receiveUpload, sweepUploads } = require('/server/lib/importUpload');
const { importLimitBytes } = require('/server/lib/importDiskSpace');
Meteor.startup(() => {
// Expired uploads, and any a stopped server left, go once an hour.
Meteor.setInterval(() => { sweepUploads().catch(() => {}); }, 60 * 60 * 1000);
sweepUploads().catch(() => {});
WebApp.handlers.use('/api/import/upload', async (req, res) => {
const send = (code, body) => {
if (res.headersSent) return;
res.writeHead(code, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(body));
};
if (req.method !== 'POST') return send(405, { error: 'method-not-allowed' });
const source = String(req.url || '').replace(/^\/+/, '').split(/[/?#]/)[0];
if (!importSource(source)) return send(404, { error: 'invalid-import-source' });
const userId = await getUserIdFromRequest(req);
if (!userId) return send(401, { error: 'error-notAuthorized' });
try { await assertImportEnabled(); } catch (error) { return send(403, { error: error.error || 'import-disabled' }); }
const Settings = require('/models/settings').default;
const limit = importLimitBytes(await Settings.findOneAsync({}, { fields: { importMaxMegabytes: 1 } }));
try {
const { uploadId, size } = await receiveUpload(req, { userId, source, limit });
return send(200, { uploadId, size });
} catch (error) {
const code = error && error.code;
const status = code === 'import-too-large' ? 413 : code === 'import-disk-full' ? 507 : 400;
return send(status, { error: code || 'import-upload-failed', reason: error && error.message });
}
});
});