Fix uploading of embedded-image attachments

This commit is contained in:
Dan Stillman 2020-12-25 03:01:37 -05:00
parent 51f760fe1a
commit 60cb299f40
4 changed files with 37 additions and 7 deletions

View file

@ -2291,6 +2291,9 @@ Zotero.Attachments = new function(){
case Zotero.Attachments.LINK_MODE_IMPORTED_FILE:
break;
case Zotero.Attachments.LINK_MODE_EMBEDDED_IMAGE:
return false;
default:
throw new Error("Invalid attachment link mode");
}

View file

@ -2319,8 +2319,7 @@ Zotero.Item.prototype.getFilePath = function () {
}
// Imported file with relative path
if (linkMode == Zotero.Attachments.LINK_MODE_IMPORTED_URL ||
linkMode == Zotero.Attachments.LINK_MODE_IMPORTED_FILE) {
if (this.isStoredFileAttachment()) {
if (!path.includes("storage:")) {
Zotero.logError("Invalid attachment path '" + path + "'");
this._updateAttachmentStates(false);
@ -3035,8 +3034,7 @@ Zotero.defineProperty(Zotero.Item.prototype, 'attachmentPath', {
val = Zotero.Attachments.resolveRelativePath(val) || val;
}
}
else if (linkMode == Zotero.Attachments.LINK_MODE_IMPORTED_URL ||
linkMode == Zotero.Attachments.LINK_MODE_IMPORTED_FILE) {
else if (this.isStoredFileAttachment()) {
if (val && !val.startsWith('storage:')) {
let storagePath = Zotero.Attachments.getStorageDirectory(this).path;
if (!val.startsWith(storagePath)) {

View file

@ -502,13 +502,14 @@ Zotero.Sync.Storage.Local = {
*/
getFilesToUpload: function (libraryID) {
var sql = "SELECT itemID FROM itemAttachments JOIN items USING (itemID) "
+ "WHERE libraryID=? AND syncState IN (?,?) AND linkMode IN (?,?)";
+ "WHERE libraryID=? AND syncState IN (?,?) AND linkMode IN (?,?,?)";
var params = [
libraryID,
this.SYNC_STATE_TO_UPLOAD,
this.SYNC_STATE_FORCE_UPLOAD,
Zotero.Attachments.LINK_MODE_IMPORTED_FILE,
Zotero.Attachments.LINK_MODE_IMPORTED_URL
Zotero.Attachments.LINK_MODE_IMPORTED_URL,
Zotero.Attachments.LINK_MODE_EMBEDDED_IMAGE,
];
return Zotero.DB.columnQueryAsync(sql, params);
},
@ -566,12 +567,13 @@ Zotero.Sync.Storage.Local = {
return Zotero.DB.executeTransaction(async function () {
var sql = "SELECT itemID FROM items JOIN itemAttachments USING (itemID) "
+ "WHERE libraryID=? AND itemTypeID=? AND linkMode IN (?, ?)";
+ "WHERE libraryID=? AND itemTypeID=? AND linkMode IN (?, ?, ?)";
var params = [
libraryID,
Zotero.ItemTypes.getID('attachment'),
Zotero.Attachments.LINK_MODE_IMPORTED_FILE,
Zotero.Attachments.LINK_MODE_IMPORTED_URL,
Zotero.Attachments.LINK_MODE_EMBEDDED_IMAGE,
];
var itemIDs = await Zotero.DB.columnQueryAsync(sql, params);
for (let itemID of itemIDs) {

View file

@ -854,6 +854,33 @@ describe("Zotero.Item", function () {
});
})
describe("#getFilePath()", function () {
it("should return the absolute path for an embedded image", async function () {
var note = await createDataObject('item', { itemType: 'note' });
var path = OS.Path.join(getTestDataDirectory().path, 'test.png');
var imageData = await Zotero.File.getBinaryContentsAsync(path);
var array = new Uint8Array(imageData.length);
for (let i = 0; i < imageData.length; i++) {
array[i] = imageData.charCodeAt(i);
}
var blob = new Blob([array], { type: 'image/png' });
var attachment = await Zotero.Attachments.importEmbeddedImage({
blob,
parentItemID: note.id
});
var storageDir = Zotero.getStorageDirectory().path;
assert.equal(
OS.Path.join(storageDir, attachment.key, 'image.png'),
attachment.getFilePath()
);
});
});
describe("#attachmentCharset", function () {
it("should get and set a value", function* () {
var charset = 'utf-8';