Fix "text is not defined" error during full-text content sync
This commit is contained in:
parent
696e828a02
commit
d527c340c6
|
@ -960,36 +960,40 @@ Zotero.Fulltext = Zotero.FullText = new function(){
|
|||
var itemID = item.id;
|
||||
var currentVersion = yield this.getItemVersion(itemID)
|
||||
|
||||
var processorCacheFile = this.getItemProcessorCacheFile(item);
|
||||
var itemCacheFile = this.getItemCacheFile(item);
|
||||
var processorCacheFile = this.getItemProcessorCacheFile(item); // .zotero-ft-unprocessed
|
||||
var itemCacheFile = this.getItemCacheFile(item); // .zotero-ft-cache
|
||||
|
||||
// If a storage directory doesn't exist, create it
|
||||
if (!processorCacheFile.parent.exists()) {
|
||||
yield Zotero.Attachments.createDirectoryForItem(item);
|
||||
}
|
||||
|
||||
// If the local version is 0 but the text matches, just update the version
|
||||
if (currentVersion == 0 && itemCacheFile.exists()
|
||||
&& (yield Zotero.File.getContentsAsync(itemCacheFile)) == text) {
|
||||
// If indexed previously and the existing extracted text matches the new text,
|
||||
// just update the version
|
||||
if (currentVersion !== false
|
||||
&& itemCacheFile.exists()
|
||||
&& (yield Zotero.File.getContentsAsync(itemCacheFile)) == data.content) {
|
||||
Zotero.debug("Current full-text content matches remote for item "
|
||||
+ libraryKey + " -- updating version");
|
||||
var synced = SYNC_STATE_IN_SYNC;
|
||||
yield Zotero.DB.queryAsync("UPDATE fulltextItems SET version=? WHERE itemID=?", [version, itemID]);
|
||||
}
|
||||
else {
|
||||
Zotero.debug("Writing full-text content and data for item " + libraryKey
|
||||
+ " to " + processorCacheFile.path);
|
||||
yield Zotero.File.putContentsAsync(processorCacheFile, JSON.stringify({
|
||||
indexedChars: data.indexedChars,
|
||||
totalChars: data.totalChars,
|
||||
indexedPages: data.indexedPages,
|
||||
totalPages: data.totalPages,
|
||||
version: version,
|
||||
text: data.content
|
||||
}));
|
||||
var synced = SYNC_STATE_TO_PROCESS;
|
||||
return Zotero.DB.queryAsync(
|
||||
"REPLACE INTO fulltextItems (itemID, version, synced) VALUES (?, ?, ?)",
|
||||
[itemID, version, SYNC_STATE_IN_SYNC]
|
||||
);
|
||||
}
|
||||
|
||||
// Otherwise save data to -unprocessed file
|
||||
Zotero.debug("Writing full-text content and data for item " + libraryKey
|
||||
+ " to " + processorCacheFile.path);
|
||||
yield Zotero.File.putContentsAsync(processorCacheFile, JSON.stringify({
|
||||
indexedChars: data.indexedChars,
|
||||
totalChars: data.totalChars,
|
||||
indexedPages: data.indexedPages,
|
||||
totalPages: data.totalPages,
|
||||
version,
|
||||
text: data.content
|
||||
}));
|
||||
var synced = SYNC_STATE_TO_PROCESS;
|
||||
|
||||
// If indexed previously, update the sync state
|
||||
if (currentVersion !== false) {
|
||||
yield Zotero.DB.queryAsync("UPDATE fulltextItems SET synced=? WHERE itemID=?", [synced, itemID]);
|
||||
|
|
|
@ -231,4 +231,64 @@ describe("Zotero.Fulltext", function () {
|
|||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe("#setItemContent()", function () {
|
||||
it("should store data in .zotero-ft-unprocessed file", function* () {
|
||||
var item = yield importFileAttachment('test.pdf');
|
||||
|
||||
var processorCacheFile = Zotero.Fulltext.getItemProcessorCacheFile(item).path;
|
||||
var itemCacheFile = Zotero.Fulltext.getItemCacheFile(item).path;
|
||||
yield Zotero.File.putContentsAsync(itemCacheFile, "Test");
|
||||
|
||||
yield Zotero.Fulltext.setItemContent(
|
||||
item.libraryID,
|
||||
item.key,
|
||||
{
|
||||
content: "Test",
|
||||
indexedChars: 4,
|
||||
totalChars: 4
|
||||
},
|
||||
5
|
||||
);
|
||||
|
||||
assert.equal((yield Zotero.Fulltext.getItemVersion(item.id)), 0);
|
||||
assert.equal(
|
||||
yield Zotero.DB. valueQueryAsync("SELECT synced FROM fulltextItems WHERE itemID=?", item.id),
|
||||
2 // to process
|
||||
);
|
||||
assert.isTrue(yield OS.File.exists(processorCacheFile));
|
||||
});
|
||||
|
||||
|
||||
it("should update the version if the local version is 0 but the text matches", function* () {
|
||||
var item = yield importFileAttachment('test.pdf');
|
||||
|
||||
yield Zotero.DB.queryAsync(
|
||||
"REPLACE INTO fulltextItems (itemID, version, synced) VALUES (?, 0, ?)",
|
||||
[item.id, 0] // to process
|
||||
);
|
||||
|
||||
var processorCacheFile = Zotero.Fulltext.getItemProcessorCacheFile(item).path;
|
||||
var itemCacheFile = Zotero.Fulltext.getItemCacheFile(item).path;
|
||||
yield Zotero.File.putContentsAsync(itemCacheFile, "Test");
|
||||
|
||||
yield Zotero.Fulltext.setItemContent(
|
||||
item.libraryID,
|
||||
item.key,
|
||||
{
|
||||
content: "Test",
|
||||
indexedChars: 4,
|
||||
totalChars: 4
|
||||
},
|
||||
5
|
||||
);
|
||||
|
||||
assert.equal((yield Zotero.Fulltext.getItemVersion(item.id)), 5);
|
||||
assert.equal(
|
||||
yield Zotero.DB. valueQueryAsync("SELECT synced FROM fulltextItems WHERE itemID=?", item.id),
|
||||
1 // in sync
|
||||
);
|
||||
assert.isFalse(yield OS.File.exists(processorCacheFile));
|
||||
});
|
||||
});
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue
Block a user