Skip to content

Commit

Permalink
+ webdav drive
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed Apr 19, 2019
1 parent 136486e commit ca1dae4
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 15 deletions.
3 changes: 1 addition & 2 deletions app/middleware/koa-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const xml2js = ( xml , options = {}) => {
}

const guessWebDAV = (ua) => {
return /(Microsoft\-WebDAV|FileExplorer|WinSCP)/i.test(ua)
return /(Microsoft\-WebDAV|FileExplorer|WinSCP|WebDAVLib)/i.test(ua)
}

const webdavMethods = ['options','head','trace','get','put','post','delete','mkcol','propfind','proppatch','copy','move','lock','unlock']
Expand All @@ -48,7 +48,6 @@ module.exports = async(ctx, next) => {
origin:ctx.origin,
protocol:ctx.protocol
})

if(webdavPath == ''){
isWebDAV = ctx.is('xml') || guessWebDAV(ctx.request.headers['user-agent'])
}
Expand Down
2 changes: 0 additions & 2 deletions app/plugins/drive.http.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const version = '1.0'

const protocols = ['http']

const url = require("url")

module.exports = (format) => {

const file = (id)=>{
Expand Down
2 changes: 0 additions & 2 deletions app/plugins/drive.https.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const version = '1.0'

const protocols = ['https']

const url = require("url")

module.exports = (format) => {

const file = (id)=>{
Expand Down
3 changes: 2 additions & 1 deletion app/services/plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs')
const path = require('path')
const querystring = require('querystring')
const {MIMEType , isArray , isObject , params , base64 , getRandomIP , retrieveSize } = require('../utils/base')
const {MIMEType , MIME , isArray , isObject , params , base64 , getRandomIP , retrieveSize } = require('../utils/base')
const format = require('../utils/format')
const cache = require('../utils/cache')
const http = require('../utils/http')
Expand Down Expand Up @@ -213,6 +213,7 @@ const updateFolder = (folder) => {

if(d.type != 'folder'){
d.type = MIMEType(d.ext)
if(d.mime) d.mime = MIME(d.ext)
}

d.displaySize = format.byte(d.size)
Expand Down
6 changes: 6 additions & 0 deletions app/utils/base.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const mime = require('mime');

const rnd = (min , max) => Math.floor(min+Math.random()*(max-min))

const isType = (type) => (obj) => ( Object.prototype.toString.call(obj) === `[object ${type}]`)
Expand Down Expand Up @@ -54,6 +56,10 @@ const MIMEType = (v) => {
}
}

const MIME = (v) => {
return mime.getType(v)
}

const extend = (source , src) => {
for(var i in src){
source[i] = src[i]
Expand Down
8 changes: 5 additions & 3 deletions app/utils/sendfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const getHTTPFile = async (url ,headers = {}) => {
}

const sendStream = async (ctx , url , stream , data) => {
let fileSize = null , start;
let fileSize = null , start , options_range = [];
if(data && data.size){
fileSize = data.size;
}
Expand All @@ -147,15 +147,17 @@ const sendStream = async (ctx , url , stream , data) => {
ctx.status = 206

chunksize = end - start + 1

options_range = [start , end]
}else{
ctx.set('Content-Range', 'bytes ' + `0-${fileSize-1}/${fileSize}`)
}

ctx.length = chunksize
}

ctx.body = await stream(url , start , ctx.res)
let opts = { ...data , range:options_range , writeStream:ctx.res}

ctx.body = await stream(url , opts)
}

module.exports = { sendFile , sendHTTPFile , sendStream , getHTTPFile , getFile }
1 change: 1 addition & 0 deletions example/link_webdav.d.ln
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
webdav:https://sharelist.reruin.net/webdav
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
"koa-views": "^5.2.1",
"less-middleware": "^2.2.1",
"lowdb": "^1.0.0",
"mime": "^2.3.1",
"mime": "^2.4.2",
"pug": "^2.0.0-rc.1",
"webdav": "^2.6.0",
"xml2js": "^0.4.19",
"yaml": "^1.0.0-rc.8"
},
Expand Down
8 changes: 5 additions & 3 deletions plugins/drive.ftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,14 @@ module.exports = ({ getConfig, cache }) => {
return resp
}

const stream = async (id, startAt = 0, writableStream) => {
const stream = async (id, options = {}) => {
let client = await getClient(id, true)
let file = id.split('/').pop()
let startAt = (options && options.range && options.range[0]) ? options.range[0] : 0;
let writeStream = options.writeStream;
if (client) {
if (writableStream) {
return await client.download(writableStream, file, startAt)
if (writeStream) {
return await client.download(writeStream, file, startAt)
} else {
let dest = new Writable()
await client.download(dest, file, startAt)
Expand Down
136 changes: 136 additions & 0 deletions plugins/drive.webdav.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* WebDAV
*/


const name = 'webdav'

const version = '1.0'

const protocols = ['webdav']

const defaultProtocol = 'webdav'

const path = require('path')

const { createClient } = require("webdav");

const { URL } = require("url")

const { Writable } = require('stream')

const clientMap = {}

module.exports = ({ getConfig, cache , base64 }) => {

const extname = (p) => path.extname(p).substring(1)

const getPath = (url) => {
let { pathname } = new URL('ftp:' + url);
return pathname
}

const getClient = async (url, cd = false) => {
let key = (url.match(/https?\:\/\/[\w\W]+?\//) || [''])[0];
let { protocol , username, password, host, port, pathname } = new URL(url);
let client = clientMap[key]
let remote_url = protocol + '//' + host + pathname
console.log(remote_url , username , password)
if (!client) {
client = createClient(remote_url,{
username:decodeURIComponent(username),password:decodeURIComponent(password)
});

clientMap[key] = client
}

return client;

}

const folder = async (id) => {
let [server , path] = id.split('>');
let resp = { id: id, type: 'folder', protocol: defaultProtocol }

/*
let resid = `${defaultProtocol}:${id}`
let r = cache(resid)
if (r) {
resp = r
if (
resp.$cached_at &&
resp.children &&
(Date.now() - resp.$cached_at < getConfig().max_age_dir)
) {
console.log('get webdav folder from cache')
return resp
}
}
*/

let client = await getClient(server)

if (client) {
let data = await client.getDirectoryContents(path || '/');

let children = [];
data.forEach(i => {
let path = (server + '>' + i.filename)
let obj = {
id: path,
name: i.basename,
protocol: defaultProtocol,
size: i.size,
created_at: i.lastmod,
updated_at: i.lastmod,
ext: extname(i.basename),
mime:i.mime,
type: i.type == 'directory' ? 'folder' : 'other'
}

children.push(obj)
})

resp.$cached_at = Date.now()
resp.children = children
//cache(resid, resp)

return resp
} else {
return false
}
}

const file = async (id , data = {}) => {

data.outputType = 'stream'
data.proxy = 'stream'

return data
}

const stream = async (id, options = {}) => {
let [server , path] = id.split('>');
let client = await getClient(server, true)

if (client) {
let writeStream = options.writeStream;
let headers = Object.assign({},options.headers);

if(options.range && options.range.length == 2){
headers['Content-Range'] = 'bytes ' + `${options.range[0]}-${options.range[1]}/${data.size}`
}
if (writeStream) {
return client.createReadStream(path , {headers}).pipe(writeStream)
} else {
return await client.getFileContents(path)
}
} else {
return null
}
}

return { name, version, drive: { protocols, folder, file, stream } }
}
55 changes: 54 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"

base-64@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/base-64/-/base-64-0.1.0.tgz#780a99c84e7d600260361511c4877613bf24f6bb"

base@^0.11.1:
version "0.11.2"
resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f"
Expand Down Expand Up @@ -1133,6 +1137,10 @@ [email protected]:
version "2.16.3"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-2.16.3.tgz#20bb7403d3cea398e91dc4710a8ff1b8274a25ed"

hot-patcher@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/hot-patcher/-/hot-patcher-0.5.0.tgz#9d401424585aaf3a91646b816ceff40eb6a916b9"

http-assert@^1.3.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/http-assert/-/http-assert-1.4.0.tgz#0e550b4fca6adf121bbeed83248c17e62f593a9a"
Expand Down Expand Up @@ -1770,6 +1778,10 @@ [email protected]:
version "0.3.0"
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"

merge@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.1.tgz#38bebf80c3220a8a487b6fcfb3941bb11720c145"

methods@^1.0.1, methods@^1.1.1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
Expand Down Expand Up @@ -1806,10 +1818,14 @@ mime@^1.2.11:
version "1.6.0"
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"

mime@^2.2.0, mime@^2.3.1:
mime@^2.2.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/mime/-/mime-2.4.0.tgz#e051fd881358585f3279df333fe694da0bcffdd6"

mime@^2.4.2:
version "2.4.2"
resolved "https://registry.yarnpkg.com/mime/-/mime-2.4.2.tgz#ce5229a5e99ffc313abac806b482c10e7ba6ac78"

minimatch@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
Expand Down Expand Up @@ -2132,6 +2148,10 @@ path-parse@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c"

path-posix@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/path-posix/-/path-posix-1.0.0.tgz#06b26113f56beab042545a23bfa88003ccac260f"

path-to-regexp@^1.1.1:
version "1.7.0"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-1.7.0.tgz#59fde0f435badacba103a84e9d3bc64e96b9937d"
Expand Down Expand Up @@ -2372,6 +2392,10 @@ qs@~6.4.0:
version "6.4.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.4.0.tgz#13e26d28ad6b0ffaa91312cd3bf708ed351e7233"

querystringify@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.1.1.tgz#60e5a5fd64a7f8bfa4d2ab2ed6fdf4c85bad154e"

random-bytes@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/random-bytes/-/random-bytes-1.0.0.tgz#4f68a1dc0ae58bd3fb95848c30324db75d64360b"
Expand Down Expand Up @@ -2488,6 +2512,10 @@ [email protected]:
tunnel-agent "^0.6.0"
uuid "^3.0.0"

requires-port@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"

resolve-path@^1.3.1:
version "1.4.0"
resolved "https://registry.yarnpkg.com/resolve-path/-/resolve-path-1.4.0.tgz#c4bda9f5efb2fce65247873ab36bb4d834fe16f7"
Expand Down Expand Up @@ -2987,12 +3015,23 @@ urix@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72"

url-join@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/url-join/-/url-join-4.0.0.tgz#4d3340e807d3773bda9991f8305acdcc2a665d2a"

url-parse-lax@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/url-parse-lax/-/url-parse-lax-1.0.0.tgz#7af8f303645e9bd79a272e7a14ac68bc0609da73"
dependencies:
prepend-http "^1.0.1"

url-parse@^1.4.4:
version "1.4.6"
resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.4.6.tgz#baf91d6e6783c8a795eb476892ffef2737fc0456"
dependencies:
querystringify "^2.0.0"
requires-port "^1.0.0"

url-template@^2.0.8:
version "2.0.8"
resolved "https://registry.yarnpkg.com/url-template/-/url-template-2.0.8.tgz#fc565a3cccbff7730c775f5641f9555791439f21"
Expand Down Expand Up @@ -3051,6 +3090,20 @@ vxx@^1.2.0:
shimmer "^1.0.0"
uuid "^3.0.1"

webdav@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/webdav/-/webdav-2.6.0.tgz#28918f01c2e1d0757c7bc719a1b240976db56979"
dependencies:
axios "^0.18.0"
base-64 "^0.1.0"
hot-patcher "^0.5.0"
merge "^1.2.1"
minimatch "^3.0.4"
path-posix "^1.0.0"
url-join "^4.0.0"
url-parse "^1.4.4"
xml2js "^0.4.19"

which@^1.2.9:
version "1.3.1"
resolved "https://registry.yarnpkg.com/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a"
Expand Down

0 comments on commit ca1dae4

Please sign in to comment.