2020-09-13 17:05:40 -05:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { FilesCollection } from 'meteor/ostrio:files';
|
2022-04-03 15:09:20 +02:00
|
|
|
import { createBucket } from './lib/grid/createBucket';
|
2022-02-16 22:20:35 +01:00
|
|
|
import fs from 'fs';
|
2022-01-30 15:26:11 +03:00
|
|
|
import path from 'path';
|
2022-04-03 15:09:20 +02:00
|
|
|
import FileStoreStrategyFactory, { FileStoreStrategyFilesystem, FileStoreStrategyGridFs} from '/models/lib/fileStoreStrategy';
|
2020-09-13 17:05:40 -05:00
|
|
|
|
2020-09-16 14:39:06 -05:00
|
|
|
let avatarsBucket;
|
|
|
|
if (Meteor.isServer) {
|
|
|
|
avatarsBucket = createBucket('avatars');
|
|
|
|
}
|
2020-09-13 17:05:40 -05:00
|
|
|
|
2022-04-03 15:09:20 +02:00
|
|
|
const fileStoreStrategyFactory = new FileStoreStrategyFactory(FileStoreStrategyFilesystem, FileStoreStrategyGridFs, avatarsBucket);
|
|
|
|
|
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() {
|
2022-04-03 15:14:38 +02:00
|
|
|
const ret = path.join(process.env.WRITABLE_PATH, 'avatars');
|
|
|
|
return ret;
|
2022-01-30 15:26:11 +03:00
|
|
|
},
|
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
|
|
|
},
|
2022-03-25 14:08:37 +01:00
|
|
|
onAfterUpload(fileObj) {
|
|
|
|
Object.keys(fileObj.versions).forEach(versionName => {
|
2022-04-03 15:09:20 +02:00
|
|
|
fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).onAfterUpload();
|
2022-03-25 14:08:37 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
interceptDownload(http, fileObj, versionName) {
|
2022-04-03 15:09:20 +02:00
|
|
|
const ret = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).interceptDownload(http);
|
2022-03-25 14:08:37 +01:00
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
onAfterRemove(files) {
|
|
|
|
files.forEach(fileObj => {
|
|
|
|
Object.keys(fileObj.versions).forEach(versionName => {
|
2022-04-03 15:09:20 +02:00
|
|
|
fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).onAfterRemove();
|
2022-03-25 14:08:37 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
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;
|