- Fix Delete button in webdav

- Other bug fixes
This commit is contained in:
Mukhtar Akere
2025-05-16 16:43:01 +01:00
parent b984697fe3
commit 35a74d8dba
6 changed files with 50 additions and 23 deletions
+2
View File
@@ -7,6 +7,7 @@ import (
// FileInfo implements os.FileInfo for our WebDAV files
type FileInfo struct {
id string
name string
size int64
mode os.FileMode
@@ -19,4 +20,5 @@ func (fi *FileInfo) Size() int64 { return fi.size }
func (fi *FileInfo) Mode() os.FileMode { return fi.mode }
func (fi *FileInfo) ModTime() time.Time { return fi.modTime }
func (fi *FileInfo) IsDir() bool { return fi.isDir }
func (fi *FileInfo) ID() string { return fi.id }
func (fi *FileInfo) Sys() interface{} { return nil }
+38 -17
View File
@@ -258,25 +258,27 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case "PROPFIND":
h.handlePropfind(w, r)
return
default:
handler := &webdav.Handler{
FileSystem: h,
LockSystem: webdav.NewMemLS(),
Logger: func(r *http.Request, err error) {
if err != nil {
h.logger.Trace().
Err(err).
Str("method", r.Method).
Str("path", r.URL.Path).
Msg("WebDAV error")
}
},
case "DELETE":
if err := h.handleDelete(w, r); err == nil {
return
}
handler.ServeHTTP(w, r)
return
// fallthrough to default
}
handler := &webdav.Handler{
FileSystem: h,
LockSystem: webdav.NewMemLS(),
Logger: func(r *http.Request, err error) {
if err != nil {
h.logger.Trace().
Err(err).
Str("method", r.Method).
Str("path", r.URL.Path).
Msg("WebDAV error")
}
},
}
handler.ServeHTTP(w, r)
return
}
func getContentType(fileName string) string {
@@ -459,3 +461,22 @@ func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("DAV", "1, 2")
w.WriteHeader(http.StatusOK)
}
// handleDelete deletes a torrent from using id
func (h *Handler) handleDelete(w http.ResponseWriter, r *http.Request) error {
cleanPath := path.Clean(r.URL.Path) // Remove any leading slashes
_, torrentId := path.Split(cleanPath)
if torrentId == "" {
return os.ErrNotExist
}
cachedTorrent := h.cache.GetTorrent(torrentId)
if cachedTorrent == nil {
return os.ErrNotExist
}
h.cache.OnRemove(cachedTorrent.Id)
w.WriteHeader(http.StatusNoContent)
return nil
}
+3 -2
View File
@@ -119,7 +119,8 @@
{{- if and $.CanDelete }}
<button
class="delete-btn"
data-path="{{printf "%s/%s" $.Path $file.Name}}">
data-name="{{$file.Name}}"
data-path="{{printf "%s/%s" $.Path $file.ID}}">
Delete
</button>
{{- end}}
@@ -130,7 +131,7 @@
document.querySelectorAll('.delete-btn').forEach(btn=>{
btn.addEventListener('click', ()=>{
let p = btn.getAttribute('data-path');
let name = p.split('/').pop();
let name = btn.getAttribute('data-name');
if(!confirm('Delete '+name+'?')) return;
fetch(p, { method: 'DELETE' })
.then(_=>location.reload());