feat: recursively remove shares when folder/file is deleted

pull/3896/head
Stavros Tsioulis 2025-05-10 20:15:06 +03:00 committed by Henrique Dias
parent 47b3e218ad
commit 7398478d29
3 changed files with 54 additions and 1 deletions

View File

@ -65,7 +65,7 @@ func resourceDeleteHandler(fileCache FileCache) handleFunc {
Fs: d.user.Fs,
Path: r.URL.Path,
Modify: d.user.Perm.Modify,
Expand: false,
Expand: true,
ReadHeader: d.server.TypeDetectionByHeader,
Checker: d,
})
@ -73,6 +73,11 @@ func resourceDeleteHandler(fileCache FileCache) handleFunc {
return errToStatus(err), err
}
// delete potential shares for this path
for _, path := range flatListFilePaths(file, d) {
d.store.Share.DeleteWithPath(path)
}
// delete thumbnails
err = delThumbs(r.Context(), fileCache, file)
if err != nil {
@ -332,6 +337,41 @@ func patchAction(ctx context.Context, action, src, dst string, d *data, fileCach
}
}
func flatListFilePaths(fileInfo *files.FileInfo, d *data) []string {
var paths []string
paths = append(paths, fileInfo.Path)
if (!fileInfo.isDir) {
return paths
}
for _, item := range fileInfo.Items {
// no need to add the directory's path here as it will provide itself in the next recursion
if (item.IsDir) {
file, err := files.NewFileInfo(&files.FileOptions{
Fs: item.Fs,
Path: item.Path + "/", // ensure we add the suffix "/" for a directory
Modify: false,
Expand: true,
ReadHeader: false,
Checker: d,
})
if (err != nil) {
return paths
}
deeperFiles := flatListFilePaths(file, d)
paths = append(paths, deeperFiles...)
} else {
paths = append(paths, item.Path)
}
}
return paths
}
type DiskUsageResponse struct {
Total uint64 `json:"total"`
Used uint64 `json:"used"`

View File

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

View File

@ -75,3 +75,11 @@ func (s shareBackend) Delete(hash string) error {
}
return err
}
func (s shareBackend) DeleteWithPath(path string) error {
err := s.db.Select(q.Eq("Path", path)).Delete(&share.Link{})
if errors.Is(err, storm.ErrNotFound) {
return nil
}
return err
}