Files
wekan/models/lib/importManyFiles.js
T
Lauri Ojansivu 01e88410cd Import and export Azure DevOps Boards, Microsoft Lists, Project, Outlook, Teams and OneNote.
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 !
2026-11-21 01:33:59 +02:00

100 lines
4.7 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// "Import many boards": several export files of one tool, or one .zip that
// holds them, each imported as its own board - what a Trello .zip of many
// boards does, for every import source. The import page and api.py build the
// same document per file that a single import sends (models/lib/importSourceShape.js).
// Plain JavaScript, so tests/importManyFiles.test.cjs runs it in Node.
import { unzipSync } from 'fflate';
import { readCsv } from './todoistCsvFormat.js';
import { csvSeparatorOf } from './csvImportMapping.js';
import { IMPORT_SOURCES } from './importSources.js';
// What each source's file is sent as comes from the one description of every
// source (models/lib/importSources.js); these lists are read from it.
const keysWhere = test => IMPORT_SOURCES.filter(test).map(source => source.key);
// The import file is an Excel workbook: sent as { excelBase64 }.
export const EXCEL_SOURCES = keysWhere(source => source.send === 'excel');
// A .zip is ONE export of these tools (and of WeKan, with attachments): it is
// a board, not a container of boards.
export const ZIP_EXPORT_SOURCES = keysWhere(source => Boolean(source.zipSend));
// The import reads the file's text.
export const TEXT_SOURCES = keysWhere(source => source.send === 'text');
// Imported by their own creators, not the generalized importer: "One board
// per project" does not apply to them.
export const OWN_CREATOR_SOURCES = keysWhere(source => source.creator !== 'generalized');
export const isGeneralizedSource = source => !OWN_CREATOR_SOURCES.includes(source);
export const MAX_MANY_FILES = 500;
export const MAX_MANY_BYTES = 200 * 1024 * 1024;
const extensionOf = name => {
const match = /\.([a-z0-9]+)$/i.exec(String(name || ''));
return match ? match[1].toLowerCase() : '';
};
const isNoise = path => /(^|\/)(__MACOSX\/|\.)/.test(path) || /\/$/.test(path);
function toBase64(bytes) {
if (typeof Buffer !== 'undefined') return Buffer.from(bytes).toString('base64');
let binary = '';
for (let i = 0; i < bytes.length; i += 0x8000) binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
return btoa(binary); // eslint-disable-line no-undef
}
// files: [{ name, bytes: Uint8Array }] as chosen. A .zip that is not one export
// of the source is opened and each file in it becomes one file to import.
// Returns { files, skipped: [reason] }.
export function expandFiles(source, files) {
const out = [];
const skipped = [];
let total = 0;
const take = (name, bytes) => {
if (out.length >= MAX_MANY_FILES) { skipped.push(`${name}: more than ${MAX_MANY_FILES} files`); return; }
total += bytes.length;
if (total > MAX_MANY_BYTES) { skipped.push(`${name}: more than ${MAX_MANY_BYTES / 1024 / 1024} MB in all`); return; }
out.push({ name, bytes });
};
for (const file of files || []) {
const name = String(file.name || 'file');
if (extensionOf(name) === 'zip' && !ZIP_EXPORT_SOURCES.includes(source)) {
let entries;
try {
// Sizes are checked before inflating: an entry's declared size is
// what fflate's filter sees.
let declared = 0;
entries = unzipSync(file.bytes, { filter: entry => {
if (isNoise(entry.name)) return false;
declared += entry.originalSize || 0;
return declared <= MAX_MANY_BYTES;
} });
} catch (error) {
skipped.push(`${name}: not a readable .zip`);
continue;
}
Object.keys(entries).sort().forEach(path => take(`${name}/${path}`, entries[path]));
} else {
take(name, file.bytes);
}
}
return { files: out, skipped };
}
// The document a single import of this file sends, for `source`.
export function documentForFile(source, name, bytes) {
const ext = extensionOf(name);
// An Outlook .msg, the Teams export's .tar: the bytes themselves.
if ((IMPORT_SOURCES.find(s => s.key === source)?.binaryFiles || []).includes(`.${ext}`)) return { binaryBase64: toBase64(bytes) };
if (EXCEL_SOURCES.includes(source) || (source === 'plane' && ext === 'xlsx')) {
return source === 'plane' ? { xlsxBase64: toBase64(bytes) } : { excelBase64: toBase64(bytes) };
}
if (ext === 'zip' && ZIP_EXPORT_SOURCES.includes(source) && source !== 'wekan') return { zipBase64: toBase64(bytes) };
const text = new TextDecoder('utf-8').decode(bytes).replace(/^/, '');
if (source === 'csv') {
// As the import page: a tab separated file is read with tabs, so a value
// with a comma in it stays one value, and a file with more semicolons than
// commas in its first line is semicolon separated.
return readCsv(text, 'CSV', csvSeparatorOf(text)).filter(row => row.some(cell => String(cell).trim()));
}
if (TEXT_SOURCES.includes(source)) return text;
return JSON.parse(text);
}