From 4dcdec0084414e7dde9e630add01ecd2865bd941 Mon Sep 17 00:00:00 2001 From: Romulus Urakagi Tsai Date: Wed, 20 Nov 2019 10:40:09 +0000 Subject: Attachment upload from card done, need to fix download link --- client/components/cards/attachments.js | 23 ++++++++++++++++++----- client/components/main/editor.js | 8 ++------ client/lib/utils.js | 14 +++++++++++--- models/attachments.js | 10 +++++----- models/cards.js | 12 +++++++----- models/export.js | 2 +- models/trelloCreator.js | 1 + models/wekanCreator.js | 2 ++ server/migrations.js | 4 ++-- 9 files changed, 49 insertions(+), 27 deletions(-) diff --git a/client/components/cards/attachments.js b/client/components/cards/attachments.js index 604dc078..f24a7f82 100644 --- a/client/components/cards/attachments.js +++ b/client/components/cards/attachments.js @@ -45,18 +45,31 @@ Template.attachmentsGalery.events({ }, }); +Template.attachmentsGalery.helpers({ + url() { + return Attachments.link(this); + } +}); + Template.previewAttachedImagePopup.events({ 'click .js-large-image-clicked'() { Popup.close(); }, }); +Template.previewAttachedImagePopup.helpers({ + url() { + return Attachments.link(this); + } +}); + Template.cardAttachmentsPopup.events({ 'change .js-attach-file'(event) { const card = this; const processFile = f => { Utils.processUploadedAttachment(card, f, attachment => { - if (attachment && attachment._id && attachment.isImage()) { + console.log('attachment', attachment); + if (attachment && attachment._id && attachment.isImage) { card.setCover(attachment._id); } Popup.close(); @@ -152,13 +165,14 @@ Template.previewClipboardImagePopup.events({ const settings = { file: results.file, streams: 'dynamic', - chunkSize: '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') { - settings.fileName = new Date().getTime() + results.file.type.replace('.+/', ''); + settings.fileName = + new Date().getTime() + results.file.type.replace('.+/', ''); } } settings.meta = {}; @@ -166,8 +180,7 @@ Template.previewClipboardImagePopup.events({ 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); + const attachment = Attachments.insert(settings); // TODO: Check image cover behavior if (attachment && attachment._id && attachment.isImage) { diff --git a/client/components/main/editor.js b/client/components/main/editor.js index 39c03aa9..bd110868 100755 --- a/client/components/main/editor.js +++ b/client/components/main/editor.js @@ -229,18 +229,14 @@ Template.editor.onRendered(() => { currentCard, fileObj, attachment => { - if ( - attachment && - attachment._id && - attachment.isImage() - ) { + if (attachment && attachment._id && attachment.isImage) { attachment.one('uploaded', function() { const maxTry = 3; const checkItvl = 500; let retry = 0; const checkUrl = function() { // even though uploaded event fired, attachment.url() is still null somehow //TODO - const url = attachment.url(); + const url = attachment.link(); if (url) { insertImage( `${location.protocol}//${location.host}${url}`, diff --git a/client/lib/utils.js b/client/lib/utils.js index 2694396f..d667382d 100644 --- a/client/lib/utils.js +++ b/client/lib/utils.js @@ -37,7 +37,15 @@ Utils = { let settings = { file: fileObj, streams: 'dynamic', - chunkSize: 'dynamic' + chunkSize: 'dynamic', + onUploaded: function(error, fileObj) { + console.log('after insert', Attachments.find({}).fetch()); + if (error) { + console.log('Error while upload', error); + } else { + next(fileObj); + } + }, }; settings.meta = {}; if (card.isLinkedCard()) { @@ -51,10 +59,10 @@ Utils = { } settings.meta.userId = Meteor.userId(); // FIXME: What is this? -/* if (file.original) { + /* if (file.original) { file.original.name = fileObj.name; }*/ - return next(Attachments.insert(settings, false)); + Attachments.insert(settings); }, shrinkImage(options) { // shrink image to certain size diff --git a/models/attachments.js b/models/attachments.js index fd03e6d2..4537e47c 100644 --- a/models/attachments.js +++ b/models/attachments.js @@ -4,7 +4,7 @@ Attachments = new FilesCollection({ storagePath: storagePath(), debug: true, // FIXME: Remove debug mode collectionName: 'attachments2', - allowClientCode: false, // Disallow remove files from Client + allowClientCode: true, // FIXME: Permissions }); if (Meteor.isServer) { @@ -15,11 +15,11 @@ if (Meteor.isServer) { // TODO: Permission related // TODO: Add Activity update // TODO: publish and subscribe -// Meteor.publish('files.attachments.all', function () { -// return Attachments.find().cursor; -// }); + Meteor.publish('attachments', function() { + return Attachments.find().cursor; + }); } else { -// Meteor.subscribe('files.attachments.all'); + Meteor.subscribe('attachments'); } // ---------- Deprecated fallback ---------- // diff --git a/models/cards.js b/models/cards.js index 3944b09f..4c3e2c99 100644 --- a/models/cards.js +++ b/models/cards.js @@ -366,7 +366,7 @@ Cards.helpers({ // Copy attachments oldCard.attachments().forEach(att => { - att.cardId = _id; + att.meta.cardId = _id; delete att._id; return Attachments.insert(att); }); @@ -456,14 +456,16 @@ Cards.helpers({ attachments() { if (this.isLinkedCard()) { return Attachments.find( - { cardId: this.linkedId }, + { 'meta.cardId': this.linkedId }, { sort: { uploadedAt: -1 } }, ); } else { - return Attachments.find( - { cardId: this._id }, + const ret = Attachments.find( + { 'meta.cardId': this._id }, { sort: { uploadedAt: -1 } }, ); + if (ret.first()) console.log('link', Attachments.link(ret.first())); + return ret; } }, @@ -471,7 +473,7 @@ Cards.helpers({ const cover = Attachments.findOne(this.coverId); // if we return a cover before it is fully stored, we will get errors when we try to display it // todo XXX we could return a default "upload pending" image in the meantime? - return cover && cover.url() && cover; + return cover && cover.link(); }, checklists() { diff --git a/models/export.js b/models/export.js index cc979ce0..c93a8bda 100644 --- a/models/export.js +++ b/models/export.js @@ -162,7 +162,7 @@ export class Exporter { readStream.pipe(tmpWriteable); }; const getBase64DataSync = Meteor.wrapAsync(getBase64Data); - result.attachments = Attachments.find(byBoard) + result.attachments = Attachments.find({ 'meta.boardId': byBoard.boardId }) .fetch() .map(attachment => { return { diff --git a/models/trelloCreator.js b/models/trelloCreator.js index cb1a6a67..b38e4652 100644 --- a/models/trelloCreator.js +++ b/models/trelloCreator.js @@ -345,6 +345,7 @@ export class TrelloCreator { // so we make it server only, and let UI catch up once it is done, forget about latency comp. const self = this; if (Meteor.isServer) { + // FIXME: Change to new model file.attachData(att.url, function(error) { file.boardId = boardId; file.cardId = cardId; diff --git a/models/wekanCreator.js b/models/wekanCreator.js index ec85d93f..175c156d 100644 --- a/models/wekanCreator.js +++ b/models/wekanCreator.js @@ -415,6 +415,7 @@ export class WekanCreator { const self = this; if (Meteor.isServer) { if (att.url) { + // FIXME: Change to new file library file.attachData(att.url, function(error) { file.boardId = boardId; file.cardId = cardId; @@ -440,6 +441,7 @@ export class WekanCreator { } }); } else if (att.file) { + // FIXME: Change to new file library file.attachData( new Buffer(att.file, 'base64'), { diff --git a/server/migrations.js b/server/migrations.js index 836220f3..7d5a5cca 100644 --- a/server/migrations.js +++ b/server/migrations.js @@ -80,7 +80,7 @@ Migrations.add('lowercase-board-permission', () => { Migrations.add('change-attachments-type-for-non-images', () => { const newTypeForNonImage = 'application/octet-stream'; Attachments.find().forEach(file => { - if (!file.isImage()) { + if (!file.isImage) { Attachments.update( file._id, { @@ -97,7 +97,7 @@ Migrations.add('change-attachments-type-for-non-images', () => { Migrations.add('card-covers', () => { Cards.find().forEach(card => { - const cover = Attachments.findOne({ cardId: card._id, cover: true }); + const cover = Attachments.findOne({ 'meta.cardId': card._id, cover: true }); if (cover) { Cards.update(card._id, { $set: { coverId: cover._id } }, noValidate); } -- cgit v1.2.3-1-g7c22