mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
The JSON branches of REST /api/attachment/upload, upload-background and
copy, their Meteor-method twins in server/attachmentApi.js, and Admin
Panel's site logo upload created the file with Attachments.insertAsync.
Meteor-Files (ostrio:files 3) has insertAsync only on the client; on the
server it is not a function, so every one of them failed - a REST 500
"Attachments.insertAsync is not a function". Only the multipart branches,
which use addAttachmentFromStream, worked.
All seven store with writeAsync(buffer, { fileName, type, userId, meta },
true) now, as email-to-card and the importers already do. Its
after-upload hook validates the file - type, size and the external
scanner, which these paths skipped - and moves it to the default storage,
or to meta.copyStorage for a copy, so the second deferred moveToStorage
each branch made is removed. The file records who stored it.
tests/attachmentApiServerWrite.test.cjs pins the seven calls and fails on
any server-side Attachments.insertAsync or Avatars.insertAsync.
This was in CHANGELOG TODO Later as needing a maintainer decision; the
site logo upload was found by the new test's tree-wide check.
Thanks to xet7 !
38 lines
2.3 KiB
JavaScript
38 lines
2.3 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { check } from 'meteor/check';
|
|
import { ObjectId } from 'bson';
|
|
import Attachments from '/models/attachments';
|
|
import Settings from '/models/settings';
|
|
import AttachmentStorageSettings from '/models/attachmentStorageSettings';
|
|
const {siteLogoField, siteLogoImageType} = require('/models/lib/siteLogo');
|
|
|
|
const MAX_LOGO_BYTES = 2 * 1024 * 1024;
|
|
Meteor.methods({
|
|
async uploadSiteLogo(kind, base64) {
|
|
check(kind, String);
|
|
check(base64, String);
|
|
const field = siteLogoField(kind);
|
|
if (!field) throw new Meteor.Error('invalid-logo-kind');
|
|
const user = this.userId && await Meteor.users.findOneAsync(this.userId);
|
|
if (!user?.isAdmin) throw new Meteor.Error('not-authorized');
|
|
if (!base64 || base64.length > Math.ceil(MAX_LOGO_BYTES * 4 / 3) + 4 || !/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(base64)) throw new Meteor.Error('invalid-logo-data');
|
|
const bytes = Buffer.from(base64, 'base64');
|
|
const type = siteLogoImageType(bytes);
|
|
if (!type || bytes.length > MAX_LOGO_BYTES) throw new Meteor.Error('invalid-logo-image', 'Upload a PNG, JPEG, GIF or WebP image smaller than 2 MB');
|
|
const setting = await Settings.findOneAsync();
|
|
if (!setting) throw new Meteor.Error('setting-not-found');
|
|
const storageSetting = await AttachmentStorageSettings.findOneAsync({});
|
|
if (storageSetting?.limitSettings?.attachmentsUploadBlocked === true) throw new Meteor.Error('uploads-disabled');
|
|
// Attachments.onAfterUpload validates the bytes and moves the file to the
|
|
// administrator's Default Storage, just as it does for board backgrounds.
|
|
// writeAsync runs it; insertAsync exists only on Meteor-Files' client, so
|
|
// on the server every logo upload failed with "is not a function".
|
|
const uploader = await Attachments.writeAsync(bytes, {fileName:`site-${kind}-logo${type[1]}`, type:type[0], userId:this.userId, meta:{source:'site-logo', fileId:new ObjectId().toString()}}, true);
|
|
if (!uploader?._id) throw new Meteor.Error('upload-failed');
|
|
const prefix = new URL(Meteor.absoluteUrl()).pathname.replace(/\/$/, '');
|
|
const url = `${prefix}/cdn/storage/attachments/${encodeURIComponent(uploader._id)}`;
|
|
await Settings.updateAsync(setting._id, {$set:{[field]:url}});
|
|
return url;
|
|
},
|
|
});
|