diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.js index 1af80b72b..4d9ddafc4 100644 --- a/packages/core/api/sync/index.js +++ b/packages/core/api/sync/index.js @@ -153,18 +153,23 @@ export default class Sync { async _uploadAttachments() { const attachments = this._db.attachments.pending; - console.log("Uploading attachments", this._db.attachments.pending); - for (var i = 0; i < attachments.length; ++i) { - sendAttachmentsProgressEvent("upload", attachments.length, i); + try { + console.log("Uploading attachments", this._db.attachments.pending); + for (var i = 0; i < attachments.length; ++i) { + sendAttachmentsProgressEvent("upload", attachments.length, i); - const attachment = attachments[i]; - const { hash } = attachment.metadata; + const attachment = attachments[i]; + const { hash } = attachment.metadata; - const isUploaded = await this._db.fs.uploadFile(hash, hash); - if (!isUploaded) throw new Error("Failed to upload file."); + const isUploaded = await this._db.fs.uploadFile(hash, hash); + if (!isUploaded) throw new Error("Failed to upload file."); - await this._db.attachments.markAsUploaded(attachment.id); + await this._db.attachments.markAsUploaded(attachment.id); + } + } catch (e) { + throw new Error("Failed to upload attachments. Error: " + e.message); + } finally { + sendAttachmentsProgressEvent("upload", attachments.length); } - sendAttachmentsProgressEvent("upload", attachments.length); } } diff --git a/packages/core/collections/attachments.js b/packages/core/collections/attachments.js index a20b7205c..ffcf7fb0f 100644 --- a/packages/core/collections/attachments.js +++ b/packages/core/collections/attachments.js @@ -179,7 +179,9 @@ export default class Attachments extends Collection { get pending() { return this.all.filter( - (attachment) => attachment.dateUploaded <= 0 || !attachment.dateUploaded + (attachment) => + (attachment.dateUploaded <= 0 || !attachment.dateUploaded) && + attachment.noteIds.length > 0 ); } diff --git a/packages/core/database/fs.js b/packages/core/database/fs.js index b2a50c8de..f583d2688 100644 --- a/packages/core/database/fs.js +++ b/packages/core/database/fs.js @@ -28,12 +28,13 @@ export default class FileStorage { } async cancel(groupId, type = undefined) { + console.trace("Cancelling", groupId, type); await Promise.all( this._queue .filter( (item) => item.groupId === groupId && (!type || item.type === type) ) - .map(async (op) => await op.cancel()) + .map(async (op) => await op.cancel("Operation canceled.")) ); }