Files
wekan/client/lib/attachmentUploadConfig.js
T
Lauri Ojansivu ac201d4b59 Upload background image works, one upload config, and an avatar with no name.
Two fixes that both came out of the same session.

"Upload background image" picked a file and nothing arrived. Two fields of an
upload's config are not optional in the way they look, and the board
background's config - written by hand - had neither:

- `fileId`, and the same id copied into `meta.fileId`. Attachments'
  namingFunction decides the name a file is STORED under, and on the client it
  reads that id out of `meta` and deletes it. Without it the name is
  `undefined`.
- `transport`. HTTP everywhere, because Meteor-Files' default DDP floods the
  WebSocket and makes Safari reconnect at ~95%; DDP on Sandstorm, whose
  http-bridge strips the x-* upload headers so every chunk comes back "Can't
  continue upload, session expired" [408].

Both come from client/lib/attachmentUploadConfig.js now, used by the card's
file picker, the card's pasted image and the board background alike - so the
next uploader gets them by asking for a config rather than by knowing two
things nothing would have told it. The two card call sites lost their
hand-written copies with it.

getInitials() ended with `this.username[0]`, so a user document that arrived
without a username threw a TypeError from a Blaze helper, mid-render. Two
helpers call it, so each avatar threw twice, and the second throw left the
SVG's viewBox half-written ("0 0  15"), which Firefox refuses outright: the
avatar drew nothing and took the rest of that render pass with it. It is total
now - initials, fullname, the first letter of a username, then an empty string -
and a fullname of only spaces no longer spells the literal word "undefined".

tests/attachmentUploadConfig.test.cjs and tests/userInitials.test.cjs pin both,
including the empty-document case that threw.

Thanks to xet7 !
2026-08-14 05:46:41 +03:00

47 lines
2.1 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { ObjectId } from 'bson';
import { sanitizeText } from '/imports/lib/secureDOMPurify';
// The config every attachment upload has to be started with.
//
// Two of its fields are not optional in the way they look:
//
// * `fileId`, AND the same id copied into `meta.fileId`. Attachments'
// `namingFunction` (models/attachments.js) is what decides the name the file
// is STORED under, and on the client it reads that id out of `meta` and
// deletes it. Without it the stored name is `undefined`, so an upload
// started without it does not work - which is exactly what happened to the
// board background uploader, whose config was written by hand and had
// neither field.
// * `transport`. Meteor-Files' default is DDP, which floods the WebSocket
// channel and makes Safari reconnect repeatedly (the "Loading, please wait"
// banner, stalling at ~95%), so uploads go over HTTP - EXCEPT on Sandstorm,
// where the grain's http-bridge strips Meteor-Files' `x-*` upload headers
// and the server rejects every chunk with "Can't continue upload, session
// expired" [408]. DDP goes over method calls and has no such headers.
//
// One builder, so a new uploader gets both by having asked for a config rather
// than by remembering two things nothing would have told it about.
export function buildAttachmentUploadConfig({ file, meta = {}, fileName }) {
const fileId = new ObjectId().toString();
const config = {
file,
fileId,
fileName: fileName || safeName(file),
meta: { ...meta, fileId },
chunkSize: 'dynamic',
transport: Meteor.settings?.public?.sandstorm ? 'ddp' : 'http',
};
return config;
}
// The name as typed, unless sanitizing it changes it - in which case it was
// either an attempt at XSS (already defused by the sanitizer) or an ordinary
// mistake, and neither is worth a warning. An empty result still needs A name.
function safeName(file) {
const name = sanitizeText((file && file.name) || '');
if (name.length) return name;
if (file && file.type) return file.type.replace('image/', 'clipboard.');
return 'Empty-filename-after-sanitize.txt';
}