Skip to content

Commit

Permalink
fix: multiple upload (#10)
Browse files Browse the repository at this point in the history
close #9
  • Loading branch information
puroong authored Aug 25, 2020
1 parent 141f085 commit f6157a7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

var queue = require('queue')
var path = require('path')
var crypto = require('crypto')
var Client = require('ssh2-sftp-client')
Expand Down Expand Up @@ -31,6 +32,10 @@ function SFTPStorage (opts) {
}

this.client = opts.client || new Client()
this.queue = queue({
concurrency: 1,
autostart: true
})
}

SFTPStorage.prototype._handleFile = function _handleFile (req, file, cb) {
Expand All @@ -42,15 +47,18 @@ SFTPStorage.prototype._handleFile = function _handleFile (req, file, cb) {

var finalPath = path.join(destination, filename)

this.client.connect(this.sftp)
.then(() => this.client.put(file.stream, finalPath))
.then((data) => cb(null, {
destination: destination,
filename: filename,
path: finalPath
}))
.catch((err) => cb(err))
.then(() => this.client.end())
this.queue.push(() => {
return this.client.connect(this.sftp)
.then(() => this.client.put(file.stream, finalPath))
.then((data) => cb(null, {
destination: destination,
filename: filename,
path: finalPath
}))
.catch((err) => cb(err))
.then(() => this.client.end())
}
)
})
})
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"multer": "^1.4.2",
"nyc": "^15.0.0",
"on-finished": "^2.3.0",
"queue": "^6.0.1",
"ssh2-sftp-client": "^5.0.2"
},
"peerDependencies": {
Expand Down

0 comments on commit f6157a7

Please sign in to comment.