Skip to content

Commit

Permalink
chore: adjust webdav route
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed Nov 1, 2021
1 parent e1a2a94 commit 3c1ba1f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/sharelist-webdav/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ export default (req: http.IncomingMessage, base: string, allows: Array<string>):
const pairs = Buffer.from(authorization, "base64").toString("utf8").split(':')
ctx.auth = { user: pairs[0], pass: pairs[1] }
}
console.log(ctx.path, ctx.base)
return ctx
}
4 changes: 2 additions & 2 deletions packages/sharelist-webdav/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export class WebDAVServer {
this.allows = Object.keys(this.methods).map(i => i.toUpperCase())//.join(', ')
}

async request(req: WebDAVRequest): Promise<Response> {
const ctx: Context = createContext(req, this.base, this.allows)
async request(req: WebDAVRequest, options?: WebDAVServerOptions): Promise<Response> {
const ctx: Context = createContext(req, options?.base || this.base, this.allows)
if (
!(ctx.method == 'options' && !ctx.path) &&
!this.auth(ctx.auth.user, ctx.auth.pass)
Expand Down
18 changes: 9 additions & 9 deletions packages/sharelist-webdav/src/operations/propfind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const DEFAULT_PROPS = [
*/
const propParse = (data: any) => {
if (!data) return {
ns: { prefix: '', uri: '' },
ns: { prefix: 'D', uri: 'DAV:' },
prop: [...DEFAULT_PROPS]
}
let prop = [...DEFAULT_PROPS]
const prefix = Object.keys(data.propfind.$).find(i => i.startsWith('xmlns:'))?.split(':')[1] || ''
const prefix = Object.keys(data.propfind.$).find(i => i.startsWith('xmlns:'))?.split(':')[1] || 'D'
const uri = data.propfind.$?.[`xmlns${prefix ? `:${prefix}` : ''}`] || ''
if (data.propfind.hasOwnProperty('prop')) {
prop = Object.keys(data.propfind.prop)
Expand Down Expand Up @@ -79,7 +79,7 @@ const fixNs = (data: any, prefix: string) => {
if (!prefix) return data
Object.keys(data).forEach((key: string) => {
const val = data[key]
if (key != '$') {
if (key != '$' && prefix) {
if (Array.isArray(val) || typeof val == 'object') {
fixNs(val, prefix)
}
Expand All @@ -98,14 +98,16 @@ const fixNs = (data: any, prefix: string) => {
const createXML = (data: any, options: any) => {
const { ns: { prefix, uri } } = options

const obj = {
const obj: any = {
multistatus: {
$: {
"xmlns": uri
},
response: convData(data || [], options)
}
}
if (uri) {
obj.multistatus.$ = {
"xmlns": uri
}
}

const builder = new xml2js.Builder({
renderOpts: { pretty: false },
Expand All @@ -122,9 +124,7 @@ export default async (ctx: Context): Promise<Response | undefined> => {
base: ctx.base,
depth: ctx.depth,
}, propParse(await parseXML(ctx.req)))

const data: any = ctx.depth == '0' ? await ctx.driver?.('stat', ctx.path) : await ctx.driver?.('ls', ctx.path)

if (!data) return { status: '404' }

if (data.error) {
Expand Down

0 comments on commit 3c1ba1f

Please sign in to comment.