fix(fs/smb): create{Read,Output}Stream and getSize with opened files

This commit is contained in:
Julien Fontanet
2018-12-05 16:30:58 +01:00
parent ba35f51459
commit 1b48c626f4

View File

@@ -70,12 +70,16 @@ export default class SmbHandler extends RemoteHandlerAbstract {
} }
async _createOutputStream(file, options) { async _createOutputStream(file, options) {
if (typeof file !== 'string') { const needsClose = typeof file === 'string'
let client
if (needsClose) {
client = this._getClient()
} else {
;({ client } = file.fd)
file = file.path file = file.path
} }
const path = this._getFilePath(file) const path = this._getFilePath(file)
const client = this._getClient()
try { try {
const dir = this._dirname(path) const dir = this._dirname(path)
if (dir) { if (dir) {
@@ -85,21 +89,29 @@ export default class SmbHandler extends RemoteHandlerAbstract {
// FIXME ensure that options are properly handled by @marsaud/smb2 // FIXME ensure that options are properly handled by @marsaud/smb2
const stream = await client.createWriteStream(path, options) const stream = await client.createWriteStream(path, options)
finished(stream, () => client.disconnect()) if (needsClose) {
finished(stream, () => client.disconnect())
}
return stream return stream
} catch (err) { } catch (err) {
client.disconnect() if (needsClose) {
client.disconnect()
}
throw normalizeError(err) throw normalizeError(err)
} }
} }
async _createReadStream(file, options) { async _createReadStream(file, options) {
if (typeof file !== 'string') { const needsClose = typeof file === 'string'
let client
if (needsClose) {
client = this._getClient()
} else {
;({ client } = file.fd)
file = file.path file = file.path
} }
const client = this._getClient()
try { try {
// FIXME ensure that options are properly handled by @marsaud/smb2 // FIXME ensure that options are properly handled by @marsaud/smb2
const stream = await client.createReadStream( const stream = await client.createReadStream(
@@ -107,26 +119,36 @@ export default class SmbHandler extends RemoteHandlerAbstract {
options options
) )
finished(stream, () => client.disconnect()) if (needsClose) {
finished(stream, () => client.disconnect())
}
return stream return stream
} catch (error) { } catch (error) {
client.disconnect() if (needsClose) {
client.disconnect()
}
throw normalizeError(error) throw normalizeError(error)
} }
} }
async _getSize(file) { async _getSize(file) {
const client = await this._getClient() const needsClose = typeof file === 'string'
let client
if (needsClose) {
client = this._getClient()
} else {
;({ client } = file.fd)
file = file.path
}
try { try {
return await client.getSize( return await client.getSize(this._getFilePath(file))
this._getFilePath(typeof file === 'string' ? file : file.path)
)
} catch (error) { } catch (error) {
throw normalizeError(error) throw normalizeError(error)
} finally { } finally {
client.disconnect() if (needsClose) {
client.disconnect()
}
} }
} }