forked from JCMais/node-libcurl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path16-ftp-wildcard-fnmatch.js
159 lines (136 loc) · 4 KB
/
16-ftp-wildcard-fnmatch.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* 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 download files from a FTP server
* using custom wildcard pattern matching
*/
const path = require('path')
const util = require('util')
const fs = require('fs')
const {
Curl,
CurlChunk,
CurlFileType,
CurlFnMatchFunc,
Easy,
} = require('../dist')
// Using the Easy interface because currently there is an issue
// when using libcurl with wildcard matching on the multi interface
// https://github.com/curl/curl/issues/800
// It can safely by used with `Curl` or directly with a `Multi` instance
// if your libcurl version is greater than 7.49.0
const handle = new Easy()
const url = 'ftp://speedtest.tele2.net/*.zip'
// object to be used to share data between callbacks
const data = {
output: null,
}
handle.setOpt(Curl.option.URL, url)
handle.setOpt(Curl.option.VERBOSE, 1)
handle.setOpt(Curl.option.WILDCARDMATCH, true)
handle.setOpt(Curl.option.FNMATCH_FUNCTION, fnMatch)
handle.setOpt(Curl.option.CHUNK_BGN_FUNCTION, fileIsComing)
handle.setOpt(Curl.option.CHUNK_END_FUNCTION, filesIsDownloaded)
handle.setOpt(Curl.option.WRITEFUNCTION, (buff, nmemb, size) => {
let written = 0
if (data.output) {
written = fs.writeSync(data.output, buff, 0, nmemb * size)
} else {
/* listing output */
console.log(buff.toString())
written = size * nmemb
}
return written
})
// Functions globStringToRegex and pregQuote from: http://stackoverflow.com/a/13818704/710693
function globStringToRegex(str) {
return new RegExp(
pregQuote(str).replace(/\\\*/g, '.*').replace(/\\\?/g, '.'),
'g',
)
}
function pregQuote(str, delimiter) {
// http://kevin.vanzonneveld.net
// + original by: booeyOH
// + improved by: Ates Goral (http://magnetiq.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Onno Marsman
// + improved by: Brett Zamir (http://brett-zamir.me)
// * example 1: preg_quote("$40");
// * returns 1: '\$40'
// * example 2: preg_quote("*RRRING* Hello?");
// * returns 2: '\*RRRING\* Hello\?'
// * example 3: preg_quote("\\.+*?[^]$(){}=!<>|:");
// * returns 3: '\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:'
return (str + '').replace(
new RegExp(
'[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]',
'g',
),
'\\$&',
)
}
/**
* Use our own logic to make the wildcard matching.
*
* Here we are just changing from the default libcurl logic
* to use one equivalent using javascript RegExp.
*
* @param {String} pattern
* @param {String} string
* @returns {number}
*/
function fnMatch(pattern, string) {
const regex = new RegExp(globStringToRegex(pattern), 'g')
return string.match(regex) ? CurlFnMatchFunc.Match : CurlFnMatchFunc.NoMatch
}
function fileIsComing(fileInfo, remains) {
console.log(
util.format(
'Remaining entries: %d / Current: %s / Size: %d - ',
remains,
fileInfo.fileName,
fileInfo.size,
),
)
console.log('fileInfo object:', { fileInfo })
switch (fileInfo.fileType) {
case CurlFileType.Directory:
console.log(' DIR')
break
case CurlFileType.File:
console.log(' FILE')
break
default:
console.log(' OTHER')
break
}
if (fileInfo.fileType === CurlFileType.File) {
/* do not transfer files > 1MB */
if (fileInfo.size > 1024 * 1024) {
console.log('SKIPPED')
return CurlChunk.BgnFuncSkip
}
data.output = fs.openSync(path.join(process.cwd(), fileInfo.fileName), 'w+')
if (!data.output) {
return CurlChunk.BgnFuncFail
}
} else {
console.log('SKIPPED')
return CurlChunk.BgnFuncSkip
}
return CurlChunk.BgnFuncOk
}
function filesIsDownloaded() {
if (data.output) {
console.log('DOWNLOADED')
fs.closeSync(data.output)
data.output = null
}
return CurlChunk.EndFuncOk
}
handle.perform()