Skip to content

Commit

Permalink
feat: support sftp upload
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed Jun 16, 2020
1 parent 571f284 commit 8495a81
Showing 1 changed file with 70 additions and 49 deletions.
119 changes: 70 additions & 49 deletions plugins/drive.sftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,70 +61,75 @@ class SFTP {
return parent.replace(/\/$/,'') + '/' + current
}

async folder(id) {
let { datetime, extname , getConfig , cache } = this.helper

async mkdir(id) {
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
if( client ){
await client.mkdir(path , true);
}

}

async path(id) {
let { datetime, extname , getConfig , cache } = this.helper

async file(id) {
let { basename , extname } = this.helper
let { protocol } = this

let { client , path } = await this.getClient(id)
if(!id.startsWith(protocol)) id = protocol + ':' + id

let resp = {
id,
ext: extname(id),
url: id,
protocol,
outputType: 'stream',
proxy: true
}
let { client , path } = await this.getClient(id)

if(client){
if (client) {
let info = await client.stat(path)
resp.size = info.size

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
}
}
}
}

resp.name = id.split('/').pop()
}
async folder(id) {
return await this.path(id)
}

return resp
async file(id) {
return await this.path(id)
}

async createReadStream({id , size , options = {}} = {}) {
Expand All @@ -144,6 +149,22 @@ class SFTP {
}
}

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
}

}


Expand Down

0 comments on commit 8495a81

Please sign in to comment.