Ref: Attachment upload handlers

This commit is contained in:
David Arnold 2020-09-13 22:17:58 -05:00
parent fb96cebc0b
commit 8d8ec8a70c
No known key found for this signature in database
GPG key ID: 6D6A936E69C59D08
3 changed files with 41 additions and 86 deletions

View file

@ -54,50 +54,28 @@ Template.previewAttachedImagePopup.events({
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()) {
card.setCover(attachment._id);
if (event.currentTarget.files && event.currentTarget.files[0]) {
const uploader = Attachments.insert(
{
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.close();
});
};
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);
}
});
uploader.start();
}
},
'click .js-computer-upload'(event, templateInstance) {
templateInstance.find('.js-attach-file').click();

View file

@ -149,39 +149,26 @@ Template.editor.onRendered(() => {
img.setAttribute('width', '100%');
$summernote.summernote('insertNode', img);
};
const processData = function(fileObj) {
Utils.processUploadedAttachment(
currentCard,
fileObj,
attachment => {
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();
if (url) {
insertImage(
`${location.protocol}//${location.host}${url}`,
);
} else {
retry++;
if (retry < maxTry) {
setTimeout(checkUrl, checkItvl);
}
}
};
checkUrl();
});
}
const processUpload = function(file) {
const uploader = Attachments.insert(
{
file,
streams: 'dynamic',
chunkSize: 'dynamic',
},
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) {
const reader = new FileReader();
@ -197,7 +184,7 @@ Template.editor.onRendered(() => {
callback(blob) {
if (blob !== false) {
blob.name = image.name;
processData(blob);
processUpload(blob);
}
},
});
@ -205,7 +192,7 @@ Template.editor.onRendered(() => {
};
reader.readAsDataURL(image);
} else {
processData(image);
processUpload(image);
}
}
},