mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 17:45:18 -05:00
The slideshow showed images, PDF, video, audio, plain text, ZIP listings and docx/xlsx/pptx. It now also shows, with viewers loaded only when such a file is opened (client/lib/attachmentRichViewer.js): - Markdown (.md) formatted, through WeKan's own +viewer, so an attached file follows every setting a card description does; - CSV and TSV as a table (papaparse), the first 1000 rows; - source code and JSON coloured by highlight.js - an HTML or SVG source file is shown as text, never run; - iCalendar (.ics) events and tasks, with WeKan's iCalendar reader; - Outlook .msg headers, text and attachment names, with WeKan's reader; - tar, tar.gz and .tgz file names (fflate, with an inflate limit and header checksums); - OpenDocument text, spreadsheets and presentations (.odt, .ods, .odp) as headings, paragraphs and tables. Everything a file contains reaches the page as text. Whether a file opens is now one answer, attachmentCanPreview, shared by the card, the board's version control view and the history table; the history table's copy had lost ZIP. tests/attachmentRichViewer.test.cjs drives each viewer in jsdom, with negative tests for binary files, refused downloads, gzip bombs, damaged tar headers and huge OpenDocument content, and attachment-rich-preview.e2e.js opens each kind in Chromium. Thanks to xet7 !
216 lines
11 KiB
JavaScript
216 lines
11 KiB
JavaScript
// #6532: what KIND of file is this, when the document does not say?
|
|
//
|
|
// After an upgrade from WeKan 6, an attachment that came through a migration can
|
|
// reach the client without the flags Meteor-Files normally writes at upload
|
|
// time: `isImage`, `isVideo`, `isPDF`, ... and sometimes without `extension` or
|
|
// `type` either. The card view decides what to render from exactly those flags:
|
|
//
|
|
// if(isImage) img.attachment-thumbnail(src="{{link}}")
|
|
// ...
|
|
// else span.attachment-thumbnail-text= extension
|
|
//
|
|
// so a migrated image fell through to the last branch and, with no extension to
|
|
// print, drew an empty white box - while the SAME `link` downloaded the file
|
|
// perfectly and the board view showed the cover. That is the report: "images can
|
|
// be seen in Board view, can be downloaded in card view, but do not show in Card
|
|
// view and cannot be maximized. Newly created cards do not show this behaviour."
|
|
//
|
|
// The kind of a file is derivable: from its mime type when there is one, and
|
|
// from its name when there is not. This module is the one place that derives it,
|
|
// so the card view, the viewer and the repair below all answer the same way.
|
|
|
|
// The extensions worth recognising by name - the ones WeKan renders inline.
|
|
const IMAGE_EXTENSIONS = [
|
|
'png', 'jpg', 'jpeg', 'jpe', 'gif', 'webp', 'bmp', 'svg', 'avif', 'heic',
|
|
'heif', 'tif', 'tiff', 'ico', 'jfif',
|
|
];
|
|
const VIDEO_EXTENSIONS = ['mp4', 'webm', 'ogv', 'mov', 'm4v', 'mkv', 'avi'];
|
|
const AUDIO_EXTENSIONS = ['mp3', 'ogg', 'oga', 'wav', 'flac', 'm4a', 'aac', 'opus'];
|
|
const TEXT_EXTENSIONS = ['txt', 'md', 'csv', 'log', 'ini', 'conf', 'yml', 'yaml'];
|
|
const OFFICE_EXTENSIONS = ['docx', 'xlsx', 'pptx'];
|
|
|
|
// The kinds the slideshow shows with a viewer of its own
|
|
// (client/lib/attachmentRichViewer.js), each loaded only when such a file is
|
|
// opened. A file whose stated type says it is something else is not one of them.
|
|
const MARKDOWN_EXTENSIONS = ['md', 'markdown', 'mdown', 'mkd'];
|
|
const CSV_EXTENSIONS = ['csv', 'tsv'];
|
|
const CALENDAR_EXTENSIONS = ['ics', 'ical', 'ifb'];
|
|
const OPENDOCUMENT_EXTENSIONS = ['odt', 'ods', 'odp'];
|
|
// Source code, as the highlight.js language (its "common" set) to colour it with.
|
|
const CODE_LANGUAGES = {
|
|
js: 'javascript', mjs: 'javascript', cjs: 'javascript', jsx: 'javascript',
|
|
ts: 'typescript', tsx: 'typescript', mts: 'typescript', cts: 'typescript',
|
|
py: 'python', rb: 'ruby', go: 'go', rs: 'rust', java: 'java', kt: 'kotlin',
|
|
kts: 'kotlin', c: 'c', h: 'c', cc: 'cpp', cpp: 'cpp', cxx: 'cpp', hpp: 'cpp',
|
|
hh: 'cpp', cs: 'csharp', php: 'php', pl: 'perl', pm: 'perl', lua: 'lua',
|
|
r: 'r', swift: 'swift', m: 'objectivec', sh: 'bash', bash: 'bash', zsh: 'bash',
|
|
sql: 'sql', css: 'css', scss: 'scss', less: 'less', html: 'xml', htm: 'xml',
|
|
xhtml: 'xml', xml: 'xml', vue: 'xml', json: 'json', toml: 'ini',
|
|
diff: 'diff', patch: 'diff', graphql: 'graphql', gql: 'graphql',
|
|
vb: 'vbnet', wat: 'wasm', jade: 'plaintext', pug: 'plaintext',
|
|
dockerfile: 'plaintext', gradle: 'plaintext',
|
|
};
|
|
// Types a browser or an operating system gives these files, besides text/*.
|
|
const GENERIC_TYPES = ['application/octet-stream', 'application/x-download', 'binary/octet-stream'];
|
|
const CODE_TYPE = /^application\/(json|javascript|x-javascript|ecmascript|xml|xhtml\+xml|x-sh|x-httpd-php|x-php|sql|typescript|x-typescript|x-python|toml|x-yaml|yaml|graphql)$/;
|
|
|
|
// Enough of a name -> mime map to answer the questions above. Not a full table:
|
|
// anything not listed simply has no derived type, and the flags stay false.
|
|
const EXTENSION_MIME = {
|
|
png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', jpe: 'image/jpeg',
|
|
jfif: 'image/jpeg', gif: 'image/gif', webp: 'image/webp', bmp: 'image/bmp',
|
|
svg: 'image/svg+xml', avif: 'image/avif', heic: 'image/heic', heif: 'image/heif',
|
|
tif: 'image/tiff', tiff: 'image/tiff', ico: 'image/x-icon',
|
|
mp4: 'video/mp4', webm: 'video/webm', ogv: 'video/ogg', mov: 'video/quicktime',
|
|
m4v: 'video/x-m4v', mkv: 'video/x-matroska', avi: 'video/x-msvideo',
|
|
mp3: 'audio/mpeg', ogg: 'audio/ogg', oga: 'audio/ogg', wav: 'audio/wav',
|
|
flac: 'audio/flac', m4a: 'audio/mp4', aac: 'audio/aac', opus: 'audio/opus',
|
|
zip: 'application/zip',
|
|
pdf: 'application/pdf', json: 'application/json',
|
|
txt: 'text/plain', md: 'text/markdown', csv: 'text/csv', log: 'text/plain',
|
|
ini: 'text/plain', conf: 'text/plain', yml: 'text/yaml', yaml: 'text/yaml',
|
|
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
};
|
|
|
|
// The extension of a file NAME, lower-case and without the dot. Never trusts a
|
|
// path separator or a query string to be absent.
|
|
function extensionOfName(name) {
|
|
if (typeof name !== 'string' || !name) return '';
|
|
// Strip a query/hash FIRST, then take the last path segment: splitting on all
|
|
// of them at once and taking the last piece turns "photo.png?x=1" into "x=1".
|
|
const withoutQuery = name.split(/[?#]/)[0];
|
|
const base = withoutQuery.split(/[\\/]/).pop();
|
|
const dot = base.lastIndexOf('.');
|
|
if (dot <= 0 || dot === base.length - 1) return '';
|
|
return base.slice(dot + 1).toLowerCase();
|
|
}
|
|
|
|
// The mime type the document states, in any of the fields that have carried it.
|
|
function statedType(doc) {
|
|
if (!doc) return '';
|
|
const versions = doc.versions || {};
|
|
const original = versions.original || {};
|
|
const candidates = [doc.type, doc.mime, doc['mime-type'], doc.contentType,
|
|
original.type, doc.meta && doc.meta.type];
|
|
const found = candidates.find(t => typeof t === 'string' && t.includes('/'));
|
|
return found ? found.toLowerCase() : '';
|
|
}
|
|
|
|
function extensionOf(doc) {
|
|
if (!doc) return '';
|
|
const stated = [doc.extension, doc.ext, doc.versions && doc.versions.original
|
|
&& doc.versions.original.extension]
|
|
.find(e => typeof e === 'string' && e && e !== '.');
|
|
if (stated) return String(stated).replace(/^\./, '').toLowerCase();
|
|
return extensionOfName(doc.name || (doc.original && doc.original.name) || '');
|
|
}
|
|
|
|
// The whole answer for one document: what it is, however little it says.
|
|
function attachmentKind(doc) {
|
|
const extension = extensionOf(doc);
|
|
const type = statedType(doc) || EXTENSION_MIME[extension] || '';
|
|
|
|
// A stated flag is believed when it is true; a missing or false flag is
|
|
// derived, so a document that was written correctly is never contradicted.
|
|
const flag = (stated, derived) => (stated === true ? true : derived);
|
|
// The NAME only answers when the document states no type of its own: a file
|
|
// called notes.txt whose type says image/png is a PNG, and asking the name as
|
|
// well would make it both.
|
|
const stated = statedType(doc);
|
|
const byName = list => !stated && list.includes(extension);
|
|
|
|
return {
|
|
extension,
|
|
type,
|
|
isImage: flag(doc && doc.isImage,
|
|
type.startsWith('image/') || byName(IMAGE_EXTENSIONS)),
|
|
isVideo: flag(doc && doc.isVideo,
|
|
type.startsWith('video/') || byName(VIDEO_EXTENSIONS)),
|
|
isAudio: flag(doc && doc.isAudio,
|
|
type.startsWith('audio/') || byName(AUDIO_EXTENSIONS)),
|
|
isPDF: flag(doc && doc.isPDF, type === 'application/pdf' || (!stated && extension === 'pdf')),
|
|
isJSON: flag(doc && doc.isJSON, type === 'application/json' || (!stated && extension === 'json')),
|
|
isText: flag(doc && doc.isText,
|
|
type.startsWith('text/') || byName(TEXT_EXTENSIONS)),
|
|
isZIP: ['application/zip', 'application/x-zip-compressed'].includes(type) ||
|
|
(extension === 'zip' && (!stated || type === 'application/octet-stream')),
|
|
isOffice: OFFICE_EXTENSIONS.includes(extension) &&
|
|
(!stated || type.startsWith('application/vnd.openxmlformats-officedocument.')),
|
|
};
|
|
}
|
|
|
|
// The slideshow's own viewer for a file, or null: 'markdown', 'csv', 'code',
|
|
// 'calendar', 'outlook', 'tar' or 'opendocument'. Asked before the plain text
|
|
// viewer, so a .md is formatted and a .py coloured rather than shown raw.
|
|
function attachmentRichKind(doc) {
|
|
const kind = attachmentKind(doc);
|
|
const { extension, type } = kind;
|
|
const stated = statedType(doc);
|
|
const generic = !stated || GENERIC_TYPES.includes(type);
|
|
const textual = generic || type.startsWith('text/');
|
|
const name = String((doc && doc.name) || '').toLowerCase().split(/[?#]/)[0];
|
|
if (['text/markdown', 'text/x-markdown'].includes(type)
|
|
|| (MARKDOWN_EXTENSIONS.includes(extension) && textual)) return 'markdown';
|
|
if (['text/csv', 'text/tab-separated-values', 'application/csv'].includes(type)
|
|
|| (CSV_EXTENSIONS.includes(extension) && (textual || type === 'application/vnd.ms-excel'))) return 'csv';
|
|
if (type === 'text/calendar' || (CALENDAR_EXTENSIONS.includes(extension) && textual)) return 'calendar';
|
|
if (type === 'application/vnd.ms-outlook'
|
|
|| (extension === 'msg' && (generic || type === 'application/x-ole-storage'))) return 'outlook';
|
|
const tarName = /\.(tar|tgz|tar\.gz)$/.test(name) || ['tar', 'tgz'].includes(extension);
|
|
if (type === 'application/x-tar' || (tarName && (generic
|
|
|| ['application/gzip', 'application/x-gzip', 'application/x-compressed-tar', 'application/x-gtar'].includes(type)))) return 'tar';
|
|
if (OPENDOCUMENT_EXTENSIONS.includes(extension)
|
|
&& (generic || type.startsWith('application/vnd.oasis.opendocument.') || type === 'application/zip')) return 'opendocument';
|
|
if (kind.isJSON || (CODE_LANGUAGES[extension] && (textual || CODE_TYPE.test(type)))) return 'code';
|
|
return null;
|
|
}
|
|
|
|
// The highlight.js language of a code file, 'json' for JSON, else 'plaintext'.
|
|
function attachmentCodeLanguage(doc) {
|
|
const kind = attachmentKind(doc);
|
|
if (kind.isJSON) return 'json';
|
|
return CODE_LANGUAGES[kind.extension] || 'plaintext';
|
|
}
|
|
|
|
// Whether the slideshow opens this file at all. One answer for the card view,
|
|
// the board's version control view, the history table and Admin Panel / Files.
|
|
function attachmentCanPreview(doc) {
|
|
const kind = attachmentKind(doc);
|
|
return !!(kind.isImage || kind.isPDF || kind.isText || kind.isJSON || kind.isOffice
|
|
|| kind.isZIP || kind.isVideo || kind.isAudio || attachmentRichKind(doc));
|
|
}
|
|
|
|
// Which of the derived flags a stored document is MISSING - what a repair would
|
|
// write. Returns null when the document already says everything it needs to.
|
|
function attachmentKindFix(doc) {
|
|
const kind = attachmentKind(doc);
|
|
const fix = {};
|
|
for (const key of ['isImage', 'isVideo', 'isAudio', 'isPDF', 'isJSON', 'isText']) {
|
|
if (kind[key] === true && doc[key] !== true) fix[key] = true;
|
|
}
|
|
// `extension` and `ext` are one fact under two names: write them only when the
|
|
// document carries neither, so a correct document is left completely alone.
|
|
if (kind.extension && !doc.extension && !doc.ext) {
|
|
fix.extension = kind.extension;
|
|
fix.ext = kind.extension;
|
|
}
|
|
if (kind.type && !doc.type) fix.type = kind.type;
|
|
return Object.keys(fix).length ? fix : null;
|
|
}
|
|
|
|
export {
|
|
attachmentKind,
|
|
attachmentKindFix,
|
|
attachmentRichKind,
|
|
attachmentCodeLanguage,
|
|
attachmentCanPreview,
|
|
CODE_LANGUAGES,
|
|
extensionOfName,
|
|
IMAGE_EXTENSIONS,
|
|
VIDEO_EXTENSIONS,
|
|
AUDIO_EXTENSIONS,
|
|
OFFICE_EXTENSIONS,
|
|
};
|