Skip to content

Commit

Permalink
chore: webdav compatibility update
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed Nov 1, 2021
1 parent b5ea6d5 commit 8a97259
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ body:
description: Sharelist Version
options:
- v0.1
- latest
- next
validations:
required: true
- type: textarea
Expand Down
2 changes: 1 addition & 1 deletion packages/sharelist-web/src/views/manage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default defineComponent({
const confirmReload = useConfirm(reload, '确认', '确认重启?')

const tabsSlots = {
tabBarExtraContent: () => <div style="cursor:pointer;" title="保存配置 / Save config" onClick={exportConfig} style="font-size:12px;color:#666;"><SaveOutlined style={{ fontSize: '15px', 'marginRight': '6px' }} />导出配置</div>
tabBarExtraContent: () => <div style="cursor:pointer;font-size:12px;color:#666;" title="保存配置 / Save config" onClick={exportConfig} ><SaveOutlined style={{ fontSize: '15px', 'marginRight': '6px' }} />导出配置</div>
}
return () => (
<div class="setting">
Expand Down
16 changes: 8 additions & 8 deletions packages/sharelist/app/controller/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const { createRuntime, selectSource, send, sendfile } = require('./shared')
const path = require('path')
const fs = require('fs')

const getConfig = (app) => {
const getConfig = (app, raw = false) => {
let { sharelist, controller } = app
if (raw) return sharelist.config
let config = { ...sharelist.config }
if (config.drives) {
config.drives = sharelist.getDisk()
Expand Down Expand Up @@ -52,14 +53,14 @@ module.exports = {
}

},
async config(ctx, next) {
let config = getConfig(this.app)
ctx.body = { data: { title: config.title } }
},

async setting(ctx, next) {
ctx.body = { data: getConfig(this.app) }
ctx.body = { data: getConfig(this.app, !!ctx.query.raw) }
},
async reload(ctx, next) {
await this.app.sharelist.reload()
ctx.body = { status: 0 }
},

async updateSetting(ctx, next) {
let data = { ...ctx.request.body }
for (let i in data) {
Expand All @@ -69,7 +70,6 @@ module.exports = {
}
this.app.sharelist.config[i] = val
}
// this.app.sharelist.configUpdated()

ctx.body = { data: getConfig(this.app) }
},
Expand Down
2 changes: 1 addition & 1 deletion packages/sharelist/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module.exports = app => {
router
.get('/api/setting', auth, controller.api.setting)
.post('/api/setting', auth, controller.api.updateSetting)
.get('/api/config', auth, controller.api.config)
.put('/api/cache/clear', auth, controller.api.clearCache)
.put('/api/reload', auth, controller.api.reload)

.post('/api/drive/list', controller.api.list)
.post('/api/drive/get', controller.api.get)
Expand Down
2 changes: 1 addition & 1 deletion packages/sharelist/package/sharelist/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exports.getFiles = async (sharelist, runtime) => {
try {
data = await sharelist.list(runtime)
} catch (e) {
console.trace(e)
//console.trace(e)
return { error: { code: e.code || 500, msg: e.message } }
}
if (data.files?.length > 0) {
Expand Down
25 changes: 15 additions & 10 deletions packages/sharelist/package/webdav/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ const createDriver = (driver, { proxy, baseUrl } = {}) => {
async get(path, options) {
let data = await driver.get({ paths: parsePath(path) })
if (!options.reqHeaders) options.reqHeaders = {}
delete options.reqHeaders.connection
options.reqHeaders['user-agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'

if (data && data.download_url && !data.extra.proxy && !proxy()) {
return {
status: 302,
Expand Down Expand Up @@ -206,14 +206,19 @@ const createDriver = (driver, { proxy, baseUrl } = {}) => {
return (cmd, ...options) => commands[cmd]?.(...options)
}

const isWebDAVRequest = (ctx) => {
return /(Microsoft\-WebDAV|FileExplorer|WinSCP|WebDAVLib|WebDAVFS|rclone|Kodi|davfs2|sharelist\-webdav|RaiDrive|nPlayer|LibVLC)/i.test(ctx.request.headers['user-agent']) || ('translate' in ctx.request.headers) || ('overwrite' in ctx.request.headers) || ('depth' in ctx.request.headers)
const isWebDAVRequest = (ctx, webdavPath) => {
if (webdavPath == '/') {
return /(Microsoft\-WebDAV|FileExplorer|WinSCP|WebDAVLib|WebDAVFS|rclone|Kodi|davfs2|sharelist\-webdav|RaiDrive|nPlayer|LibVLC|PotPlayer)/i.test(ctx.request.headers['user-agent']) || ('translate' in ctx.request.headers) || ('overwrite' in ctx.request.headers) || ('depth' in ctx.request.headers)
} else {
return ctx.params.path.startsWith(webdavPath)
}
}

module.exports = (app) => {
app.addSingleton('webdav', async () => {
const { config } = app.sharelist
const webdavPath = config.webdav_path || '/'

const webdavServer = new WebDAVServer({
driver: createDriver(app.sharelist, {
request: app.curl,
Expand All @@ -226,15 +231,14 @@ module.exports = (app) => {
}
})

app.router.all(webdavPath + ':path(.*)', async (ctx, next) => {
if (webdavPath == '/' || webdavPath == '') {
if (!isWebDAVRequest(ctx)) {
await next()
return
}
app.router.all(':path(.*)', async (ctx, next) => {
let webdavPath = config.webdav_path || '/'
if (!isWebDAVRequest(ctx, webdavPath)) {
await next()
return
}
console.log('[WebDAV]', ctx.method, ctx.url, '<-->', ctx.ip)
const resp = await webdavServer.request(ctx.req)
const resp = await webdavServer.request(ctx.req, { base: webdavPath })
const { headers, status, body } = resp
if (status == 302) {
ctx.redirect(body)
Expand All @@ -247,6 +251,7 @@ module.exports = (app) => {
if (status) {
ctx.status = parseInt(status)
}

if (body) {
// ctx.set('Content-Length', body.length)
ctx.body = body
Expand Down

0 comments on commit 8a97259

Please sign in to comment.