Skip to content

Commit

Permalink
feat: support teambition
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed Dec 10, 2020
1 parent 2c99fb1 commit 5e2e298
Show file tree
Hide file tree
Showing 8 changed files with 1,111 additions and 19 deletions.
10 changes: 7 additions & 3 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const addr = require('./middleware/koa-addr')
const paths = require('./middleware/koa-paths')
const render = require('./middleware/koa-render')
const staticCache = require('koa-static-cache')
const Router = require('koa-router')

const routers = require('./routers/index')
const cors = require('@koa/cors')
Expand All @@ -28,11 +29,16 @@ const fs = require('fs')
const app = new Koa()
app.proxy = true

const createRouter = () => new Router()

const getWeb = () => app

loader('plugin', {
dirs: [__dirname + '/plugins',path.resolve(__dirname,'../plugins')],
router:createRouter , web:getWeb
})

loader('endpoints')
loader('endpoints',{ router:createRouter , web:getWeb })

onerror(app)

Expand Down Expand Up @@ -91,6 +97,4 @@ app.use(async (ctx) => {
}
})

bonjour.publish({'name':'Sharelist',type:"http",port:config.getConfig('port')})

module.exports = app
23 changes: 16 additions & 7 deletions 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 {getFileType , getMIME , isArray , isObject , params , base64 , getRandomIP , retrieveSize , extname , pathNormalize, parseStream , xml2json } = require('../utils/base')
const {getFileType , getMIME , isArray , isObject , params , base64 , getRandomIP , retrieveSize , extname , pathNormalize, parseStream , xml2json , match } = require('../utils/base')
const format = require('../utils/format')
const cache = require('../utils/cache')
const http = require('../utils/http')
Expand Down Expand Up @@ -246,6 +246,7 @@ const getHelpers = (id) => {
chunkStream,
recognize,
xml2json,
match:match,
getOption:()=>{

},
Expand Down Expand Up @@ -291,16 +292,22 @@ const getHelpers = (id) => {
/**
* 加载插件
*/
var loadOptions = []
var loadOptions = {}
const load = (options) => {
loadOptions = options
const dir = options.dir
const dirs = options.dirs

let { dir, dirs , ...rest } = options

if (dir && dirs.indexOf(dir) === -1) {
dirs.push(dir)
}


const services = { ...rest }
services.command = command
services.setRuntime = config.setRuntime
services.getConfig = config.getConfig
services.bonjour = bonjour
services.sendStream = getStream
for (let i = 0; i < dirs.length; i++) {
const p = dirs[i]
if (!fs.existsSync(p)) {
Expand All @@ -326,7 +333,7 @@ const load = (options) => {

let resource
if( isClass(ins) ){
let driver = new ins(helpers)
let driver = new ins(helpers,services)
let { protocol , mountable , createReadStream , createWriteStream } = driver
driver.helper = helpers
resources[id] = {
Expand Down Expand Up @@ -407,11 +414,12 @@ const loader = (type, ...rest) => {
load(...rest)
}else{
const files = fs.readdirSync(path.resolve(__dirname,'../../'+type))
const services = {}
const services = { ...rest[0] }
services.command = command
services.setRuntime = config.setRuntime
services.getConfig = config.getConfig
services.bonjour = bonjour
services.sendStream = getStream

for (let i = 0; i<files.length;i++) {
let filepath = path.join(path.resolve(__dirname,'../../'+type),files[i])
Expand Down Expand Up @@ -627,4 +635,5 @@ const createWriteStream = async (options) => {
}
}


module.exports = { load , reload , getDrive , getStream , getSource , updateFolder , updateFile , updateLnk , getVendors , getAuth , getPreview , isPreviewable , command , loader , bonjour}
27 changes: 27 additions & 0 deletions app/utils/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,32 @@ const xml2json = ( xml , options = {}) => {
})
}

const match = (route , pathname) => {
let optionalParam = /\((.*?)\)/g ,
namedParam = /(\(\?)?:\w+/g,
splatParam = /\*\w+/g,
escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
let route_new = route.replace(escapeRegExp, '\\$&')
.replace(optionalParam, '(?:$1)?')
.replace(namedParam, function(match, optional) {
return optional ? match : '([^/?]+)';
})
.replace(splatParam, '([^?]*?)');
let expr = new RegExp('^' + route_new + '(?:\\?([\\s\\S]*))?$');
let res = expr.exec(route).slice(1)
res.pop()
let key = res.map( i => i.replace(/^\:/,''))
let hit = expr.exec( pathname )
let params = {}
if( hit ){
hit = hit.slice(1)
key.forEach((i , idx) => {
params[i] = hit[idx]
})
}
return params
}

module.exports = {
parsePath,
getFileType,
Expand All @@ -183,6 +209,7 @@ module.exports = {
md5,
parseStream,
xml2json,
match,
params(url) {
url = url.split('?')[1]
let reg = /(?:&)?([^=]+)=([^&]*)/ig,
Expand Down
11 changes: 11 additions & 0 deletions docs/zh-cn/plugins/teambition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Teambition

[drive.teambition.js](https://github.com/reruin/sharelist/tree/master/plugins/drive.teambition.js) 插件实现。
``` 挂载路径
挂载路径留空
```
```挂载路径留空```,ShareList将自动开启挂载向导,按指示填写用户名、密码、初始路径(可留空)即可。

?> 初始路径可用于挂载指定目录,从官网访问到对应文件夹内,复制浏览器URL 作为初始路径即可,它的格式如下:```https://www.teambition.com/pan/org/xxxxxxxxxx/space/xxxxxxxxxx/folder/xxxxxxxxxx```

?> 未适配国际版。
67 changes: 67 additions & 0 deletions endpoints/rss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class RSS {
constructor(app) {
this.name = 'RSS Server'
this.app = app
this.path = '/_rss'
this.start()
}

start() {
let { app, path } = this
let port = app.getConfig('port')
let router = app.router().all(this.path + ':path(.*)', async (ctx, next) => {
await this.onRequest(ctx, next)
})
app.web().use(router.routes())

this.zeroconf = app.bonjour.publish({ name: 'ShareList RSS', type: 'http', port, txt: { path } })
}

async onRequest(ctx, next) {
let url = ctx.params.path
let resp = await this.app.command('ls', url)
if( resp ){
let k = this.createDir(resp.children)
ctx.type = 'application/rss+xml'
ctx.body = k
}else{
ctx.body = {
code:404
}
}

await next()
}

createDir(items) {
let path = ''
let body = items.filter(i => i.hidden !== true).map(i => {
let href = ((path + '/' + encodeURIComponent(i.name))).replace(/\/{2,}/g, '/')
return `<item>
<title>${i.name}</title>
<link>${href}</link>
<description>${i.name}</description>
<pubDate>${i.updated_at}</pubDate>
<enclosure url="${href}"
length="${i.size}" type="${i.type}" />
</item>`
})

return `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
${body.join('')}
</rss>`
}

restart() {
if (this.zeroconf) {
this.zeroconf.stop(()=>{
this.start()
})
}else{
this.start()
}
}
}

module.exports = RSS
Loading

0 comments on commit 5e2e298

Please sign in to comment.