11import * as vscode from 'vscode' ;
22import { FileStat , WebDAVClient , WebDAVClientOptions , WebDAVClientError , AuthType , createClient } from 'webdav' ;
33import { parse } from 'date-fns' ;
4- import { Buffer } from "buffer"
4+ import { Buffer } from "buffer"
55
6- let authToken : string | undefined = undefined ;
6+ let authToken : string | undefined = undefined ;
77
88async function getAuthToken ( ) : Promise < string > {
9- if ( authToken )
9+ if ( authToken )
1010 return authToken ;
11- authToken = await fetch ( "/syscall/getToken" ) . then ( r => r . text ( ) ) as string ;
11+ authToken = await fetch ( "/syscall/getToken" ) . then ( r => r . text ( ) ) as string ;
1212 return authToken ;
1313}
1414
@@ -49,7 +49,7 @@ export async function resetAuth() {
4949
5050export async function openDefaultFolder ( ) {
5151 console . log ( "opening defauilt" ) ;
52- let uriValue = "https://dav.puter.com/" + await fetch ( "/syscall/getUsername" ) . then ( r => r . text ( ) ) ;
52+ let uriValue = "https://dav.puter.com/" + await fetch ( "/syscall/getUsername" ) . then ( r => r . text ( ) ) ;
5353 let name = uriValue . split ( "/" ) . pop ( )
5454 let webdavUri = vscode . Uri . parse ( uriValue . trim ( ) . replace ( / ^ h t t p / i, 'webdav' ) ) ;
5555 console . log ( "webdavuri: " , webdavUri ) ;
@@ -64,18 +64,18 @@ export async function openDefaultFolder() {
6464 name : name ?. trim ( ) ?? webdavUri . authority ,
6565 } ,
6666 ) ;
67- const openedPath = await fetch ( "/syscall/getOpenedFile" ) . then ( r => r . text ( ) ) ;
67+ const openedPath = await fetch ( "/syscall/getOpenedFile" ) . then ( r => r . text ( ) ) ;
6868 if ( openedPath ) {
6969 console . log ( "opened path: " , openedPath ) ;
7070 const document = await vscode . workspace . openTextDocument ( vscode . Uri . parse ( ( "https://dav.puter.com/" + openedPath ) . trim ( ) . replace ( / ^ h t t p / i, 'webdav' ) ) ) ;
7171 await vscode . window . showTextDocument ( document ) ;
7272 console . log ( "done opening" )
73- }
73+ }
7474}
7575
7676export async function openWebdav ( ) {
77- let uriValue = await fetch ( "/syscall/getFolderPicker" ) . then ( r => r . text ( ) ) ;
78-
77+ let uriValue = await fetch ( "/syscall/getFolderPicker" ) . then ( r => r . text ( ) ) ;
78+
7979 if ( ! uriValue ) {
8080 return ;
8181 } else {
@@ -154,8 +154,8 @@ export async function configureAuthForUri(uriKey: string): Promise<void> {
154154 delete connections [ uriKey ] ; // The conections are keyed on the baseUri
155155 let authOptions = [ "None" , "Basic" , "Digest" ] ;
156156
157- let settings : AuthSettings = {
158- auth : "Basic"
157+ let settings : AuthSettings = {
158+ auth : "Basic"
159159 } ;
160160 if ( settings . auth === "Basic" || settings . auth === "Digest" ) {
161161 settings . user = "-token" ;
@@ -216,13 +216,13 @@ export class WebDAVFileSystemProvider implements vscode.FileSystemProvider {
216216 log ( `${ operation } : ${ uri } ` ) ;
217217 let baseUri = toBaseUri ( uri ) ;
218218 try {
219- if ( ! connections [ baseUri ] ) {
220- connections [ baseUri ] = this . createClient ( baseUri ) ;
221- }
222219 if ( ! authConfigured [ baseUri ] ) {
223220 await configureAuthForUri ( baseUri ) ;
224221 authConfigured [ baseUri ] = true ;
225222 }
223+ if ( ! connections [ baseUri ] ) {
224+ connections [ baseUri ] = this . createClient ( baseUri ) ;
225+ }
226226 return await action ( await connections [ baseUri ] ) ;
227227 } catch ( e ) {
228228 log ( `${ e } for ${ uri } ` ) ;
@@ -253,7 +253,7 @@ export class WebDAVFileSystemProvider implements vscode.FileSystemProvider {
253253
254254 public async readFile ( uri : vscode . Uri ) : Promise < Uint8Array > {
255255 return await this . forConnection ( "readFile" , uri , async webdav => {
256- return new Uint8Array ( await ( await fetch ( "https://dav.puter.com" + toWebDAVPath ( uri ) , { headers : { "Authorization" : "Bearer " + await getAuthToken ( ) } } ) ) . arrayBuffer ( ) )
256+ return new Uint8Array ( await ( await fetch ( "https://dav.puter.com" + toWebDAVPath ( uri ) , { headers : { "Authorization" : "Bearer " + await getAuthToken ( ) } } ) ) . arrayBuffer ( ) )
257257 // let body = await webdav.getFileContents(toWebDAVPath(uri));
258258 // if (typeof body === "string") {
259259 // return Buffer.from(body, 'binary');
@@ -290,7 +290,7 @@ export class WebDAVFileSystemProvider implements vscode.FileSystemProvider {
290290
291291 public async writeFile ( uri : vscode . Uri , content : Uint8Array , options : { create : boolean , overwrite : boolean } ) : Promise < void > {
292292 return await this . forConnection ( "stat" , uri , async webdav => {
293- ( await fetch ( "https://dav.puter.com" + toWebDAVPath ( uri ) , { method : "PUT" , body : new Blob ( [ content as BlobPart ] ) , headers : { "Authorization" : "Bearer " + await getAuthToken ( ) } } ) )
293+ ( await fetch ( "https://dav.puter.com" + toWebDAVPath ( uri ) , { method : "PUT" , body : new Blob ( [ content as BlobPart ] ) , headers : { "Authorization" : "Bearer " + await getAuthToken ( ) } } ) )
294294
295295 // await this.throwIfWriteFileIsNotAllowed(uri, options);
296296 // await webdav.putFileContents(toWebDAVPath(uri), content, { overwrite: options.overwrite });
0 commit comments