forked from reruin/sharelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrive.ftp.js
176 lines (144 loc) · 3.64 KB
/
drive.ftp.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* FTP >> Deprecated
*/
const name = '(Deprecated)FTP'
const version = '1.0'
const protocols = ['ftp']
const defaultProtocol = 'ftp'
const path = require('path')
const ftp = require("basic-ftp")
const { URL } = require("url")
const { PassThrough } = require('stream')
const clientMap = {}
module.exports = ({ getConfig, cache }) => {
const extname = (p) => path.extname(p).substring(1)
const getClient = async (url, cd = false) => {
let key = (url.match(/ftp\:\/\/[\w\W]+?\//) || [''])[0];
let { username, password, hostname, port, pathname } = new URL(url);
/*
let client = clientMap[key]
if (client && client.closed == false) {
if (pathname) {
if (cd) {
pathname = pathname.replace(/\/[^\/]+?$/, '')
}
console.log('cd1')
await client.cd(pathname);
}
return client
}
*/
let client = new ftp.Client();
let flag = false
client.ftp.verbose = true
try {
let p = await client.access({
host: hostname,
user: decodeURIComponent(username),
password: decodeURIComponent(password),
port: port || 21,
secure: false
})
if (pathname) {
if (cd) {
pathname = pathname.replace(/\/[^\/]+?$/, '')
}
await client.cd(pathname);
}
flag = true
} catch (err) {
flag = false
}
if (flag) {
//clientMap[key] = client
return client;
}
}
const folder = async (id) => {
let resid = `${defaultProtocol}:${id}`
let r = cache.get(resid)
let resp = { id: id, type: 'folder', protocol: defaultProtocol }
if (r) {
resp = r
if (
resp.$cached_at &&
resp.children &&
(Date.now() - resp.$cached_at < getConfig('max_age_dir'))
) {
console.log('get ftp folder from cache')
return resp
}
}
let client = await getClient(id)
if (client) {
let data = await client.list();
//client.close()
let children = [];
data.forEach(i => {
let path = (id + '/').replace(/\/{2,}$/g,'/') + i.name
let obj = {
id: path,
name: i.name,
protocol: defaultProtocol,
size: i.size,
created_at: i.date,
updated_at: i.date,
ext: extname(i.name),
type: 'other'
}
/*
Unknown = 0,
File = 1,
Directory = 2,
SymbolicLink = 3
*/
if (i.type == 2 || i.type == 3) {
obj.type = 'folder'
}
children.push(obj)
})
resp.$cached_at = Date.now()
resp.children = children
cache.set(resid, resp)
return resp
} else {
return false
}
}
const file = async (id) => {
console.log(id)
let client = await getClient(id , true)
let resp = {
id,
name: path.basename(id),
ext: extname(id),
url: id,
protocol: defaultProtocol,
outputType: 'stream',
proxy: true
}
let file = id.split('/').pop()
if (client) {
let size = await client.size(file)
resp.size = size
//client.close()
}
return resp
}
const stream = async (id, options = {}) => {
let client = await getClient(id, true)
let file = id.split('/').pop()
let startAt = (options && options.range && options.range.start) ? options.range.start : 0;
if (client) {
let dest = new PassThrough()
client.download(dest, file)
return {
stream: dest,
acceptRanges:false
}
} else {
return null
}
}
return { name, version, drive: { protocols, folder, file, stream } }
}