Skip to content

Commit

Permalink
$mol_file webdav 2
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Nov 13, 2024
1 parent 2350b61 commit d4e4d21
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 45 deletions.
2 changes: 1 addition & 1 deletion fetch/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ namespace $ {
const response = this.response( input , init )
if( response.status() === 'success' ) return response

throw new Error( response.message() )
throw new Error( response.message(), { cause: response } )
}

@ $mol_action
Expand Down
21 changes: 8 additions & 13 deletions file/base/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ namespace $ {

@ $mol_mem
version() {
return this.modified()?.getTime().toString( 36 ).toUpperCase() ?? ''
return this.stat()?.mtime.getTime().toString( 36 ).toUpperCase() ?? ''
}

protected info( path: string ) { return null as null | $mol_file_stat }
Expand All @@ -190,8 +190,6 @@ namespace $ {
protected kids() {
return [] as readonly this[]
}
static headers() { return {} as Record<string, string> }
headers() { return (this.constructor as typeof $mol_file_base).headers() }

@ $mol_mem_key
readable(opts: { start?: number, end?: number }) {
Expand All @@ -214,14 +212,6 @@ namespace $ {
if (! this.version() ) return new Uint8Array

next = this.read()
// try {
// } catch (error) {
// // Файл может удалиться между изменением version и read, обрабатываем эту ситуацию
// if (error instanceof $mol_file_error && error.cause.code === 'not found') {
// return new Uint8Array()
// }
// $mol_fail_hidden(error)
// }

const prev = $mol_mem_cached( ()=> this.buffer() )

Expand Down Expand Up @@ -281,8 +271,13 @@ namespace $ {

static watch_root = ''

// static watcher_warned = false
watcher() {
console.warn('$mol_file_web.watcher() not implemented')
// const constructor = this.constructor as typeof $mol_file_base
// if (! constructor.watcher_warned) {
// console.warn(`${constructor}.watcher() not implemented`)
// constructor.watcher_warned = true
// }

return {
destructor() {}
Expand Down Expand Up @@ -353,7 +348,7 @@ namespace $ {
if (! this.exists() ) return []
if ( this.type() !== 'dir') return []

this.stat()
this.version()

// Если дочерний file удалился, список надо обновить
return this.kids().filter(file => file.exists())
Expand Down
38 changes: 38 additions & 0 deletions file/file.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,44 @@ namespace $ {
? new URL( '.' , ($mol_dom_context.document.currentScript as any)['src'] ).toString()
: ''

// Вотчер выключен, версия всегда будет одна
override version() { return '' }
// Ворнинги подавляем, иначе в каждом приложении, загружающим локали, будет ворнинг
// override watcher() { return { destructor() {} }}

protected override info() {
// Директории не поддерживаются
try {
const response = this.fetch({ method: 'HEAD' })
const headers = response.headers()

let size = Number(headers.get('Content-Length'))
if (Number.isNaN(size)) size = 0

const last = headers.get('Last-Modified')

const mtime = last ? new Date(last) : new Date()

return {
type: 'file',
size,
mtime,
atime: mtime,
ctime: mtime,
} as $mol_file_stat

} catch (error) {
if (
error instanceof Error
&& error.cause instanceof $mol_fetch_response
&& error.cause.native.status === 404
) return null

$mol_fail_hidden(error)
}

}

}

$.$mol_file = $mol_file_web
Expand Down
47 changes: 16 additions & 31 deletions file/webdav/webdav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ namespace $ {
return this.absolute<This>( new URL( path , this.base ).toString() )
}

override watcher() {
console.warn('$mol_file_web.watcher() not implemented')

return {
destructor() {}
}
}

override resolve( path : string ) {
let res = this.path() + '/' + path

Expand All @@ -30,7 +22,8 @@ namespace $ {
return ( this.constructor as typeof $mol_file_base ).absolute( res ) as this
}

headers() { return {} as Record<string, string> }
static headers() { return {} as Record<string, string> }
headers() { return (this.constructor as typeof $mol_file_webdav).headers() }

protected fetch(init: RequestInit) {
return this.$.$mol_fetch.success(this.path(), {
Expand All @@ -43,12 +36,18 @@ namespace $ {
}

protected override read() {
const response = this.$.$mol_fetch.response(this.path(), {
headers: this.headers()
})
if (response.native.status === 404) return new Uint8Array

return new Uint8Array(response.buffer())
try {
const response = this.fetch({})
return new Uint8Array(response.buffer())
} catch (error) {
if (
error instanceof Error
&& error.cause instanceof $mol_fetch_response
&& error.cause.native.status === 404
) return new Uint8Array

$mol_fail_hidden(error)
}
}

protected override write( body : Uint8Array ) { this.fetch({ method: 'PUT', body }) }
Expand Down Expand Up @@ -98,22 +97,8 @@ namespace $ {
}).stream() || $mol_fail(new Error('Not found'))
}

protected override info(): $mol_file_stat {
const response = this.fetch({ method: 'HEAD' })
const headers = response.headers()
let size = Number(headers.get('Content-Length'))
if (Number.isNaN(size)) size = 0
const last = headers.get('Last-Modified')

const mtime = last ? new Date(last) : new Date()

return {
type: 'file',
size,
mtime,
atime: mtime,
ctime: mtime,
}
protected override info() {
return this.kids().at(0)?.stat() ?? null
}
}

Expand Down

0 comments on commit d4e4d21

Please sign in to comment.