Switch to OS.File for most full-text file access

This commit is contained in:
Dan Stillman 2017-07-26 05:38:27 -04:00
parent 61f8a2c3c5
commit 10637ca9ea

View File

@ -844,8 +844,8 @@ Zotero.Fulltext = Zotero.FullText = new function(){
let contentType = item.attachmentContentType; let contentType = item.attachmentContentType;
if (isCachedMIMEType(contentType) || Zotero.MIME.isTextType(contentType)) { if (isCachedMIMEType(contentType) || Zotero.MIME.isTextType(contentType)) {
try { try {
let cacheFile = this.getItemCacheFile(item); let cacheFile = this.getItemCacheFile(item).path;
if (cacheFile.exists()) { if (yield OS.File.exists(cacheFile)) {
Zotero.debug("Getting full-text content from cache " Zotero.debug("Getting full-text content from cache "
+ "file for item " + libraryKey); + "file for item " + libraryKey);
content = yield Zotero.File.getContentsAsync(cacheFile); content = yield Zotero.File.getContentsAsync(cacheFile);
@ -988,18 +988,18 @@ Zotero.Fulltext = Zotero.FullText = new function(){
var itemID = item.id; var itemID = item.id;
var currentVersion = yield this.getItemVersion(itemID) var currentVersion = yield this.getItemVersion(itemID)
var processorCacheFile = this.getItemProcessorCacheFile(item); // .zotero-ft-unprocessed var processorCacheFile = this.getItemProcessorCacheFile(item).path; // .zotero-ft-unprocessed
var itemCacheFile = this.getItemCacheFile(item); // .zotero-ft-cache var itemCacheFile = this.getItemCacheFile(item).path; // .zotero-ft-cache
// If a storage directory doesn't exist, create it // If a storage directory doesn't exist, create it
if (!processorCacheFile.parent.exists()) { if (!(yield OS.File.exists(OS.Path.dirname(processorCacheFile)))) {
yield Zotero.Attachments.createDirectoryForItem(item); yield Zotero.Attachments.createDirectoryForItem(item);
} }
// If indexed previously and the existing extracted text matches the new text, // If indexed previously and the existing extracted text matches the new text,
// just update the version // just update the version
if (currentVersion !== false if (currentVersion !== false
&& itemCacheFile.exists() && (yield OS.File.exists(itemCacheFile))
&& (yield Zotero.File.getContentsAsync(itemCacheFile)) == data.content) { && (yield Zotero.File.getContentsAsync(itemCacheFile)) == data.content) {
Zotero.debug("Current full-text content matches remote for item " Zotero.debug("Current full-text content matches remote for item "
+ libraryKey + " -- updating version"); + libraryKey + " -- updating version");
@ -1011,7 +1011,7 @@ Zotero.Fulltext = Zotero.FullText = new function(){
// Otherwise save data to -unprocessed file // Otherwise save data to -unprocessed file
Zotero.debug("Writing full-text content and data for item " + libraryKey Zotero.debug("Writing full-text content and data for item " + libraryKey
+ " to " + processorCacheFile.path); + " to " + processorCacheFile);
yield Zotero.File.putContentsAsync(processorCacheFile, JSON.stringify({ yield Zotero.File.putContentsAsync(processorCacheFile, JSON.stringify({
indexedChars: data.indexedChars, indexedChars: data.indexedChars,
totalChars: data.totalChars, totalChars: data.totalChars,
@ -1154,8 +1154,8 @@ Zotero.Fulltext = Zotero.FullText = new function(){
this.indexFromProcessorCache = Zotero.Promise.coroutine(function* (itemID) { this.indexFromProcessorCache = Zotero.Promise.coroutine(function* (itemID) {
try { try {
var item = yield Zotero.Items.getAsync(itemID); var item = yield Zotero.Items.getAsync(itemID);
var cacheFile = this.getItemProcessorCacheFile(item); var cacheFile = this.getItemProcessorCacheFile(item).path;
if (!cacheFile.exists()) { if (!(yield OS.File.exists(cacheFile))) {
Zotero.debug("Full-text content processor cache file doesn't exist for item " + itemID); Zotero.debug("Full-text content processor cache file doesn't exist for item " + itemID);
yield Zotero.DB.queryAsync( yield Zotero.DB.queryAsync(
"UPDATE fulltextItems SET synced=? WHERE itemID=?", [SYNC_STATE_UNSYNCED, itemID] "UPDATE fulltextItems SET synced=? WHERE itemID=?", [SYNC_STATE_UNSYNCED, itemID]
@ -1168,8 +1168,8 @@ Zotero.Fulltext = Zotero.FullText = new function(){
// Write the text content to the regular cache file // Write the text content to the regular cache file
var item = yield Zotero.Items.getAsync(itemID); var item = yield Zotero.Items.getAsync(itemID);
cacheFile = this.getItemCacheFile(item); cacheFile = this.getItemCacheFile(item).path;
Zotero.debug("Writing full-text content to " + cacheFile.path); Zotero.debug("Writing full-text content to " + cacheFile);
yield Zotero.File.putContentsAsync(cacheFile, data.text); yield Zotero.File.putContentsAsync(cacheFile, data.text);
yield indexString( yield indexString(
@ -1298,12 +1298,12 @@ Zotero.Fulltext = Zotero.FullText = new function(){
let binaryMode = mode && mode.indexOf('Binary') != -1; let binaryMode = mode && mode.indexOf('Binary') != -1;
if (isCachedMIMEType(mimeType)) { if (isCachedMIMEType(mimeType)) {
let file = this.getItemCacheFile(item); let file = this.getItemCacheFile(item).path;
if (!file.exists()) { if (!(yield OS.File.exists(file))) {
continue; continue;
} }
Zotero.debug("Searching for text '" + searchText + "' in " + file.path); Zotero.debug("Searching for text '" + searchText + "' in " + file);
content = yield Zotero.File.getContentsAsync(file, 'utf-8', maxLength); content = yield Zotero.File.getContentsAsync(file, 'utf-8', maxLength);
} }
else { else {
@ -1316,9 +1316,9 @@ Zotero.Fulltext = Zotero.FullText = new function(){
} }
// Check for a cache file // Check for a cache file
let cacheFile = this.getItemCacheFile(item); let cacheFile = this.getItemCacheFile(item).path;
if (cacheFile.exists()) { if (yield OS.File.exists(cacheFile)) {
Zotero.debug("Searching for text '" + searchText + "' in " + cacheFile.path); Zotero.debug("Searching for text '" + searchText + "' in " + cacheFile);
content = yield Zotero.File.getContentsAsync(cacheFile, 'utf-8', maxLength); content = yield Zotero.File.getContentsAsync(cacheFile, 'utf-8', maxLength);
} }
else { else {
@ -1398,9 +1398,11 @@ Zotero.Fulltext = Zotero.FullText = new function(){
* @return {Promise} * @return {Promise}
*/ */
var getTotalPagesFromFile = Zotero.Promise.coroutine(function* (itemID) { var getTotalPagesFromFile = Zotero.Promise.coroutine(function* (itemID) {
var file = Zotero.Attachments.getStorageDirectoryByID(itemID); var file = OS.Path.join(
file.append(Zotero.Fulltext.pdfInfoCacheFile); Zotero.Attachments.getStorageDirectoryByID(itemID).path,
if (!file.exists()) { Zotero.Fulltext.pdfInfoCacheFile
);
if (!(yield OS.File.exists(file))) {
return false; return false;
} }
var contents = yield Zotero.File.getContentsAsync(file); var contents = yield Zotero.File.getContentsAsync(file);
@ -1435,15 +1437,17 @@ Zotero.Fulltext = Zotero.FullText = new function(){
var item = yield Zotero.Items.getAsync(itemID); var item = yield Zotero.Items.getAsync(itemID);
switch (item.attachmentContentType) { switch (item.attachmentContentType) {
case 'application/pdf': case 'application/pdf':
var file = Zotero.Attachments.getStorageDirectory(item); var file = OS.Path.join(
file.append(this.pdfConverterCacheFile); Zotero.Attachments.getStorageDirectory(item).path,
if (!file.exists()) { this.pdfConverterCacheFile
);
if (!(yield OS.File.exists(file))) {
return false; return false;
} }
break; break;
default: default:
var file = item.getFile(); var file = yield item.getFilePathAsync();
if (!file) { if (!file) {
return false; return false;
} }
@ -1762,9 +1766,9 @@ Zotero.Fulltext = Zotero.FullText = new function(){
// Write the converted text to a cache file // Write the converted text to a cache file
var item = yield Zotero.Items.getAsync(itemID); var item = yield Zotero.Items.getAsync(itemID);
var cacheFile = Zotero.Fulltext.getItemCacheFile(item); var cacheFile = Zotero.Fulltext.getItemCacheFile(item).path;
Zotero.debug("Writing converted full-text HTML content to " + cacheFile.path); Zotero.debug("Writing converted full-text HTML content to " + cacheFile);
if (!cacheFile.parent.exists()) { if (!(yield OS.File.exists(OS.Path.dirname(cacheFile)))) {
yield Zotero.Attachments.createDirectoryForItem(item); yield Zotero.Attachments.createDirectoryForItem(item);
} }
yield Zotero.File.putContentsAsync(cacheFile, text) yield Zotero.File.putContentsAsync(cacheFile, text)