Skip to content

Commit

Permalink
Merge pull request #660 from nono/fix-overwrite-both-sides
Browse files Browse the repository at this point in the history
Fix for files overwriten on both sides
  • Loading branch information
nono authored Apr 24, 2017
2 parents 4624442 + 13d0c3d commit 0591626
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 45 deletions.
2 changes: 2 additions & 0 deletions src/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
12 changes: 1 addition & 11 deletions src/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/remote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,16 @@ export default class Remote implements Side {
async overwriteFileAsync (doc: Metadata, old: ?Metadata): Promise<Metadata> {
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

Expand Down
6 changes: 5 additions & 1 deletion test/unit/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 0 additions & 30 deletions test/unit/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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()
})
})

Expand Down

0 comments on commit 0591626

Please sign in to comment.