Ref: Attachment upload handlers

This commit is contained in:
David Arnold 2020-09-13 22:17:58 -05:00 committed by Denis Perov
parent 9c3043d40a
commit 55acce9f0c
3 changed files with 41 additions and 88 deletions

View file

@ -63,50 +63,28 @@ Template.previewAttachedImagePopup.events({
Template.cardAttachmentsPopup.events({ Template.cardAttachmentsPopup.events({
'change .js-attach-file'(event) { 'change .js-attach-file'(event) {
const card = this; const card = this;
const processFile = f => { if (event.currentTarget.files && event.currentTarget.files[0]) {
Utils.processUploadedAttachment(card, f, attachment => { const uploader = Attachments.insert(
if (attachment && attachment._id && attachment.isImage()) { {
card.setCover(attachment._id); file: event.currentTarget.files[0],
streams: 'dynamic',
chunkSize: 'dynamic',
},
false,
);
uploader.on('uploaded', (error, fileObj) => {
if (!error) {
if (fileObj.isImage) {
card.setCover(fileObj._id);
}
Utils.addCommonMetaToAttachment(card, fileObj);
} }
});
uploader.on('end', (error, fileObj) => {
Popup.back(); Popup.back();
}); });
}; uploader.start();
}
FS.Utility.eachFile(event, f => {
if (
MAX_IMAGE_PIXEL > 0 &&
typeof f.type === 'string' &&
f.type.match(/^image/)
) {
// is image
const reader = new FileReader();
reader.onload = function(e) {
const dataurl = e && e.target && e.target.result;
if (dataurl !== undefined) {
Utils.shrinkImage({
dataurl,
maxSize: MAX_IMAGE_PIXEL,
ratio: COMPRESS_RATIO,
toBlob: true,
callback(blob) {
if (blob === false) {
processFile(f);
} else {
blob.name = f.name;
processFile(blob);
}
},
});
} else {
// couldn't process it let other function handle it?
processFile(f);
}
};
reader.readAsDataURL(f);
} else {
processFile(f);
}
});
}, },
'click .js-computer-upload'(event, templateInstance) { 'click .js-computer-upload'(event, templateInstance) {
templateInstance.find('.js-attach-file').click(); templateInstance.find('.js-attach-file').click();

View file

@ -153,7 +153,6 @@ BlazeComponent.extendComponent({
}); });
} }
}, },
onImageUpload(files) { onImageUpload(files) {
const $summernote = getSummernote(this); const $summernote = getSummernote(this);
if (files && files.length > 0) { if (files && files.length > 0) {
@ -162,45 +161,31 @@ BlazeComponent.extendComponent({
const MAX_IMAGE_PIXEL = Utils.MAX_IMAGE_PIXEL; const MAX_IMAGE_PIXEL = Utils.MAX_IMAGE_PIXEL;
const COMPRESS_RATIO = Utils.IMAGE_COMPRESS_RATIO; const COMPRESS_RATIO = Utils.IMAGE_COMPRESS_RATIO;
const insertImage = src => { const insertImage = src => {
// process all image upload types to the description/comment window
const img = document.createElement('img'); const img = document.createElement('img');
img.src = src; img.src = src;
img.setAttribute('width', '100%'); img.setAttribute('width', '100%');
$summernote.summernote('insertNode', img); $summernote.summernote('insertNode', img);
}; };
const processData = function(fileObj) { const processUpload = function(file) {
Utils.processUploadedAttachment( const uploader = Attachments.insert(
currentCard, {
fileObj, file,
attachment => { streams: 'dynamic',
if ( chunkSize: 'dynamic',
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();
if (url) {
insertImage(
`${location.protocol}//${location.host}${url}`,
);
} else {
retry++;
if (retry < maxTry) {
setTimeout(checkUrl, checkItvl);
}
}
};
checkUrl();
});
}
}, },
false,
); );
uploader.on('uploaded', (error, fileObj) => {
if (!error) {
if (fileObj.isImage) {
insertImage(
`${location.protocol}//${location.host}${fileObj.path}`,
);
}
Utils.addCommonMetaToAttachment(currentCard, fileObj);
}
});
uploader.start();
}; };
if (MAX_IMAGE_PIXEL) { if (MAX_IMAGE_PIXEL) {
const reader = new FileReader(); const reader = new FileReader();
@ -216,7 +201,7 @@ BlazeComponent.extendComponent({
callback(blob) { callback(blob) {
if (blob !== false) { if (blob !== false) {
blob.name = image.name; blob.name = image.name;
processData(blob); processUpload(blob);
} }
}, },
}); });
@ -224,7 +209,7 @@ BlazeComponent.extendComponent({
}; };
reader.readAsDataURL(image); reader.readAsDataURL(image);
} else { } else {
processData(image); processUpload(image);
} }
} }
}, },

View file

@ -164,16 +164,7 @@ Utils = {
}, },
MAX_IMAGE_PIXEL: Meteor.settings.public.MAX_IMAGE_PIXEL, MAX_IMAGE_PIXEL: Meteor.settings.public.MAX_IMAGE_PIXEL,
COMPRESS_RATIO: Meteor.settings.public.IMAGE_COMPRESS_RATIO, COMPRESS_RATIO: Meteor.settings.public.IMAGE_COMPRESS_RATIO,
processUploadedAttachment(card, fileObj, callback) { addCommonMetaToAttachment(card, file) {
const next = attachment => {
if (typeof callback === 'function') {
callback(attachment);
}
};
if (!card) {
return next();
}
const file = new FS.File(fileObj);
if (card.isLinkedCard()) { if (card.isLinkedCard()) {
file.boardId = Cards.findOne(card.linkedId).boardId; file.boardId = Cards.findOne(card.linkedId).boardId;
file.cardId = card.linkedId; file.cardId = card.linkedId;
@ -185,9 +176,8 @@ Utils = {
} }
file.userId = Meteor.userId(); file.userId = Meteor.userId();
if (file.original) { if (file.original) {
file.original.name = fileObj.name; file.original.name = file.name;
} }
return next(Attachments.insert(file));
}, },
shrinkImage(options) { shrinkImage(options) {
// shrink image to certain size // shrink image to certain size