forked from reruin/sharelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.sftp.js
171 lines (132 loc) · 3.63 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* 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:decodeURIComponent(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:decodeURIComponent(pathname) }
} catch (err) {
console.log(err)
return { }
}
}
createId(parent , current){
return parent.replace(/\/$/,'') + '/' + current
}
async mkdir(id) {
let { protocol } = this
let { client , path } = await this.getClient(id)
if( client ){
await client.mkdir(path , true);
}
}
async path(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 info = await client.stat(path)
if( info ){
if(info.isDirectory){
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 {
id,protocol,
name: id.split('/').pop(),
ext: extname(id),
size: info.size,
url: id,
outputType: 'stream',
proxy: true
}
}
}
}
}
async folder(id) {
return await this.path(id)
}
async file(id) {
return await this.path(id)
}
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
}
}
async createWriteStream({ id, options = {}, target = '' } = {}) {
let newId = this.createId(id , target)
let { client , path } = await this.getClient(newId)
let parentId = newId.split('/').slice(0,-1).join('/')
await this.mkdir(parentId)
let writeStream = new PassThrough()
client.put(writeStream , path , { autoClose:false })
return writeStream
}
}
module.exports = SFTP