Files
wekan/models/lib/fsHooks/createOnAfterUpload.js
T
Lauri Ojansivu 582a22c291 Fix GitHub code scanning code-quality findings.
Real faults: a card's original-position description formatted a promise
because it did not await the history; the attachment swipe distances and
two drag-scroll locals were undeclared globals; a file that cannot be read
during a GridFS upload left the upload open; api.py called exit() instead of
sys.exit(); a rules test redefined a property and a security-log test kept
an unreachable branch. Also 31 missing semicolons, unused imports in the
card details, exporters and attachment API, unused Python imports, and
comments on empty except clauses. The list-sync scanner test cut its source
at the last Meteor.startup, which the new index startup of #6855 had moved.

Thanks to GitHub CodeQL and xet7 !
2026-11-02 14:15:02 +02:00

60 lines
2.1 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import fs from 'fs';
export const createOnAfterUpload = bucket =>
function onAfterUpload(file) {
const self = this;
// here you could manipulate your file
// and create a new version, for example a scaled 'thumbnail'
// ...
// then we read all versions we have got so far
Object.keys(file.versions).forEach(versionName => {
const metadata = { ...file.meta, versionName, fileId: file._id };
const source = fs.createReadStream(file.versions[versionName].path);
// pipe() does not pass a read error on to the upload stream: without
// this a file that cannot be read left the upload open and the file in place.
source.on('error', () => {
self.unlink(file, versionName);
});
source
// this is where we upload the binary to the bucket using bucket.openUploadStream
// see http://mongodb.github.io/node-mongodb-native/3.2/api/GridFSBucket.html#openUploadStream
.pipe(
bucket.openUploadStream(file.name, {
contentType: file.type || 'binary/octet-stream',
metadata,
}),
)
// and we unlink the file from the fs on any error
// that occurred during the upload to prevent zombie files
.on('error', err => {
// console.error("[createOnAfterUpload error]", err);
self.unlink(file, versionName); // Unlink files from FS
})
// once we are finished, we attach the gridFS Object id on the
// FilesCollection document's meta section and finally unlink the
// upload file from the filesystem
.on(
'finish',
ver => {
const property = `versions.${versionName}.meta.gridFsFileId`;
self.collection.updateAsync(file._id, {
$set: {
[property]: ver._id.toHexString(),
},
}).catch(err => {
console.error('[createOnAfterUpload update error]', err);
});
self.unlink(file, versionName); // Unlink files from FS
},
);
});
};