forked from hyoo-ru/mam_mol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebdav.ts
97 lines (74 loc) · 1.91 KB
/
webdav.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
namespace $ {
export class $mol_webdav extends $mol_http {
@ $mol_mem_key
static item( uri : string ) {
return this.make({
uri : $mol_const( uri ) ,
})
}
@ $mol_mem
data_tree() {
const dom = this.response().xml() as XMLDocument
const responses = dom.querySelectorAll( 'response' ) as any as Element[]
const data = {} as { [ uri : string ] : Element }
for( let response of responses ) {
const uri = this.resolve( response.querySelector( 'href' ).textContent ).uri()
data[ uri ] = response
}
return data
}
data_self() {
return this.parent().data_tree()
}
parent() {
return $mol_webdav.item( this.uri().replace( /\/[^\/]*\/?$/ , '/' ) )
}
@ $mol_mem
sub() {
const next = [] as $mol_webdav[]
for( let uri of Object.keys( this.data_tree() ) ) {
if( uri == this.uri() ) continue
next.push( $mol_webdav.item( uri ) )
}
return next
}
depth() {
return 1
}
headers() {
return {
'Depth' : String( this.depth() )
}
}
method_get() {
return 'PROPFIND'
}
resolve( uri : string ) : $mol_webdav {
if( !uri ) return this
if( /^[-\w]+:/.test( uri ) ) {
return $mol_webdav.item( uri )
}
if( uri[0] === '/' ) {
return $mol_webdav.item( this.uri().replace( /^([^\/]+\/\/[^\/]+).*/, '$1' ) + uri )
}
let res = this.uri() + '/' + uri
while( true ) {
let prev = res
res = res.replace( /\/[^\/]+\/\.\.\// , '/' )
if( prev === res ) break
}
while( true ) {
let prev = res
res = res.replace( /\/\.\.\/[^\/]+\// , '/' )
if( prev === res ) break
}
return ( this.constructor as typeof $mol_webdav ).item( res )
}
prop( prop: string ) {
return this.data_self()[ this.uri() ].querySelector( prop ).textContent
}
type() {
return this.data_self()[ this.uri() ].querySelector( 'collection' ) ? 'dir' : 'file'
}
}
}