wekan/models/avatars.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-09-13 17:05:40 -05:00
import { Meteor } from 'meteor/meteor';
import { FilesCollection } from 'meteor/ostrio:files';
2022-02-16 22:20:35 +01:00
import fs from 'fs';
import path from 'path';
2020-09-13 17:05:40 -05:00
import { createBucket } from './lib/grid/createBucket';
import { createOnAfterUpload } from './lib/fsHooks/createOnAfterUpload';
import { createInterceptDownload } from './lib/fsHooks/createInterceptDownload';
import { createOnAfterRemove } from './lib/fsHooks/createOnAfterRemove';
let avatarsBucket;
if (Meteor.isServer) {
avatarsBucket = createBucket('avatars');
}
2020-09-13 17:05:40 -05:00
Avatars = new FilesCollection({
2020-09-13 17:05:40 -05:00
debug: false, // Change to `true` for debugging
collectionName: 'avatars',
allowClientCode: true,
storagePath() {
if (process.env.WRITABLE_PATH) {
return path.join(process.env.WRITABLE_PATH, 'uploads', 'avatars');
}
return path.normalize(`assets/app/uploads/${this.collectionName}`);;
},
2020-09-13 17:05:40 -05:00
onBeforeUpload(file) {
2020-09-17 01:57:58 -05:00
if (file.size <= 72000 && file.type.startsWith('image/')) {
return true;
2020-09-17 01:57:58 -05:00
}
return 'avatar-too-big';
},
2020-09-13 17:05:40 -05:00
onAfterUpload: createOnAfterUpload(avatarsBucket),
interceptDownload: createInterceptDownload(avatarsBucket),
onAfterRemove: createOnAfterRemove(avatarsBucket),
});
2020-09-13 17:05:40 -05:00
function isOwner(userId, doc) {
return userId && userId === doc.userId;
}
if (Meteor.isServer) {
Avatars.allow({
insert: isOwner,
update: isOwner,
remove: isOwner,
fetch: ['userId'],
});
2022-02-16 22:20:35 +01:00
Meteor.startup(() => {
const storagePath = Avatars.storagePath();
if (!fs.existsSync(storagePath)) {
console.log("create storagePath because it doesn't exist: " + storagePath);
fs.mkdirSync(storagePath, { recursive: true });
}
});
}
export default Avatars;