Files
wekan/models/server/createWorkbook.js
T
Lauri Ojansivu a712db9473 Fix the board Excel export, and the attachment stuck as .bin.
#6591 - "Board Settings -> Export board -> export/Excel didn't work".
Reproduced against a running WeKan: CSV and JSON of the same board answered
200, Excel never answered at all, and the log said nothing.

  csv:   HTTP 200 4758b
  json:  HTTP 200 4491b
  excel: Operation timed out after 30002 ms with 0 bytes received

Two faults, either of which hangs the browser on its own.

The zip: exceljs 4.7.3's streaming WorkbookWriter calls archiver the way
archiver 7 was called - Archiver('zip', opts) - and WeKan moved to archiver 8
for the low-memory backup zips. archiver 8 is ESM and exports classes, so that
is "TypeError: Archiver is not a function"; the export has been broken since
the bump. Supplying the missing factory does not rescue it: archiver 8's
readable-stream then refuses the objects exceljs appends ("input source must be
valid Stream or Buffer instance"), as an error EVENT. So createWorkbookWriter
asks what archiver exports and, when it cannot stream, returns a buffered
writer with the same API that writes the whole workbook on commit() - the path
the export used before it was made streaming. Bounded memory is what is lost,
not the export.

The silence: the route called exporterExcel.build(res) without awaiting it, so
the rejection went nowhere - no 500, no log, and a response that was never
written or ended. Every sibling route awaits; this one did not. It does now,
and a failure answers 500 with the reason.

#6589 - a .drawio attachment stored and shown as ".bin", unopenable, and
impossible to rename. Also two faults:

The name: a browser sends application/octet-stream for a type it does not
know, and the upload-time "correct the extension to the type" step took that
literally - mime.extension('application/octet-stream') is 'bin', so
"sso-proconnect-keycloak.drawio" became "...drawio.bin". Every unrecognised
format went the same way. An uninformative type now yields no extension at all,
while a type that does say something still corrects the name.

The rename: versions[].path and the file on disk had diverged, and rename used
the recorded path alone -

  Error: ENOENT: no such file or directory, rename
    '/data/files/attachments/6a7d66369c6aee799e857d36.drawio' -> ...

- while the READER already searched every layout WeKan has used and found the
file. That search is a method now (candidatePaths/resolveExistingPath) and
reading, renaming and deleting all use it, so an attachment that can be read
can also be repaired. When there is genuinely no file, the error names the
attachment instead of a path nobody recognises.

Thanks to rmb82, titver968 and xet7 !

Fixes #6589,
Fixes #6591.
2026-08-13 15:51:25 +03:00

104 lines
4.3 KiB
JavaScript

import Excel from '@wekanteam/exceljs';
export const createWorkbook = function() {
return new Excel.Workbook();
};
// CAN the streaming writer actually stream? (#6591: "Board Settings -> Export
// board -> export/Excel didn't work".)
//
// exceljs 4.7.3's WorkbookWriter zips through archiver, and calls it the way
// archiver 7 was used:
//
// const Archiver = require('archiver'); // lib/stream/xlsx/workbook-writer.js
// this.zip = Archiver('zip', this.zipOptions);
//
// archiver 8 is ESM and exports CLASSES - { Archiver, ZipArchive, TarArchive,
// JsonArchive } - so that call is `TypeError: Archiver is not a function`, and
// WeKan moved to archiver 8 for the low-memory backup zips. The export was
// therefore broken from the moment the dependency was bumped, and broken in the
// worst way: the route calls build(res) without awaiting it, so the rejection
// went nowhere, no error was logged, and the browser waited for a response that
// was never coming. A request for the board export simply hung.
//
// Constructing the missing factory is not enough - archiver 8's own
// readable-stream then rejects the stream objects exceljs hands to append()
// ("input source must be valid Stream or Buffer instance"), which is an error
// EVENT on the archive, i.e. a crash rather than a rejected promise. So the
// question is answered before anything is constructed, by what archiver
// exports: callable means exceljs's writer works, an object means it cannot.
function streamingWriterWorks() {
try {
// eslint-disable-next-line global-require
return typeof require('archiver') === 'function';
} catch (e) {
return false;
}
}
// The same API as WorkbookWriter, backed by an ordinary Workbook: rows are kept
// in memory and the whole file is written when commit() is called. That is what
// the export did before it was made streaming, so it is a known-good path -
// bounded memory is what is lost, not the export.
class BufferedWorkbookWriter {
constructor(stream) {
this._stream = stream;
this._workbook = new Excel.Workbook();
}
// exceljs's writer takes these as plain properties; pass them through.
set creator(v) { this._workbook.creator = v; }
get creator() { return this._workbook.creator; }
set lastModifiedBy(v) { this._workbook.lastModifiedBy = v; }
get lastModifiedBy() { return this._workbook.lastModifiedBy; }
set created(v) { this._workbook.created = v; }
get created() { return this._workbook.created; }
set modified(v) { this._workbook.modified = v; }
get modified() { return this._workbook.modified; }
set lastPrinted(v) { this._workbook.lastPrinted = v; }
get lastPrinted() { return this._workbook.lastPrinted; }
addWorksheet(name, options) {
const worksheet = this._workbook.addWorksheet(name, options);
// A streaming worksheet has commit(); an in-memory one does not, and the
// caller commits every row and then the sheet.
if (typeof worksheet.commit !== 'function') worksheet.commit = () => {};
return worksheet;
}
getWorksheet(name) {
const worksheet = this._workbook.getWorksheet(name);
if (worksheet && typeof worksheet.commit !== 'function') worksheet.commit = () => {};
return worksheet;
}
// Rows already have a commit() on the in-memory Row class, so nothing is
// needed for those.
async commit() {
await this._workbook.xlsx.write(this._stream);
if (this._stream && typeof this._stream.end === 'function') this._stream.end();
}
}
// Streaming workbook writer: flushes rows straight to `stream` (the HTTP
// response) as they are committed, instead of holding the whole workbook in
// memory. Used by board Excel export so a board with thousands of cards exports
// with bounded RAM. useStyles/useSharedStrings keep the styled output identical
// to the in-memory Workbook.
//
// When the streaming writer cannot work (see above), this returns the buffered
// stand-in instead. Same calls, same file, more memory - and an export that
// finishes, which is the part that matters.
export const createWorkbookWriter = function(stream) {
if (!streamingWriterWorks()) {
return new BufferedWorkbookWriter(stream);
}
return new Excel.stream.xlsx.WorkbookWriter({
stream,
useStyles: true,
useSharedStrings: true,
});
};
export const streamingExcelAvailable = streamingWriterWorks;