Files
wekan/server/lib/importAttachmentStream.js
T
Lauri Ojansivu 73db241bd4 Choose who an import's people become, for every import, and stream imported attachments.
Every import page now asks once what the people named in the file become
(models/lib/importMembersMode.js): existing users chosen in the map-members
step, placeholder users that keep the original username and name and cannot
log in, to be mapped to real users later, or all the person importing. All
five creators read the same mapping. The generalized importer, which used to
drop people nobody mapped, now makes placeholders for them, as Trello and
WeKan imports do, and offers its people for mapping by reading the file with
the same parser (importBoard with previewPeople, which creates nothing).
Import many boards offers placeholders or the importer. The REST import, the
WeKan .zip route and the Trello route take the mode too.

A WeKan .zip imported as a new board no longer puts its attachment files
inline in memory within a 512 MB budget: the full-board importer streams each
from the archive into storage, as the import into an existing board does.
Both, and the Trello importer when given a stream, go through one helper
(server/lib/importAttachmentStream.js) that holds every imported attachment
to the Admin Panel's upload limit and to none when none is set, skipping a
file whose stated size is over it and stopping one that grows past it.

The import page instructions name Jira's current search (GET
/rest/api/3/search/jql with fields=*all, paged by nextPageToken), Todoist's
Manage data > Export as CSV and Planner's Export as Excel.

Thanks to xet7 !
2026-10-09 12:20:36 +03:00

62 lines
2.5 KiB
JavaScript

// Attachments brought in by an import - a WeKan .zip, a Trello .zip - are
// streamed into the default storage the Admin Panel configures, one at a time,
// and never held whole in memory, whatever their size. The only size limit is
// the one an administrator set for uploads (Admin Panel > Attachments >
// Limits, or ATTACHMENTS_UPLOAD_MAX_SIZE): an import is an upload too. With no
// limit set, there is none.
const { Transform } = require('stream');
// The upload limit in bytes; 0 means none.
async function importAttachmentLimit() {
try {
const { getAttachmentUploadMaxBytes } = require('/models/attachments.server');
const limit = await getAttachmentUploadMaxBytes();
return Number.isFinite(limit) && limit > 0 ? limit : 0;
} catch (e) {
return 0;
}
}
// `source` piped through a counter that fails the stream once more than `max`
// bytes have passed, so a file over the limit is never written in full.
function limitStream(source, max) {
if (!max) return source;
let total = 0;
const guard = new Transform({
transform(chunk, encoding, done) {
total += chunk.length;
if (total > max) done(new Error('import-attachment-too-large'));
else done(null, chunk);
},
});
source.on('error', error => guard.destroy(error));
return source.pipe(guard);
}
// Stream one imported attachment into storage. `declaredSize`, when the
// archive states it, lets a file over the limit be skipped before it is read.
// Returns the new file's reference, or { skipped: reason }.
async function writeImportedAttachment(stream, { fileName, type, meta, userId, declaredSize }) {
const max = await importAttachmentLimit();
if (max && Number.isFinite(declaredSize) && declaredSize > max) {
try { stream.destroy(); } catch (e) { /* not opened */ }
return { skipped: 'larger than the attachment upload limit' };
}
const { addAttachmentFromStream } = require('/models/lib/fileStoreStrategy');
const { fileStoreStrategyFactory } = require('/models/attachments.server');
try {
return await addAttachmentFromStream(limitStream(stream, max), {
fileName: fileName || 'attachment',
type: type || 'application/octet-stream',
userId,
size: Number.isFinite(declaredSize) ? declaredSize : undefined,
meta,
}, fileStoreStrategyFactory);
} catch (error) {
if (error && error.message === 'import-attachment-too-large') return { skipped: 'larger than the attachment upload limit' };
throw error;
}
}
module.exports = { importAttachmentLimit, limitStream, writeImportedAttachment };