Trying to upload an attachment with Meteor-Files

This commit is contained in:
Romulus Urakagi Tsai 2019-11-18 01:47:26 +00:00
parent 9fd14f7ecb
commit 05c53ca01d
8 changed files with 132 additions and 84 deletions

View file

@ -96,3 +96,4 @@ konecty:mongo-counter
percolate:synced-cron
easylogic:summernote
cfs:filesystem
ostrio:files

View file

@ -133,6 +133,8 @@ oauth2@1.2.1
observe-sequence@1.0.16
ongoworks:speakingurl@1.1.0
ordered-dict@1.1.0
ostrio:cookies@2.5.0
ostrio:files@1.13.0
peerlibrary:assert@0.2.5
peerlibrary:base-component@0.16.0
peerlibrary:blaze-components@0.15.1

View file

@ -149,20 +149,28 @@ Template.previewClipboardImagePopup.events({
if (results && results.file) {
window.oPasted = pastedResults;
const card = this;
const file = new FS.File(results.file);
const settings = {
file: results.file,
streams: 'dynamic',
chunkSize: 'dynamic'
};
if (!results.name) {
// if no filename, it's from clipboard. then we give it a name, with ext name from MIME type
// FIXME: Check this behavior
if (typeof results.file.type === 'string') {
file.name(results.file.type.replace('image/', 'clipboard.'));
settings.fileName = new Date().getTime() + results.file.type.replace('.+/', '');
}
}
file.updatedAt(new Date());
file.boardId = card.boardId;
file.cardId = card._id;
file.userId = Meteor.userId();
const attachment = Attachments.insert(file);
settings.meta = {};
settings.meta.updatedAt = new Date().getTime();
settings.meta.boardId = card.boardId;
settings.meta.cardId = card._id;
settings.meta.userId = Meteor.userId();
console.log('settings', settings);
const attachment = Attachments.insert(settings, false);
if (attachment && attachment._id && attachment.isImage()) {
// TODO: Check image cover behavior
if (attachment && attachment._id && attachment.isImage) {
card.setCover(attachment._id);
}

View file

@ -34,21 +34,27 @@ Utils = {
if (!card) {
return next();
}
const file = new FS.File(fileObj);
let settings = {
file: fileObj,
streams: 'dynamic',
chunkSize: 'dynamic'
};
settings.meta = {};
if (card.isLinkedCard()) {
file.boardId = Cards.findOne(card.linkedId).boardId;
file.cardId = card.linkedId;
settings.meta.boardId = Cards.findOne(card.linkedId).boardId;
settings.meta.cardId = card.linkedId;
} else {
file.boardId = card.boardId;
file.swimlaneId = card.swimlaneId;
file.listId = card.listId;
file.cardId = card._id;
settings.meta.boardId = card.boardId;
settings.meta.swimlaneId = card.swimlaneId;
settings.meta.listId = card.listId;
settings.meta.cardId = card._id;
}
file.userId = Meteor.userId();
if (file.original) {
settings.meta.userId = Meteor.userId();
// FIXME: What is this?
/* if (file.original) {
file.original.name = fileObj.name;
}
return next(Attachments.insert(file));
}*/
return next(Attachments.insert(settings, false));
},
shrinkImage(options) {
// shrink image to certain size

View file

@ -1,3 +1,29 @@
import { FilesCollection } from 'meteor/ostrio:files';
Attachments = new FilesCollection({
storagePath: storagePath(),
debug: true, // FIXME: Remove debug mode
collectionName: 'attachments2',
allowClientCode: false, // Disallow remove files from Client
});
if (Meteor.isServer) {
Meteor.startup(() => {
Attachments.collection._ensureIndex({ cardId: 1 });
});
// TODO: Permission related
// TODO: Add Activity update
// TODO: publish and subscribe
// Meteor.publish('files.attachments.all', function () {
// return Attachments.find().cursor;
// });
} else {
// Meteor.subscribe('files.attachments.all');
}
// ---------- Deprecated fallback ---------- //
const localFSStore = process.env.ATTACHMENTS_STORE_PATH;
const storeName = 'attachments';
const defaultStoreOptions = {
@ -171,16 +197,16 @@ if (localFSStore) {
...defaultStoreOptions,
});
}
Attachments = new FS.Collection('attachments', {
DeprecatedAttachs = new FS.Collection('attachments', {
stores: [store],
});
if (Meteor.isServer) {
Meteor.startup(() => {
Attachments.files._ensureIndex({ cardId: 1 });
DeprecatedAttachs.files._ensureIndex({ cardId: 1 });
});
Attachments.allow({
DeprecatedAttachs.allow({
insert(userId, doc) {
return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
},
@ -206,10 +232,10 @@ if (Meteor.isServer) {
});
}
// XXX Enforce a schema for the Attachments CollectionFS
// XXX Enforce a schema for the DeprecatedAttachs CollectionFS
if (Meteor.isServer) {
Attachments.files.after.insert((userId, doc) => {
DeprecatedAttachs.files.after.insert((userId, doc) => {
// If the attachment doesn't have a source field
// or its source is different than import
if (!doc.source || doc.source !== 'import') {
@ -227,7 +253,7 @@ if (Meteor.isServer) {
} else {
// Don't add activity about adding the attachment as the activity
// be imported and delete source field
Attachments.update(
DeprecatedAttachs.update(
{
_id: doc._id,
},
@ -240,7 +266,7 @@ if (Meteor.isServer) {
}
});
Attachments.files.before.remove((userId, doc) => {
DeprecatedAttachs.files.before.remove((userId, doc) => {
Activities.insert({
userId,
type: 'card',
@ -253,11 +279,16 @@ if (Meteor.isServer) {
});
});
Attachments.files.after.remove((userId, doc) => {
DeprecatedAttachs.files.after.remove((userId, doc) => {
Activities.remove({
attachmentId: doc._id,
});
});
}
function storagePath(defaultPath) {
const storePath = process.env.ATTACHMENTS_STORE_PATH;
return storePath ? storePath : defaultPath;
}
export default Attachments;

View file

@ -128,7 +128,7 @@ Meteor.publishRelations('board', function(boardId, isArchived) {
// Gather queries and send in bulk
const cardComments = this.join(CardComments);
cardComments.selector = _ids => ({ cardId: _ids });
const attachments = this.join(Attachments);
const attachments = this.join(Attachments.collection);
attachments.selector = _ids => ({ cardId: _ids });
const checklists = this.join(Checklists);
checklists.selector = _ids => ({ cardId: _ids });