Ref: Avatars to use modern gridfs

This commit is contained in:
David Arnold 2020-09-13 17:05:40 -05:00
parent f650a17cd8
commit 2f605c9a4c
No known key found for this signature in database
GPG key ID: 6D6A936E69C59D08

View file

@ -1,29 +1,34 @@
Avatars = new FS.Collection('avatars', {
stores: [new FS.Store.GridFS('avatars')],
filter: {
maxSize: 72000,
allow: {
contentTypes: ['image/*'],
},
import { Meteor } from 'meteor/meteor';
import { FilesCollection } from 'meteor/ostrio:files';
import { createBucket } from './lib/grid/createBucket';
import { createOnAfterUpload } from './lib/fsHooks/createOnAfterUpload';
import { createInterceptDownload } from './lib/fsHooks/createInterceptDownload';
import { createOnAfterRemove } from './lib/fsHooks/createOnAfterRemove';
const avatarsBucket = createBucket('avatars');
export const Avatars = new FilesCollection({
debug: false, // Change to `true` for debugging
collectionName: 'avatars',
allowClientCode: false,
onBeforeUpload(file) {
if (file.size <= 72000 && file.isImage) return true;
return 'Please upload image, with size equal or less than 72KB';
},
onAfterUpload: createOnAfterUpload(avatarsBucket),
interceptDownload: createInterceptDownload(avatarsBucket),
onAfterRemove: createOnAfterRemove(avatarsBucket),
});
function isOwner(userId, file) {
return userId && userId === file.userId;
function isOwner(userId, doc) {
return userId && userId === doc.userId;
}
Avatars.allow({
insert: isOwner,
update: isOwner,
remove: isOwner,
download() {
return true;
},
fetch: ['userId'],
});
Avatars.files.before.insert((userId, doc) => {
doc.userId = userId;
});
export default Avatars;