mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 10:48:06 -05:00
Reported after upgrading from WeKan 6 to 10.37/38/40: an attached image is
visible in board view, downloads correctly from the card, and yet the card view
shows an empty white box that cannot be maximized. Cards created AFTER the
upgrade are fine.
The card view does not look at the file; it looks at the flags Meteor-Files
writes at upload time:
if(isImage) img.attachment-thumbnail(src="{{link}}")
...
else span.attachment-thumbnail-text= extension
An attachment that came through a migration can arrive without them - no
isImage, and often no `extension` or `type` either - so it fell through to the
last branch and printed an extension that was not there: an empty box. The same
`link` in the download button worked, because a download needs no flag. The
viewer refused to open it for the same reason.
The kind of a file is derivable, and models/lib/attachmentKind.js is now the one
place that derives it: from the mime type where the document has one (in any of
the fields that have carried it over the years - type, mime, mime-type,
contentType, versions.original.type), and from the file NAME where it does not.
A flag the document states as true is believed, never contradicted; the name is
consulted only when there is no stated type, so a notes.txt that really is
image/png stays an image and does not also become text. The card gallery, the
"add cover" menu and the viewer all ask it, so a thumbnail that renders can also
be opened.
A startup repair (schema upgrade step `attachment-kind-flags`) writes the same
answer back to the documents, so the REST API, the exports and everything else
reading those flags agree with what is on screen. A file whose kind cannot be
told from either its type or its name is counted as unresolved rather than
guessed at.
tests/attachmentKind.test.cjs pins the rules - the reported case first - and the
wiring: 17 kinds derived from a name alone, mime beating a misleading name, a
name that must not be trusted to be a plain name (path separators, query
strings, dotfiles), the type read from each of the fields that carry it, and a
repair that writes exactly what is missing and nothing else.
NOT verified here: no server and no node in this environment, so the rules were
replayed against a model of the module rather than executed. That replay caught
three real mistakes in the first draft (a query string eating the extension, a
name overriding a stated type, and the repair rewriting a document that was
already correct), which are fixed.
Thanks to S0QR2 and xet7 !
Fixes #6532,
148 lines
6.7 KiB
JavaScript
148 lines
6.7 KiB
JavaScript
'use strict';
|
|
|
|
// #6532: "Migrated Images don't show in Card view".
|
|
//
|
|
// After an upgrade from WeKan 6, an attachment that came through a migration can
|
|
// arrive without the flags Meteor-Files writes at upload time - `isImage`,
|
|
// `isVideo`, `isPDF` - and sometimes without `extension` or `type` either. The
|
|
// card view renders 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 and
|
|
// the board view showed it as a cover. That is the report exactly: visible in
|
|
// board view, downloadable, invisible in the card view, cannot be maximized, and
|
|
// only for cards created BEFORE the upgrade.
|
|
//
|
|
// The kind of a file is derivable - from its mime type, or from its name. These
|
|
// are the rules, and the repair that writes them back to the documents.
|
|
//
|
|
// Run: node tests/attachmentKind.test.cjs
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
let passed = 0;
|
|
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
|
|
const read = rel => fs.readFileSync(path.join(__dirname, '..', rel), 'utf8');
|
|
|
|
// The module is ESM; load it the way the other ESM tests here do.
|
|
let attachmentKind;
|
|
let attachmentKindFix;
|
|
|
|
(async () => {
|
|
({ attachmentKind, attachmentKindFix } = await import('../models/lib/attachmentKind.js'));
|
|
|
|
console.log('attachmentKind:');
|
|
|
|
test('the reported case: a migrated image with nothing but a name', () => {
|
|
const migrated = { _id: 'a1', name: 'screenshot.png', meta: { cardId: 'c1' } };
|
|
const kind = attachmentKind(migrated);
|
|
assert.strictEqual(kind.isImage, true, 'this is what drew the white box');
|
|
assert.strictEqual(kind.extension, 'png');
|
|
assert.strictEqual(kind.type, 'image/png', 'derived, so the viewer can open it too');
|
|
});
|
|
|
|
test('a document that DOES say is believed, not second-guessed', () => {
|
|
const uploaded = { name: 'photo.jpg', type: 'image/jpeg', extension: 'jpg', isImage: true };
|
|
assert.deepStrictEqual(attachmentKind(uploaded), {
|
|
extension: 'jpg', type: 'image/jpeg',
|
|
isImage: true, isVideo: false, isAudio: false,
|
|
isPDF: false, isJSON: false, isText: false,
|
|
});
|
|
// ...and nothing to repair.
|
|
assert.strictEqual(attachmentKindFix(uploaded), null);
|
|
});
|
|
|
|
test('the mime type wins over the name when they disagree', () => {
|
|
// A .txt that is really a PNG is a PNG: the type is the stronger statement.
|
|
const kind = attachmentKind({ name: 'notes.txt', type: 'image/png' });
|
|
assert.strictEqual(kind.isImage, true);
|
|
assert.strictEqual(kind.isText, false);
|
|
});
|
|
|
|
test('every kind the card view renders is derivable from a name alone', () => {
|
|
const cases = [
|
|
['a.png', 'isImage'], ['a.JPG', 'isImage'], ['a.jpeg', 'isImage'],
|
|
['a.gif', 'isImage'], ['a.webp', 'isImage'], ['a.svg', 'isImage'],
|
|
['a.heic', 'isImage'], ['a.tiff', 'isImage'],
|
|
['a.mp4', 'isVideo'], ['a.webm', 'isVideo'], ['a.mov', 'isVideo'],
|
|
['a.mp3', 'isAudio'], ['a.ogg', 'isAudio'], ['a.flac', 'isAudio'],
|
|
['a.pdf', 'isPDF'], ['a.json', 'isJSON'], ['a.txt', 'isText'],
|
|
];
|
|
for (const [name, expected] of cases) {
|
|
const kind = attachmentKind({ name });
|
|
assert.strictEqual(kind[expected], true, `${name} must be ${expected}`);
|
|
}
|
|
});
|
|
|
|
test('a file that says nothing and is nothing stays nothing', () => {
|
|
const kind = attachmentKind({ name: 'archive.zip' });
|
|
assert.strictEqual(kind.isImage, false);
|
|
assert.strictEqual(kind.extension, 'zip', 'but the extension still prints in the box');
|
|
const nameless = attachmentKind({ _id: 'x' });
|
|
assert.strictEqual(nameless.isImage, false);
|
|
assert.strictEqual(nameless.extension, '');
|
|
assert.strictEqual(nameless.type, '');
|
|
});
|
|
|
|
test('a name is not trusted to be a plain name', () => {
|
|
// Path separators and query strings must not become the "extension".
|
|
assert.strictEqual(attachmentKind({ name: 'a.png/../evil' }).extension, '');
|
|
assert.strictEqual(attachmentKind({ name: 'photo.png?x=1' }).extension, 'png');
|
|
assert.strictEqual(attachmentKind({ name: '.hidden' }).extension, '',
|
|
'a dotfile has no extension');
|
|
assert.strictEqual(attachmentKind({ name: 'trailing.' }).extension, '');
|
|
});
|
|
|
|
test('the type is read from wherever the document happens to carry it', () => {
|
|
for (const doc of [
|
|
{ name: 'x', mime: 'image/png' },
|
|
{ name: 'x', 'mime-type': 'image/png' },
|
|
{ name: 'x', contentType: 'image/png' },
|
|
{ name: 'x', versions: { original: { type: 'image/png' } } },
|
|
]) {
|
|
assert.strictEqual(attachmentKind(doc).isImage, true, JSON.stringify(doc));
|
|
}
|
|
});
|
|
|
|
test('the repair writes exactly what is missing, and nothing else', () => {
|
|
const fix = attachmentKindFix({ _id: 'a1', name: 'screenshot.png' });
|
|
assert.deepStrictEqual(fix, {
|
|
isImage: true, extension: 'png', ext: 'png', type: 'image/png',
|
|
});
|
|
// An existing (correct) value is never overwritten.
|
|
const partial = attachmentKindFix({ name: 'clip.mp4', type: 'video/mp4', extension: 'mp4' });
|
|
assert.deepStrictEqual(partial, { isVideo: true },
|
|
'`extension` and `ext` are one fact: a document that has either keeps it');
|
|
// Nothing derivable: nothing to write.
|
|
assert.strictEqual(attachmentKindFix({ _id: 'x' }), null);
|
|
});
|
|
|
|
test('the card view and the viewer both ask the helper', () => {
|
|
const src = read('client/components/cards/attachments.js');
|
|
assert.ok(/import \{ attachmentKind \} from '\/models\/lib\/attachmentKind';/.test(src));
|
|
assert.ok(/isImage\(\) \{\s*\n\s*return attachmentKind\(this\)\.isImage;/.test(src),
|
|
'the gallery helper - a Blaze helper wins over the data context');
|
|
assert.ok(/extension\(\) \{\s*\n\s*return attachmentKind\(this\)\.extension;/.test(src));
|
|
assert.ok(/const kind = attachmentKind\(attachment\);[\s\S]*?case \(kind\.isImage\)/.test(src),
|
|
'and the viewer, or a thumbnail that renders still would not open');
|
|
assert.ok(!/case \(attachment\.isImage\)/.test(src), 'no raw flag left in the switch');
|
|
});
|
|
|
|
test('and the stored documents are repaired on startup', () => {
|
|
const steps = read('server/lib/schemaUpgradeSteps.js');
|
|
assert.ok(/name: 'attachment-kind-flags'/.test(steps), 'the step must exist');
|
|
const step = steps.slice(steps.indexOf("name: 'attachment-kind-flags'"));
|
|
assert.ok(/attachmentKindFix/.test(step.slice(0, 1500)), 'using the same rules');
|
|
assert.ok(/unresolved \+= 1;/.test(step.slice(0, 1800)),
|
|
'a file whose kind cannot be told is counted, not guessed at');
|
|
});
|
|
|
|
console.log(`\n${passed} tests passed`);
|
|
})();
|