diff --git a/src/merge.js b/src/merge.js index 279cc9ed8..e5db76244 100644 --- a/src/merge.js +++ b/src/merge.js @@ -180,6 +180,8 @@ class Merge { if (doc.size == null) { doc.size = file.size } if (doc.class == null) { doc.class = file.class } if (doc.mime == null) { doc.mime = file.mime } + } else if (!isUpToDate(side, file)) { + return this.resolveConflictAsync(side, doc) } if (sameFile(file, doc)) { log.info({doc}, 'up to date') diff --git a/src/metadata.js b/src/metadata.js index 556310a7e..2ef1481de 100644 --- a/src/metadata.js +++ b/src/metadata.js @@ -182,17 +182,7 @@ export function sameFile (one: Metadata, two: Metadata) { // Return true if the two files have the same binary content export function sameBinary (one: Metadata, two: Metadata) { - if ((one.docType !== 'file') || (two.docType !== 'file')) { - return false - } else if ((one.md5sum != null) && (one.md5sum === two.md5sum)) { - return true - } else if ((one.remote != null) && (two.remote != null)) { - let oneId = one.remote._id - let twoId = two.remote._id - return (oneId != null) && (oneId === twoId) - } else { - return false - } + return one.md5sum === two.md5sum } // Mark the next rev for this side diff --git a/src/remote/index.js b/src/remote/index.js index 39c3785a2..184462e17 100644 --- a/src/remote/index.js +++ b/src/remote/index.js @@ -119,11 +119,16 @@ export default class Remote implements Side { async overwriteFileAsync (doc: Metadata, old: ?Metadata): Promise { log.info(`${doc.path}: Uploading new file version...`) const stream = await this.other.createReadStreamAsync(doc) - const updated = await this.remoteCozy.updateFileById(doc.remote._id, stream, { + const options = { contentType: doc.mime, checksum: doc.md5sum, - lastModifiedDate: new Date(doc.updated_at) - }) + lastModifiedDate: new Date(doc.updated_at), + ifMatch: '' + } + if (old && old.remote) { + options.ifMatch = old.remote._rev + } + const updated = await this.remoteCozy.updateFileById(doc.remote._id, stream, options) doc.remote._rev = updated._rev diff --git a/test/unit/merge.js b/test/unit/merge.js index 595b2499f..ff9778e3c 100644 --- a/test/unit/merge.js +++ b/test/unit/merge.js @@ -146,7 +146,11 @@ describe('Merge', function () { path: 'FIZZBUZZ.JPG', docType: 'file', md5sum: '3333333333333333333333333333333333333333', - tags: ['qux', 'quux'] + tags: ['qux', 'quux'], + sides: { + local: 2, + remote: 2 + } } this.merge.updateFileAsync(this.side, clone(doc)).then(() => { this.pouch.db.get(this.file._id, function (err, res) { diff --git a/test/unit/metadata.js b/test/unit/metadata.js index 9cd89012b..9e867de69 100644 --- a/test/unit/metadata.js +++ b/test/unit/metadata.js @@ -325,26 +325,6 @@ describe('metadata', function () { ret.should.be.true() }) - it('returns true for two docs with the same remote file', function () { - let one = { - md5sum: 'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc', - docType: 'file', - remote: { - _id: 'f00b4r' - } - } - let two = { - docType: 'file', - remote: { - _id: 'f00b4r' - } - } - let ret = sameBinary(one, two) - ret.should.be.true() - ret = sameBinary(two, one) - ret.should.be.true() - }) - it('returns false for two different documents', function () { let one = { docType: 'file', @@ -357,18 +337,8 @@ describe('metadata', function () { _id: 'f00b4r' } } - let three = { - docType: 'file', - remote: { - _id: 'c00463' - } - } let ret = sameBinary(one, two) ret.should.be.false() - ret = sameBinary(two, three) - ret.should.be.false() - ret = sameBinary(three, one) - ret.should.be.false() }) })