From 3363cb448ae8c6561821465c9b5588a4d4da5dc0 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 29 Dec 2023 05:13:17 -0500 Subject: [PATCH] Local API: Attachment size fixes (#4270) - Don't fail on missing attachment - Get size of main file only - Include best-attachment `attachmentSize` for parent items --- chrome/content/zotero/xpcom/data/item.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index a95439c576..70e57d2d17 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -5517,6 +5517,21 @@ Zotero.Item.prototype.toResponseJSON = function (options = {}) { Zotero.Item.prototype.toResponseJSONAsync = async function (options = {}) { + async function getFileSize(attachment) { + let path = attachment.getFilePath(); + if (path) { + try { + return (await IOUtils.stat(path)).size; + } + catch (e) { + if (e.name != 'NotFoundError') { + throw e; + } + } + } + return undefined + } + let json = this.toResponseJSON(options); if (this.isRegularItem()) { let bestAttachment = await this.getBestAttachment(); @@ -5526,10 +5541,11 @@ Zotero.Item.prototype.toResponseJSONAsync = async function (options = {}) { type: 'application/json', attachmentType: bestAttachment.attachmentContentType }; + json.links.attachment.attachmentSize = await getFileSize(bestAttachment); } } else if (this.isImportedAttachment()) { - json.links.enclosure.length = await Zotero.Attachments.getTotalFileSize(this); + json.links.enclosure.length = await getFileSize(this); } return json; };