fix: remove associated shares when deleting file/folder

This commit is contained in:
Stavros Tsioulis 2025-07-03 07:42:55 +03:00 committed by GitHub
parent 47b3e218ad
commit e99e0b3028
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -73,6 +74,11 @@ func resourceDeleteHandler(fileCache FileCache) handleFunc {
return errToStatus(err), err return errToStatus(err), err
} }
err = d.store.Share.DeleteWithPathPrefix(file.Path)
if err != nil {
log.Printf("WARNING: Error(s) occurred while deleting associated shares with file: %s", err)
}
// delete thumbnails // delete thumbnails
err = delThumbs(r.Context(), fileCache, file) err = delThumbs(r.Context(), fileCache, file)
if err != nil { if err != nil {

View File

@ -15,6 +15,7 @@ type StorageBackend interface {
Gets(path string, id uint) ([]*Link, error) Gets(path string, id uint) ([]*Link, error)
Save(s *Link) error Save(s *Link) error
Delete(hash string) error Delete(hash string) error
DeleteWithPathPrefix(path string) error
} }
// Storage is a storage. // Storage is a storage.
@ -118,3 +119,7 @@ func (s *Storage) Save(l *Link) error {
func (s *Storage) Delete(hash string) error { func (s *Storage) Delete(hash string) error {
return s.back.Delete(hash) return s.back.Delete(hash)
} }
func (s *Storage) DeleteWithPathPrefix(path string) error {
return s.back.DeleteWithPathPrefix(path)
}

View File

@ -75,3 +75,16 @@ func (s shareBackend) Delete(hash string) error {
} }
return err return err
} }
func (s shareBackend) DeleteWithPathPrefix(pathPrefix string) error {
var links []share.Link
if err := s.db.Prefix("Path", pathPrefix, &links); err != nil {
return err
}
var err error
for _, link := range links {
err = errors.Join(err, s.db.DeleteStruct(&share.Link{Hash: link.Hash}))
}
return err
}