fix(transfer): fix multiple transfer requests

This commit is contained in:
Alexey Kasyanchuk 2018-09-02 06:48:13 +03:00
parent 0e9ae9584f
commit 7098bdd0b3

View File

@ -26,6 +26,7 @@ class p2p {
this.p2pStatus = 0 this.p2pStatus = 0
this.version = '0' this.version = '0'
this.info = {} this.info = {}
this.filesRequests = {}
if(!config.peerId) if(!config.peerId)
{ {
logT('p2p', 'generate peerId') logT('p2p', 'generate peerId')
@ -152,7 +153,7 @@ class p2p {
if(!fs.existsSync(filePath)) if(!fs.existsSync(filePath))
{ {
logTE('transfer', 'no such file or directory', filePath) logT('transfer', 'no such file or directory', filePath)
return return
} }
@ -420,8 +421,14 @@ class p2p {
return return
} }
if(this.filesRequests[path])
{
logT('transfer', 'already downloading', path, 'return downloading request')
return this.filesRequests[path]
}
logT('transfer', 'get file request', path) logT('transfer', 'get file request', path)
return new Promise(async (resolve) => const promise = new Promise(async (resolve) =>
{ {
const filePath = this.dataDirectory + '/' + (targetPath || path) const filePath = this.dataDirectory + '/' + (targetPath || path)
// recreate directory to file if not exist // recreate directory to file if not exist
@ -504,6 +511,13 @@ class p2p {
fileStream.write(buffer) fileStream.write(buffer)
}, true) // dont clear callback }, true) // dont clear callback
}) })
this.filesRequests[path] = promise
promise.then(() => {
delete this.filesRequests[path]
})
return promise
} }
peersList() peersList()