forked from nxtedition/node-libcurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16-ftp-download.js
57 lines (43 loc) · 1.45 KB
/
16-ftp-download.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
/**
* 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 directly download files from a FTP server
*/
const path = require('path')
const fs = require('fs')
const { Curl, CurlFeature } = require('../dist')
const curl = new Curl()
const url = 'ftp://speedtest.tele2.net/1MB.zip'
const fileOutPath = process.argv[2] || path.join(process.cwd(), '1MB.zip')
const fileOut = fs.openSync(fileOutPath, 'w+')
curl.setOpt(Curl.option.URL, url)
curl.setOpt(Curl.option.WRITEFUNCTION, (buff, nmemb, size) => {
let written = 0
if (fileOut) {
written = fs.writeSync(fileOut, buff, 0, nmemb * size)
} else {
/* listing output */
process.stdout.write(buff.toString())
written = size * nmemb
}
return written
})
// we don't need Curl to parse the data or store it internally
// since we specified WRITEFUNCTION above, the data for the body of the response (the file itself in this case)
// would not be stored anyway. But there are still the headers, which for FTP are the operations output.
curl.enable(CurlFeature.Raw | CurlFeature.NoStorage)
curl.on('end', (_code, _body, _headers) => {
console.log('Finished download of FTP file')
fs.closeSync(fileOut)
curl.close()
})
curl.on('error', (error) => {
console.error(error)
fs.closeSync(fileOut)
curl.close()
})
curl.perform()