Fix setting of local mtime when remote file change matches local file

This commit is contained in:
Dan Stillman 2016-03-16 02:01:51 -04:00
parent 8df6b4bbd3
commit 28dc7d17e2
4 changed files with 64 additions and 6 deletions

View file

@ -577,13 +577,10 @@ Zotero.Sync.Storage.Local = {
// Set the file mtime to the time from the server // Set the file mtime to the time from the server
yield OS.File.setDates(path, null, new Date(parseInt(mtime))); yield OS.File.setDates(path, null, new Date(parseInt(mtime)));
item.attachmentSyncState = this.SYNC_STATE_IN_SYNC;
item.attachmentSyncedModificationTime = mtime; item.attachmentSyncedModificationTime = mtime;
item.attachmentSyncedHash = md5; item.attachmentSyncedHash = md5;
yield item.saveTx({ item.attachmentSyncState = "in_sync";
skipDateModifiedUpdate: true, yield item.saveTx({ skipAll: true });
skipSelect: true
});
return new Zotero.Sync.Storage.Result({ return new Zotero.Sync.Storage.Result({
localChanges: true localChanges: true

View file

@ -156,6 +156,8 @@ Zotero.Sync.Storage.StreamListener.prototype = {
if (!result) { if (!result) {
oldChannel.cancel(Components.results.NS_BINDING_ABORTED); oldChannel.cancel(Components.results.NS_BINDING_ABORTED);
newChannel.cancel(Components.results.NS_BINDING_ABORTED); newChannel.cancel(Components.results.NS_BINDING_ABORTED);
Zotero.debug("Cancelling redirect");
// TODO: Prevent onStateChange error
return false; return false;
} }
} }

View file

@ -99,7 +99,7 @@ Zotero.Sync.Storage.Mode.ZFS.prototype = {
var header; var header;
try { try {
header = "Zotero-File-Modification-Time"; header = "Zotero-File-Modification-Time";
requestData.mtime = oldChannel.getResponseHeader(header); requestData.mtime = parseInt(oldChannel.getResponseHeader(header));
header = "Zotero-File-MD5"; header = "Zotero-File-MD5";
requestData.md5 = oldChannel.getResponseHeader(header); requestData.md5 = oldChannel.getResponseHeader(header);
header = "Zotero-File-Compressed"; header = "Zotero-File-Compressed";
@ -131,6 +131,7 @@ Zotero.Sync.Storage.Mode.ZFS.prototype = {
} }
// Update local metadata and stop request, skipping file download // Update local metadata and stop request, skipping file download
yield OS.File.setDates(path, null, new Date(requestData.mtime));
item.attachmentSyncedModificationTime = requestData.mtime; item.attachmentSyncedModificationTime = requestData.mtime;
if (updateHash) { if (updateHash) {
item.attachmentSyncedHash = requestData.md5; item.attachmentSyncedHash = requestData.md5;
@ -138,6 +139,10 @@ Zotero.Sync.Storage.Mode.ZFS.prototype = {
item.attachmentSyncState = "in_sync"; item.attachmentSyncState = "in_sync";
yield item.saveTx({ skipAll: true }); yield item.saveTx({ skipAll: true });
deferred.resolve(new Zotero.Sync.Storage.Result({
localChanges: true
}));
return false; return false;
}), }),
onProgress: function (a, b, c) { onProgress: function (a, b, c) {

View file

@ -561,6 +561,60 @@ describe("Zotero.Sync.Storage.Mode.ZFS", function () {
assert.equal(item2.version, 15); assert.equal(item2.version, 15);
}) })
it("should update local info for remotely updated file that matches local file", function* () {
var { engine, client, caller } = yield setup();
var library = Zotero.Libraries.userLibrary;
library.libraryVersion = 5;
yield library.saveTx();
library.storageDownloadNeeded = true;
var file = getTestDataDirectory();
file.append('test.txt');
var item = yield Zotero.Attachments.importFromFile({ file });
item.version = 5;
item.attachmentSyncState = "to_download";
yield item.saveTx();
var path = yield item.getFilePathAsync();
yield OS.File.setDates(path, null, new Date() - 100000);
var json = item.toJSON();
yield Zotero.Sync.Data.Local.saveCacheObject('item', item.libraryID, json);
var mtime = (Math.floor(new Date().getTime() / 1000) * 1000) + "";
var md5 = Zotero.Utilities.Internal.md5(file)
var s3Path = `pretend-s3/${item.key}`;
this.httpd.registerPathHandler(
`/users/1/items/${item.key}/file`,
{
handle: function (request, response) {
if (!request.hasHeader('Zotero-API-Key')) {
response.setStatusLine(null, 403, "Forbidden");
return;
}
var key = request.getHeader('Zotero-API-Key');
if (key != apiKey) {
response.setStatusLine(null, 403, "Invalid key");
return;
}
response.setStatusLine(null, 302, "Found");
response.setHeader("Zotero-File-Modification-Time", mtime, false);
response.setHeader("Zotero-File-MD5", md5, false);
response.setHeader("Zotero-File-Compressed", "No", false);
response.setHeader("Location", baseURL + s3Path, false);
}
}
);
var result = yield engine.start();
assert.equal(item.attachmentSyncedModificationTime, mtime);
yield assert.eventually.equal(item.attachmentModificationTime, mtime);
assert.isTrue(result.localChanges);
assert.isFalse(result.remoteChanges);
assert.isFalse(result.syncRequired);
})
it("should update local info for file that already exists on the server", function* () { it("should update local info for file that already exists on the server", function* () {
var { engine, client, caller } = yield setup(); var { engine, client, caller } = yield setup();