forked from reruin/sharelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.sftp.js
150 lines (115 loc) · 2.95 KB
/
drive.sftp.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
/**
* SFTP
*/
const { PassThrough } = require('stream')
const SFTPClient = require('ssh2-sftp-client');
const { URL } = require("url")
const CLOSED = Symbol('client_closed')
class SFTP {
constructor() {
this.name = 'sftp'
this.label = 'SFTP'
this.mountable = true
this.cache = false
this.version = '1.0'
this.protocol = 'sftp'
this.clientMap = {}
}
async getClient (url, cd = false) {
let { clientMap } = this
let key = (url.match(/\/\/[\w\W]+?\//) || [''])[0];
let { username, password, hostname, port, pathname } = new URL(url);
if (clientMap[key] && clientMap[key][CLOSED] !== true) {
return { client : clientMap[key] , path:pathname }
}
let client = new SFTPClient();
let options = {
host:hostname
}
if(port) options.port = port
if( username && password) {
options.username = decodeURIComponent(username)
options.password = decodeURIComponent(password)
}
try {
await client.connect( options )
client.on('close' , () => {
client[CLOSED] = true
})
clientMap[key] = client
return { client , path:pathname }
} catch (err) {
console.log(err)
return { }
}
}
createId(parent , current){
return parent.replace(/\/$/,'') + '/' + current
}
async folder(id) {
let { datetime, extname , getConfig , cache } = this.helper
let { protocol } = this
if(!id.startsWith(protocol)) id = protocol + ':' + id
let { client , path } = await this.getClient(id)
if (client) {
let data = await client.list(path);
let children = data.map(i => {
let obj = {
id: this.createId(id , i.name),
name: i.name,
protocol,
size: i.size,
created_at: null,
updated_at: datetime(i.modifyTime),
ext: extname(i.name),
type: 'other'
}
/*
d / l / -
*/
if (i.type == 'd') {
obj.type = 'folder'
}
return obj
})
return { id: id, type: 'folder', protocol , children }
} else {
return false
}
}
async file(id) {
let { basename , extname } = this.helper
let { protocol } = this
let { client , path } = await this.getClient(id)
let resp = {
id,
ext: extname(id),
url: id,
protocol,
outputType: 'stream',
proxy: true
}
if(client){
let info = await client.stat(path)
resp.size = info.size
}
resp.name = id.split('/').pop()
return resp
}
async createReadStream({id , size , options = {}} = {}) {
let { client , path } = await this.getClient(id)
let resp = {}
if (client) {
let dest = new PassThrough()
let cfg = { autoClose:false }
if(options.encoding){
cfg.encoding = options.encoding
}
client.get(path, dest, cfg)
return dest
} else {
return null
}
}
}
module.exports = SFTP