Delete cache image when image annotation position changes

This commit is contained in:
Dan Stillman 2021-01-26 02:23:34 -05:00
parent a19693fa7a
commit 9f954488c6
3 changed files with 34 additions and 13 deletions

View file

@ -57,6 +57,7 @@ Zotero.Annotations = new function () {
await Zotero.File.createDirectoryIfMissingAsync(file, { from: cacheDir }); await Zotero.File.createDirectoryIfMissingAsync(file, { from: cacheDir });
file = OS.Path.join(file, item.key + '.png'); file = OS.Path.join(file, item.key + '.png');
Zotero.debug("Creating annotation cache file " + file);
await Zotero.File.putContentsAsync(file, blob); await Zotero.File.putContentsAsync(file, blob);
await Zotero.File.setNormalFilePermissions(file); await Zotero.File.setNormalFilePermissions(file);
@ -66,6 +67,7 @@ Zotero.Annotations = new function () {
this.removeCacheImage = async function ({ libraryID, key }) { this.removeCacheImage = async function ({ libraryID, key }) {
var path = this.getCacheImagePath({ libraryID, key }); var path = this.getCacheImagePath({ libraryID, key });
Zotero.debug("Deleting annotation cache file " + path);
await OS.File.remove(path, { ignoreAbsent: true }); await OS.File.remove(path, { ignoreAbsent: true });
}; };
@ -101,12 +103,6 @@ Zotero.Annotations = new function () {
} }
return OS.Path.join(...parts); return OS.Path.join(...parts);
}; };
this.positionEquals = function (position1, position2) {
return position1.pageIndex == position2.pageIndex
&& JSON.stringify(position1.rects) == JSON.stringify(position2.rects);
};
this.toJSON = async function (item) { this.toJSON = async function (item) {
@ -203,13 +199,6 @@ Zotero.Annotations = new function () {
item.annotationPageLabel = json.pageLabel; item.annotationPageLabel = json.pageLabel;
item.annotationSortIndex = json.sortIndex; item.annotationSortIndex = json.sortIndex;
if (item.annotationType == 'image' && item.annotationPosition) {
var currentPosition = JSON.parse(item.annotationPosition);
if (!this.positionEquals(currentPosition, json.position)) {
await this.removeCacheImage(item);
}
}
item.annotationPosition = JSON.stringify(Object.assign({}, json.position)); item.annotationPosition = JSON.stringify(Object.assign({}, json.position));
// TODO: Can colors be set? // TODO: Can colors be set?
item.setTags((json.tags || []).map(t => ({ tag: t.name }))); item.setTags((json.tags || []).map(t => ({ tag: t.name })));

View file

@ -1833,6 +1833,15 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
// Clear cached child items of the parent attachment // Clear cached child items of the parent attachment
reloadParentChildItems[parentItemID] = true; reloadParentChildItems[parentItemID] = true;
// Mark cache image for deletion when image annotation position changes
if (!isNew && type == 'image' && this._hasFieldChanged('annotationPosition')) {
let libraryID = this.libraryID;
let key = this.key;
Zotero.DB.addCurrentCallback("commit", function () {
Zotero.Annotations.removeCacheImage({ libraryID, key });
}.bind(this));
}
} }
// Add to new collections // Add to new collections

View file

@ -1304,6 +1304,29 @@ describe("Zotero.Item", function () {
imageData imageData
); );
}); });
it("should remove cached image for an annotation item when position changes", async function () {
var attachment = await importFileAttachment('test.pdf');
var annotation = await createAnnotation('image', attachment);
// Get Blob from file and attach it
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 file = await Zotero.Annotations.saveCacheImage(annotation, blob);
assert.isTrue(await OS.File.exists(file));
var position = JSON.parse(annotation.annotationPosition);
position.rects[0][0] = position.rects[0][0] + 1;
annotation.annotationPosition = JSON.stringify(position);
await annotation.saveTx();
assert.isFalse(await OS.File.exists(file));
});
}); });
describe("#getAnnotations()", function () { describe("#getAnnotations()", function () {