mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Azure DevOps Boards: a query's "Export to CSV" is imported (Board Column or State as lists, Board Lane as swimlanes, Title 1/2/3 tree levels as parent cards, "Name <address>" people, regional dates, other columns as custom fields), and a board exports as the CSV "Import from CSV" reads. Microsoft Lists: a list's "Export to CSV" is imported (Status or Progress as the lists, people, dates, tags, other columns as custom fields), and a board exports as a CSV that "New list - From CSV" makes a list of. Microsoft Project: the "XML Format" (MSPDI) plan is imported (summary tasks as swimlanes, progress as lists, subtasks, resources, predecessors), read by a small XML reader that never resolves a DTD or an entity, and a board exports as a plan Project opens. Outlook: .msg files are read without a library - the Compound File Binary format (MS-CFB) and the message's properties (MS-OXMSG) - into the mail import, threads and attachments included, alone or in a .zip with .eml files; a board exports as .eml files Outlook opens. The reader was checked against .msg files saved by Outlook. Teams and OneNote: fetched from Microsoft Graph with the member's token (chats and channels; notebooks with their pages' HTML, to-do boxes as a checklist), or read from a file - Teams' personal export .tar, Graph chat messages, and the files WeKan exports: Teams chats as Graph chat messages, OneNote pages as Graph's create-page XHTML in a .zip. All six are under the Microsoft heading of Import and Export, with tests, negative tests, live tests for the two Graph imports, documentation and the import instruction in every language. Low confidence translations include ace, ak, ay, bm, bo, bua, chr, csb, cv, dz, ee, ff, fj, gn, gv, haw, ig, iu, kl, ks, kok, kw, ht, hsb, fur, fo, ga, gd, ku, lld, nah, nap, rup, sah, sc, scn, se, nd, nso, ss, om, rn, rw, sm, sn, qu, pap, lg, mg, mi, ny, so, mai, sd, ps, or_IN, my, si, rm, la, lb, tig, tlh, uz-AR, ve, ve-PP, ve-CC, vo, wa, wal, wo, zgh, to, tpi, wa-RR and ts. Thanks to xet7 !
95 lines
4.4 KiB
JavaScript
95 lines
4.4 KiB
JavaScript
'use strict';
|
|
// Server-only: what importBoard reads for an Import page upload that was
|
|
// streamed to the local disk (server/lib/importUpload.js), by the source's
|
|
// own way of reading (models/lib/importSources.js):
|
|
// a .zip that is one export of the tool -> { zipFile, maxBytes, inflateLimit }
|
|
// (opened from disk, never whole)
|
|
// mail -> { mboxFile } (a message at a time)
|
|
// a binary file (.msg, .tar) -> { binaryFile }
|
|
// a workbook -> { excelBase64 } / { xlsxBase64 }
|
|
// JSON -> the parsed document
|
|
// text -> the text
|
|
// A zip's entries may inflate to the administrator's limit, or with none to
|
|
// the free disk space - what unpacking it there could take.
|
|
const fs = require('fs');
|
|
const { takeUpload } = require('./importUpload');
|
|
const { roomFor, importLimitBytes } = require('./importDiskSpace');
|
|
const { isZipMagic } = require('./zipSource');
|
|
|
|
class UploadError extends Error {
|
|
constructor(code, message) { super(message); this.code = code; }
|
|
}
|
|
|
|
async function startsAsZip(file) {
|
|
const fd = await fs.promises.open(file, 'r');
|
|
const head = Buffer.alloc(4);
|
|
try { await fd.read(head, 0, 4, 0); } finally { await fd.close(); }
|
|
return isZipMagic(head);
|
|
}
|
|
|
|
async function uploadedImportValue(uploadId, userId, source, { setting = {}, spec } = {}) {
|
|
const upload = takeUpload(uploadId, userId, source);
|
|
if (!upload) throw new UploadError('import-upload-not-found', 'The uploaded file is not there any more; choose it again');
|
|
const { file } = upload;
|
|
const limit = importLimitBytes(setting);
|
|
if (spec && spec.zipSend === 'zip' && await startsAsZip(file)) {
|
|
const room = await roomFor(require('path').dirname(file));
|
|
return { zipFile: file, maxBytes: limit, inflateLimit: limit || (room === Infinity ? 0 : room) };
|
|
}
|
|
if (source === 'mail') return { mboxFile: file };
|
|
// An Outlook .msg or the Teams export's .tar: read as bytes from the disk.
|
|
if (spec && Array.isArray(spec.binaryFiles) && spec.binaryFiles.some(ext => String(upload.name || '').toLowerCase().endsWith(ext))) {
|
|
return { binaryFile: file };
|
|
}
|
|
// CSV/TSV: the rows, parsed on the server as the page parses a pasted text.
|
|
if (source === 'csv') {
|
|
try {
|
|
return await require('./csvUpload').readCsvUploadRows(file);
|
|
} catch (e) {
|
|
throw new UploadError(e.code || 'invalid-import-format', e.message);
|
|
}
|
|
}
|
|
if (spec && spec.send === 'excel') {
|
|
const base64 = (await fs.promises.readFile(file)).toString('base64');
|
|
return source === 'plane' ? { xlsxBase64: base64 } : { excelBase64: base64 };
|
|
}
|
|
if (source === 'plane' && /\.xlsx$/i.test(upload.name || '')) {
|
|
return { xlsxBase64: (await fs.promises.readFile(file)).toString('base64') };
|
|
}
|
|
// JSON is built from the file a chunk at a time (server/lib/streamJson.js):
|
|
// a JavaScript string cannot exceed about 512 MB, so a larger export could
|
|
// not be read as text at all.
|
|
if (spec && spec.send === 'json') {
|
|
try {
|
|
return await require('./streamJson').parseJsonStream(fs.createReadStream(file, { encoding: 'utf8' }));
|
|
} catch (e) {
|
|
throw new UploadError('invalid-import-format', `The file is not JSON: ${e.message}`);
|
|
}
|
|
}
|
|
// A Mattermost bulk export is JSON Lines: read a line at a time, each line
|
|
// parsed on its own.
|
|
if (source === 'mattermost') {
|
|
const readline = require('readline');
|
|
const lines = [];
|
|
let n = 0;
|
|
for await (const line of readline.createInterface({ input: fs.createReadStream(file, { encoding: 'utf8' }), crlfDelay: Infinity })) {
|
|
n += 1;
|
|
if (!line.trim()) continue;
|
|
try { lines.push(JSON.parse(n === 1 ? line.replace(/^\uFEFF/, '') : line)); } catch (e) {
|
|
throw new UploadError('invalid-import-format', `Line ${n} is not JSON`);
|
|
}
|
|
}
|
|
return { jsonLines: lines };
|
|
}
|
|
// The other text formats are parsed as one text; past what one string can
|
|
// hold that is refused with its reason rather than failing as out of memory.
|
|
const { size } = await fs.promises.stat(file);
|
|
if (size > MAX_TEXT_BYTES) {
|
|
throw new UploadError('import-too-large', `A ${source} file is read as one text, at most ${MAX_TEXT_BYTES} bytes`);
|
|
}
|
|
return (await fs.promises.readFile(file, 'utf8')).replace(/^\uFEFF/, '');
|
|
}
|
|
const MAX_TEXT_BYTES = 256 * 1024 * 1024;
|
|
|
|
module.exports = { uploadedImportValue, UploadError, MAX_TEXT_BYTES };
|