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';
|
2022-01-30 15:26:11 +03:00
|
|
|
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';
|
|
|
|
|
|
2020-09-16 14:39:06 -05:00
|
|
|
let avatarsBucket;
|
|
|
|
|
if (Meteor.isServer) {
|
|
|
|
|
avatarsBucket = createBucket('avatars');
|
|
|
|
|
}
|
2020-09-13 17:05:40 -05:00
|
|
|
|
2020-09-16 18:39:57 -05:00
|
|
|
Avatars = new FilesCollection({
|
2020-09-13 17:05:40 -05:00
|
|
|
debug: false, // Change to `true` for debugging
|
|
|
|
|
collectionName: 'avatars',
|
2020-09-16 14:39:06 -05:00
|
|
|
allowClientCode: true,
|
2022-01-30 15:26:11 +03:00
|
|
|
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/')) {
|
2020-09-17 01:41:29 -05:00
|
|
|
return true;
|
2020-09-17 01:57:58 -05:00
|
|
|
}
|
2020-09-17 01:41:29 -05:00
|
|
|
return 'avatar-too-big';
|
2015-09-03 23:12:46 +02:00
|
|
|
},
|
2020-09-13 17:05:40 -05:00
|
|
|
onAfterUpload: createOnAfterUpload(avatarsBucket),
|
|
|
|
|
interceptDownload: createInterceptDownload(avatarsBucket),
|
|
|
|
|
onAfterRemove: createOnAfterRemove(avatarsBucket),
|
2015-06-08 11:47:06 +02:00
|
|
|
});
|
|
|
|
|
|
2020-09-13 17:05:40 -05:00
|
|
|
function isOwner(userId, doc) {
|
|
|
|
|
return userId && userId === doc.userId;
|
2015-09-03 23:12:46 +02:00
|
|
|
}
|
2015-06-08 11:47:06 +02:00
|
|
|
|
2020-09-16 14:39:06 -05:00
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-09-16 14:39:06 -05:00
|
|
|
}
|
2015-06-08 11:47:06 +02:00
|
|
|
|
2019-06-26 17:47:27 -05:00
|
|
|
export default Avatars;
|