mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
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 !
65 lines
3.3 KiB
JavaScript
65 lines
3.3 KiB
JavaScript
'use strict';
|
|
|
|
// Imported attachments stream into storage, never held whole in memory,
|
|
// within only the Admin Panel's upload limit (server/lib/importAttachmentStream.js).
|
|
// Run: node tests/importAttachmentStream.test.cjs
|
|
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const { Readable } = require('node:stream');
|
|
const read = file => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
|
const { limitStream } = require('../server/lib/importAttachmentStream');
|
|
|
|
const collect = stream => new Promise((resolve, reject) => {
|
|
const chunks = [];
|
|
stream.on('data', chunk => chunks.push(chunk));
|
|
stream.on('end', () => resolve(Buffer.concat(chunks)));
|
|
stream.on('error', reject);
|
|
});
|
|
|
|
async function main() {
|
|
let passed = 0;
|
|
const test = async (name, fn) => { await fn(); passed += 1; console.log(' ok -', name); };
|
|
|
|
await test('with no limit the stream passes through unchanged, however large', async () => {
|
|
const source = Readable.from([Buffer.alloc(1024, 1), Buffer.alloc(4096, 2)]);
|
|
assert.equal(limitStream(source, 0), source);
|
|
assert.equal((await collect(limitStream(Readable.from([Buffer.alloc(10)]), 0))).length, 10);
|
|
});
|
|
|
|
await test('within the limit every byte arrives', async () => {
|
|
const out = await collect(limitStream(Readable.from([Buffer.alloc(600), Buffer.alloc(400)]), 1000));
|
|
assert.equal(out.length, 1000);
|
|
});
|
|
|
|
await test('negative: past the limit the stream fails before the rest is read', async () => {
|
|
let produced = 0;
|
|
// 64 KB chunks, a 100 KB limit, 200 chunks on offer: reading stops after a
|
|
// few, not after all 12.5 MB.
|
|
const source = new Readable({ read() { produced += 1; this.push(produced > 200 ? null : Buffer.alloc(64 * 1024)); } });
|
|
await assert.rejects(collect(limitStream(source, 100 * 1024)), /import-attachment-too-large/);
|
|
assert.ok(produced < 20, `stopped early (${produced} chunks read)`);
|
|
});
|
|
|
|
await test('every import writer streams through it, with the Admin Panel limit', async () => {
|
|
const helper = read('server/lib/importAttachmentStream.js');
|
|
assert.match(helper, /const \{ getAttachmentUploadMaxBytes \} = require\('\/models\/attachments\.server'\);/);
|
|
assert.match(helper, /if \(max && Number\.isFinite\(declaredSize\) && declaredSize > max\) \{/);
|
|
assert.match(read('models/attachments.server.js'), /^export async function getAttachmentUploadMaxBytes\(\) \{/m);
|
|
for (const file of ['models/wekanCreator.js', 'models/trelloCreator.js', 'models/server/scopedImporter.js']) {
|
|
assert.match(read(file), /require\('\/server\/lib\/importAttachmentStream'\)/, file);
|
|
}
|
|
const wekan = read('models/wekanCreator.js');
|
|
assert.match(wekan, /const stream = !att\.file && this\.attachmentStream \? this\.attachmentStream\(att\) : null;/);
|
|
assert.match(wekan, /const stream = !bg\.file && this\.attachmentStream \? this\.attachmentStream\(bg\) : null;/, 'board backgrounds too');
|
|
const zip = read('models/importZip.js');
|
|
assert.match(zip, /creator\.attachmentStream = attachmentStream;/);
|
|
assert.doesNotMatch(zip, /attachment\.file = bytes\.toString\('base64'\)/, 'negative: no zip attachment is put inline');
|
|
});
|
|
|
|
console.log(`\nimportAttachmentStream: ${passed} checks passed`);
|
|
}
|
|
|
|
main().catch(error => { console.error(error); process.exit(1); });
|