Files
wekan/client/lib/zipAttachmentViewer.js
T
Lauri Ojansivu cd74045a9f Attachment slideshow: Outlook .msg and OpenDocument are converted once, a piece at a time, into rows read a screen at a time.
An Outlook .msg (a Compound File of scattered sectors) and an
OpenDocument (a ZIP whose text is a compressed content.xml) cannot be
read a row at a time as they are: the slideshow read a .msg whole, up to
32 MB, and an OpenDocument as far as it was scrolled. Now the server
converts each the first time it is opened (server/lib/
attachmentPreviewStore.js):

- the original is read from its own storage in 64 KB byte ranges, never
  whole; of a .msg only the sector tables are kept and the text streams
  (models/lib/outlookMsgFormat.js openCompoundFile, outlookMsgRows),
  attachment data is never read; of an OpenDocument only content.xml is
  inflated and parsed as a stream (models/lib/zipDirectory.js and
  odfBlocks.js, moved from the client so both share them);
- what is found is written as rows - one JSON object a line - with an
  index of where each starts, to temporary names renamed when complete,
  in attachment-previews/ under the files root; made again when the file
  changes and removed with the attachment; two conversions at a time;
- one preview takes at most 512 MB of the server's disk, and a row
  repeated more than 100,000 times is written that many times.

The parts are served at /cdn/storage/attachments/<id>/preview-meta,
-index and -rows, after every check the original has, with Range, as
application/octet-stream, nosniff and a sandboxing CSP. Two path
segments, as /thumbnail: Meteor-Files answers every longer path under
/cdn/storage itself. The slideshow reads them through
client/lib/attachmentRecords.js into IndexedView, whose scrollbar is
exactly the rows; only the visible rows are fetched and drawn, and no
viewer reads a file whole any more.

tests/attachmentPreviewStore.test.cjs reads a .msg with a 5 MB
attachment through ranges and finds it the same as the whole-file
reader without reading the attachment, a 60,000-line body and an
HTML-only body as rows, an OpenDocument's 6 MB picture never read, the
repeat and disk guards, made once and removed, and the route after
every check. attachment-rich-preview.e2e.js opens a 100,000-line .msg
in Chromium and finds only ranged preview parts fetched, the last line
at the end of the scrollbar, and the preview refused without signing in.

Thanks to xet7 !
2026-11-22 12:58:26 +02:00

45 lines
1.8 KiB
JavaScript

// A ZIP attachment's file names in the slideshow, read through HTTP Range
// requests: the end of the file for where the central directory is (ZIP64 as
// well), then the central directory itself, as far as it is scrolled
// (attachmentWindow.js GrowingView). No entry is inflated or downloaded; a
// 4 GB archive of a million files costs the screenful of names shown.
//
// The directory is read by models/lib/zipDirectory.js, which the server's
// OpenDocument preview conversion shares.
import { RangeReader, GrowingView } from './attachmentWindow.js';
import { ZipDirectory } from '../../models/lib/zipDirectory.js';
export { ZipDirectory };
export async function openZipAttachment({ container, signal, size, url, emptyMessage, fetchImpl, viewportHeight }) {
const reader = new RangeReader(url, size, { signal, fetchImpl });
await reader.probeSize();
const directory = await new ZipDirectory(reader).open();
if (signal && signal.aborted) return { destroy() {} };
container.replaceChildren();
if (!directory.count) {
container.textContent = emptyMessage;
return { destroy() { container.replaceChildren(); } };
}
const entries = directory.entries();
const doc = container.ownerDocument;
const view = new GrowingView(container, {
listTag: 'ul',
viewportHeight,
next: async () => {
const rows = [];
while (rows.length < 200) {
const { value, done } = await entries.next();
if (done) break;
rows.push(value.name);
}
return rows.length ? rows : null;
},
// Archive paths are inert text: never HTML, links or extraction targets.
renderRow: name => { const li = doc.createElement('li'); li.textContent = name; return li; },
});
await view.start();
return { view, destroy() { view.destroy(); container.replaceChildren(); } };
}