forked from JCMais/node-libcurl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path16-ftp-resume-upload.js
74 lines (60 loc) · 2.08 KB
/
16-ftp-resume-upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Copyright (c) Jonathan Cardoso Machado. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Example showing how to resume upload of a file to a ftp server
* How to run:
* node ftp-upload.js ftp://example-of-ftp-host.com username password /some/local/file.ext /some/remote/file.ext 0-100
*/
const fs = require('fs')
const { Curl, CurlFeature } = require('../dist')
const curl = new Curl()
const url = process.argv[2].replace(/\/$/, '') + '/'
const username = process.argv[3]
const password = process.argv[4]
const localFile = process.argv[5]
const remoteFile = process.argv[6]
const percentage = +process.argv[7] / 100
const fd = fs.openSync(localFile, 'r')
const fileStat = fs.fstatSync(fd)
// enable verbose mode
curl.setOpt(Curl.option.VERBOSE, true)
// specify target, username and password
curl.setOpt(Curl.option.URL, url + remoteFile)
curl.setOpt(Curl.option.USERNAME, username)
curl.setOpt(Curl.option.PASSWORD, password)
// enable uploading
curl.setOpt(Curl.option.UPLOAD, true)
// now specify which file to upload, in this case
// we are using a file descriptor given by fs.openSync
curl.setOpt(Curl.option.READDATA, fd)
// Set the size of the file to upload (optional).
// You must the *_LARGE option if the file is greater than 2GB.
curl.setOpt(Curl.option.INFILESIZE_LARGE, fileStat.size)
// enable feature flag to not use storage / parsing
curl.enable(CurlFeature.NoStorage)
// tell curl to figure out the remote file size by itself
curl.setOpt(Curl.option.RESUME_FROM, -1)
// (ab)use the progress callback to stop the upload after the given percentage
curl.setOpt(Curl.option.NOPROGRESS, false)
curl.setProgressCallback((dltotal, dlnow, ultotal, ulnow) => {
if (ultotal === fileStat.size) {
if (ulnow > fileStat.size * percentage) {
return 1
}
}
return 0
})
curl.perform()
curl.on('end', function () {
curl.close()
fs.closeSync(fd)
})
curl.on('error', function (error, errorCode) {
console.log(error, errorCode)
curl.close()
fs.closeSync(fd)
})