chore(xo-server): RemoteHandler#rename handle cheksum

This commit is contained in:
Julien Fontanet 2018-03-02 19:51:03 +01:00
parent 48b2297bc1
commit 052817ccbf

View File

@ -10,6 +10,8 @@ import {
validChecksumOfReadStream, validChecksumOfReadStream,
} from '../utils' } from '../utils'
const checksumFile = file => file + '.checksum'
export default class RemoteHandlerAbstract { export default class RemoteHandlerAbstract {
constructor (remote) { constructor (remote) {
this._remote = { ...remote, ...parse(remote.url) } this._remote = { ...remote, ...parse(remote.url) }
@ -92,8 +94,15 @@ export default class RemoteHandlerAbstract {
return this.createReadStream(file, options).then(streamToBuffer) return this.createReadStream(file, options).then(streamToBuffer)
} }
async rename (oldPath, newPath) { async rename (oldPath, newPath, { checksum = false } = {}) {
return this._rename(oldPath, newPath) let p = this._rename(oldPath, newPath)
if (checksum) {
p = Promise.all([
p,
this._rename(checksumFile(oldPath), checksumFile(newPath)),
])
}
return p
} }
async _rename (oldPath, newPath) { async _rename (oldPath, newPath) {
@ -143,7 +152,7 @@ export default class RemoteHandlerAbstract {
// avoid a unhandled rejection warning // avoid a unhandled rejection warning
;streamP::ignoreErrors() ;streamP::ignoreErrors()
return this.readFile(`${path}.checksum`).then( return this.readFile(checksumFile(path)).then(
checksum => checksum =>
streamP.then(stream => { streamP.then(stream => {
const { length } = stream const { length } = stream
@ -185,7 +194,7 @@ export default class RemoteHandlerAbstract {
const stream = addChecksumToReadStream(await this.createReadStream(path)) const stream = addChecksumToReadStream(await this.createReadStream(path))
stream.resume() // start reading the whole file stream.resume() // start reading the whole file
const checksum = await stream.checksum const checksum = await stream.checksum
await this.outputFile(`${path}.checksum`, checksum) await this.outputFile(checksumFile(path), checksum)
} }
async createOutputStream (file, { checksum = false, ...options } = {}) { async createOutputStream (file, { checksum = false, ...options } = {}) {
@ -210,7 +219,7 @@ export default class RemoteHandlerAbstract {
streamWithChecksum.pipe(stream) streamWithChecksum.pipe(stream)
streamWithChecksum.checksum streamWithChecksum.checksum
.then(value => this.outputFile(`${path}.checksum`, value)) .then(value => this.outputFile(checksumFile(path), value))
.catch(forwardError) .catch(forwardError)
return connectorStream return connectorStream