fix(nas): refuse deletes while library storage is unreachable; real file removal; stat cache
Three changes from the network-drive audit: 1. Delete-ordering fix. Deleting a media item cascades its annotations, so the delete must never proceed while the item's library storage is unreachable — the DB row died, the file survived as an orphan, and the next scan re-imported it bare (annotations gone forever). resolveMediaStorage stats the library's folders: reachable → delete proceeds and the file is removed by ABSOLUTE path (the old code passed a library-relative path to os.Remove, which never resolved — file deletion has been a silent no-op); every folder unreachable → 409 with 'storage unreachable', row and annotations intact. 2. Latency: resolveLibrary stats every library folder on every SSR render; folder reachability is now cached with a 30s TTL. 3. README: Network Storage section documenting the outage hysteresis (mark → archive → auto-restore), the 90-day retention purge (ARCHIVE_RETENTION_DAYS, 0 = never), refused deletes while storage is down, and watch mode not firing on network mounts. Integration tests: storage-down bulk delete refuses with the row intact; real-file delete removes the file from disk.
This commit is contained in:
1 parent
f42720ac64
commit
17274bb868
4 files changed
+237
-14
No files matched your search
@@ -439,6 +439,20 @@ func (h *MediaHandler) HandleBulkDelete(c *echo.Context) error {
|
||||
continue
|
||||
}
|
||||
|
||||
// Storage-down guard: deleting cascades the item's annotations, so
|
||||
// refuse while the library storage is unreachable — the file would
|
||||
// survive as an orphan and re-import bare on the next scan.
|
||||
storage := h.resolveMediaStorage(c.Request().Context(), uuid.UUID(media.LibraryID.Bytes).String(), media.FilePath)
|
||||
if !storage.Reachable {
|
||||
results = append(results, map[string]interface{}{
|
||||
"media_item_id": mediaIDStr,
|
||||
"status": "error",
|
||||
"error": "storage unreachable — archive the item instead, or retry when the storage is mounted",
|
||||
})
|
||||
failedCount++
|
||||
continue
|
||||
}
|
||||
|
||||
err = h.db.DeleteMediaItem(c.Request().Context(), mediaUUID)
|
||||
if err != nil {
|
||||
results = append(results, map[string]interface{}{
|
||||
@@ -450,11 +464,11 @@ func (h *MediaHandler) HandleBulkDelete(c *echo.Context) error {
|
||||
continue
|
||||
}
|
||||
|
||||
if media.FilePath != "" {
|
||||
if err := os.Remove(media.FilePath); err != nil {
|
||||
if storage.AbsolutePath != "" {
|
||||
if err := os.Remove(storage.AbsolutePath); err != nil {
|
||||
// Log file removal failure but don't fail the request
|
||||
// DB record is already deleted, which is the primary concern
|
||||
log.Printf("Warning: failed to remove file %s: %v", media.FilePath, err)
|
||||
log.Printf("Warning: failed to remove file %s: %v", storage.AbsolutePath, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -649,6 +663,54 @@ func (mh *MediaHandler) visibleLibrarySet(c *echo.Context) (map[string]bool, []p
|
||||
return set, ids, nil
|
||||
}
|
||||
|
||||
// mediaStorageState reports whether the library storage holding a media
|
||||
// item's file is currently reachable, and resolves the file's absolute
|
||||
// path when it can be located.
|
||||
//
|
||||
// Why this exists: deleting a media item cascades its annotations, so the
|
||||
// delete must never proceed while the storage is unreachable — otherwise
|
||||
// a NAS outage turns an admin cleanup into permanent annotation loss
|
||||
// (the DB row dies, the orphaned file re-imports bare on the next scan).
|
||||
type mediaStorageState struct {
|
||||
// Absolute path of the file when located on reachable storage;
|
||||
// empty when the file is missing or reachability is undeterminable.
|
||||
AbsolutePath string
|
||||
// Reachable is false ONLY when the library has folders and every one
|
||||
// of them fails to stat — positive evidence the mount is down. When
|
||||
// no folders are configured reachability is undeterminable and this
|
||||
// is true (delete proceeds; behavior unchanged from before this check).
|
||||
Reachable bool
|
||||
}
|
||||
|
||||
func (mh *MediaHandler) resolveMediaStorage(ctx context.Context, libraryID string, relativePath string) mediaStorageState {
|
||||
libUUID, err := uuid.Parse(libraryID)
|
||||
if err != nil {
|
||||
return mediaStorageState{Reachable: true}
|
||||
}
|
||||
folders, err := mh.libraryService.GetLibraryFolders(ctx, pgtype.UUID{Bytes: libUUID, Valid: true})
|
||||
if err != nil || len(folders) == 0 {
|
||||
return mediaStorageState{Reachable: true}
|
||||
}
|
||||
|
||||
anyFolderUp := false
|
||||
for _, f := range folders {
|
||||
if _, err := os.Stat(f.FolderPath); err != nil {
|
||||
continue // folder root unreachable
|
||||
}
|
||||
anyFolderUp = true
|
||||
candidate := filepath.Join(f.FolderPath, filepath.FromSlash(relativePath))
|
||||
if _, err := os.Stat(candidate); err == nil {
|
||||
return mediaStorageState{AbsolutePath: candidate, Reachable: true}
|
||||
}
|
||||
}
|
||||
if !anyFolderUp {
|
||||
return mediaStorageState{Reachable: false}
|
||||
}
|
||||
// Storage is up but the file is genuinely gone: a stale row is safe
|
||||
// to delete.
|
||||
return mediaStorageState{Reachable: true}
|
||||
}
|
||||
|
||||
// ListMediaItems handles GET /api/media-items
|
||||
func (mh *MediaHandler) ListMediaItems(c *echo.Context) error {
|
||||
libraryID := c.QueryParam("library_id")
|
||||
@@ -1599,11 +1661,35 @@ func (mh *MediaHandler) DeleteMediaItem(c *echo.Context) error {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid media item id"})
|
||||
}
|
||||
|
||||
media, err := mh.db.GetMediaItem(c.Request().Context(), pgtype.UUID{Bytes: mediaUUID, Valid: true})
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return c.JSON(http.StatusNotFound, map[string]string{"error": "media item not found"})
|
||||
}
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
}
|
||||
|
||||
// Storage-down guard (see HandleBulkDelete): refuse while the library
|
||||
// storage is unreachable so the cascade can't destroy annotations for
|
||||
// a file that will re-import on the next scan.
|
||||
storage := mh.resolveMediaStorage(c.Request().Context(), uuid.UUID(media.LibraryID.Bytes).String(), media.FilePath)
|
||||
if !storage.Reachable {
|
||||
return c.JSON(http.StatusConflict, map[string]string{
|
||||
"error": "storage unreachable — archive the item instead, or retry when the storage is mounted",
|
||||
})
|
||||
}
|
||||
|
||||
err = mh.db.DeleteMediaItem(c.Request().Context(), pgtype.UUID{Bytes: mediaUUID, Valid: true})
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
}
|
||||
|
||||
if storage.AbsolutePath != "" {
|
||||
if err := os.Remove(storage.AbsolutePath); err != nil {
|
||||
log.Printf("Warning: failed to remove file %s: %v", storage.AbsolutePath, err)
|
||||
}
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusNoContent)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user