11import { AuthType , createClient , FileStat , WebDAVClient } from "webdav/web" ;
22import FileSystem , { File , FileReader , FileWriter } from "../filesystem" ;
3+ import { joinPath } from "../utils" ;
34import { WebDAVFileReader , WebDAVFileWriter } from "./rw" ;
45
56export default class WebDAVFileSystem implements FileSystem {
67 client : WebDAVClient ;
78
8- basePath : string = "" ;
9+ basePath : string = "/ " ;
910
1011 constructor (
1112 authType : AuthType | WebDAVClient ,
@@ -15,10 +16,7 @@ export default class WebDAVFileSystem implements FileSystem {
1516 ) {
1617 if ( typeof authType === "object" ) {
1718 this . client = authType ;
18- this . basePath = url || "" ;
19- if ( ! this . basePath . endsWith ( "/" ) ) {
20- this . basePath += "/" ;
21- }
19+ this . basePath = joinPath ( url || "" ) ;
2220 } else {
2321 this . client = createClient ( url ! , {
2422 authType,
@@ -34,37 +32,34 @@ export default class WebDAVFileSystem implements FileSystem {
3432 }
3533
3634 open ( file : File ) : Promise < FileReader > {
37- const path = file . name ;
3835 return Promise . resolve (
39- new WebDAVFileReader ( this . client , this . getPath ( path ) )
36+ new WebDAVFileReader ( this . client , joinPath ( file . path , file . name ) )
4037 ) ;
4138 }
4239
4340 openDir ( path : string ) : Promise < FileSystem > {
44- return Promise . resolve ( new WebDAVFileSystem ( this . client , path ) ) ;
41+ return Promise . resolve (
42+ new WebDAVFileSystem ( this . client , joinPath ( this . basePath , path ) )
43+ ) ;
4544 }
4645
4746 create ( path : string ) : Promise < FileWriter > {
4847 return Promise . resolve (
49- new WebDAVFileWriter ( this . client , this . getPath ( path ) )
48+ new WebDAVFileWriter ( this . client , joinPath ( this . basePath , path ) )
5049 ) ;
5150 }
5251
5352 createDir ( path : string ) : Promise < void > {
54- return this . client . createDirectory ( this . getPath ( path ) ) ;
53+ return this . client . createDirectory ( joinPath ( this . basePath , path ) ) ;
5554 }
5655
5756 async delete ( path : string ) : Promise < void > {
58- return this . client . deleteFile ( this . getPath ( path ) ) ;
59- }
60-
61- getPath ( path : string ) : string {
62- return this . basePath + path ;
57+ return this . client . deleteFile ( joinPath ( this . basePath , path ) ) ;
6358 }
6459
6560 async list ( ) : Promise < File [ ] > {
6661 const dir = ( await this . client . getDirectoryContents (
67- this . getPath ( this . basePath )
62+ this . basePath
6863 ) ) as FileStat [ ] ;
6964 const ret : File [ ] = [ ] ;
7065 dir . forEach ( ( item : FileStat ) => {
@@ -73,10 +68,7 @@ export default class WebDAVFileSystem implements FileSystem {
7368 }
7469 ret . push ( {
7570 name : item . basename ,
76- path : item . filename . substring (
77- 0 ,
78- item . filename . length - item . basename . length
79- ) ,
71+ path : this . basePath ,
8072 digest : item . etag || "" ,
8173 size : item . size ,
8274 createtime : new Date ( item . lastmod ) . getTime ( ) ,
0 commit comments