feat(metadata): per-field user overrides that survive library rescans
Previously both the metadata editor (PUT /api/media-items/:id) and the
scanner (library scans, force rescans, per-book rescans) wrote through
the same unconditional UPDATE media_items query, so any rescan wiped
user-written descriptions, tags, and uploaded covers. Custom and scanned
values were indistinguishable, and custom cover uploads even wrote to
the same {file}.cover.jpg sidecar path the scanner generates, so each
side silently clobbered the other.
Introduce metadata_overrides, a TEXT[] column on media_items listing the
column names the user has customized:
- Saving metadata records overrides per field by diffing the submitted
values against the stored row (an untouched save records nothing);
overrides accumulate until an explicit reset. Cover upload/removal
always marks cover_image_path. Bulk updates mark each applied field.
- The scanner merges: updateMediaItem() now takes the existing row and
restores every overridden column (including derived *_search arrays)
before writing, and preserves the override set itself.
- Uploaded covers move to a dedicated {file}.custom_cover.{jpg|png|webp}
sidecar so the scanner can never overwrite a user cover on disk.
- RescanMediaItem gains resetOverrides: POST /api/media-items/:id/rescan?
reset_overrides=true clears the set first, returning the item to pure
scanned defaults.
Shared detection/restore helpers live in internal/utils
(metadata_overrides.go) with unit tests covering detection, accumulation,
unset-form equality, and restore-with-derived-fields. Schema change is
an idempotent ADD COLUMN IF NOT EXISTS applied on startup. Also includes
incidental gofmt of NewMediaScanner literals in media_scanner.go.
This commit is contained in:
+90
-55
@@ -63,41 +63,41 @@ type CreateMediaItemRequest struct {
|
||||
|
||||
// UpdateMediaItemRequest represents the request for updating a media item
|
||||
type UpdateMediaItemRequest struct {
|
||||
Title string `form:"title" json:"title" validate:"required,min=1,max=500"`
|
||||
Author string `form:"author" json:"author"`
|
||||
ISBN string `form:"isbn" json:"isbn"`
|
||||
Description string `form:"description" json:"description"`
|
||||
CoverImagePath string `form:"cover_image_path" json:"cover_image_path"`
|
||||
CoverAction string `form:"cover_action" json:"cover_action"`
|
||||
Series string `form:"series" json:"series"`
|
||||
SeriesNumber int32 `form:"series_number" json:"series_number"`
|
||||
Tags []string `form:"tags" json:"tags"`
|
||||
ASIN string `form:"asin" json:"asin"`
|
||||
DatePublished string `form:"date_published" json:"date_published"`
|
||||
Publisher string `form:"publisher" json:"publisher"`
|
||||
Contributors []string `form:"contributors" json:"contributors"`
|
||||
Language string `form:"language" json:"language"`
|
||||
Edition string `form:"edition" json:"edition"`
|
||||
PageCount int32 `form:"page_count" json:"page_count"`
|
||||
Genre string `form:"genre" json:"genre"`
|
||||
CopyrightYear int32 `form:"copyright_year" json:"copyright_year"`
|
||||
GoodreadsID string `form:"goodreads_id" json:"goodreads_id"`
|
||||
OpenlibraryID string `form:"openlibrary_id" json:"openlibrary_id"`
|
||||
GoogleBooksID string `form:"google_books_id" json:"google_books_id"`
|
||||
MangaType string `form:"manga_type" json:"manga_type"`
|
||||
ReadingDirection string `form:"reading_direction" json:"reading_direction"`
|
||||
SeriesCount int32 `form:"series_count" json:"series_count"`
|
||||
Volume int32 `form:"volume" json:"volume"`
|
||||
Imprint string `form:"imprint" json:"imprint"`
|
||||
AgeRating string `form:"age_rating" json:"age_rating"`
|
||||
WebURL string `form:"web_url" json:"web_url"`
|
||||
MetadataNotes string `form:"metadata_notes" json:"metadata_notes"`
|
||||
CommunityRating float64 `form:"community_rating" json:"community_rating"`
|
||||
StoryArc string `form:"story_arc" json:"story_arc"`
|
||||
IsBlackAndWhite bool `form:"is_black_and_white" json:"is_black_and_white"`
|
||||
AlternateInfo string `form:"alternate_info" json:"alternate_info"`
|
||||
ScanInformation string `form:"scan_information" json:"scan_information"`
|
||||
Summary string `form:"summary" json:"summary"`
|
||||
Title string `form:"title" json:"title" validate:"required,min=1,max=500"`
|
||||
Author string `form:"author" json:"author"`
|
||||
ISBN string `form:"isbn" json:"isbn"`
|
||||
Description string `form:"description" json:"description"`
|
||||
CoverImagePath string `form:"cover_image_path" json:"cover_image_path"`
|
||||
CoverAction string `form:"cover_action" json:"cover_action"`
|
||||
Series string `form:"series" json:"series"`
|
||||
SeriesNumber int32 `form:"series_number" json:"series_number"`
|
||||
Tags []string `form:"tags" json:"tags"`
|
||||
ASIN string `form:"asin" json:"asin"`
|
||||
DatePublished string `form:"date_published" json:"date_published"`
|
||||
Publisher string `form:"publisher" json:"publisher"`
|
||||
Contributors []string `form:"contributors" json:"contributors"`
|
||||
Language string `form:"language" json:"language"`
|
||||
Edition string `form:"edition" json:"edition"`
|
||||
PageCount int32 `form:"page_count" json:"page_count"`
|
||||
Genre string `form:"genre" json:"genre"`
|
||||
CopyrightYear int32 `form:"copyright_year" json:"copyright_year"`
|
||||
GoodreadsID string `form:"goodreads_id" json:"goodreads_id"`
|
||||
OpenlibraryID string `form:"openlibrary_id" json:"openlibrary_id"`
|
||||
GoogleBooksID string `form:"google_books_id" json:"google_books_id"`
|
||||
MangaType string `form:"manga_type" json:"manga_type"`
|
||||
ReadingDirection string `form:"reading_direction" json:"reading_direction"`
|
||||
SeriesCount int32 `form:"series_count" json:"series_count"`
|
||||
Volume int32 `form:"volume" json:"volume"`
|
||||
Imprint string `form:"imprint" json:"imprint"`
|
||||
AgeRating string `form:"age_rating" json:"age_rating"`
|
||||
WebURL string `form:"web_url" json:"web_url"`
|
||||
MetadataNotes string `form:"metadata_notes" json:"metadata_notes"`
|
||||
CommunityRating float64 `form:"community_rating" json:"community_rating"`
|
||||
StoryArc string `form:"story_arc" json:"story_arc"`
|
||||
IsBlackAndWhite bool `form:"is_black_and_white" json:"is_black_and_white"`
|
||||
AlternateInfo string `form:"alternate_info" json:"alternate_info"`
|
||||
ScanInformation string `form:"scan_information" json:"scan_information"`
|
||||
Summary string `form:"summary" json:"summary"`
|
||||
}
|
||||
|
||||
// CreateMediaNoteRequest represents the request for creating a media note
|
||||
@@ -572,24 +572,33 @@ func (h *MediaHandler) HandleBulkUpdate(c *echo.Context) error {
|
||||
Summary: existingMedia.Summary,
|
||||
}
|
||||
|
||||
// Bulk edits are user customizations: keep existing overrides and mark
|
||||
// each applied update as overridden so scans preserve it.
|
||||
overrides := existingMedia.MetadataOverrides
|
||||
if update.Updates.Title != nil {
|
||||
updateParams.Title = *update.Updates.Title
|
||||
overrides = utils.MergeOverrides(overrides, utils.OverrideTitle)
|
||||
}
|
||||
if update.Updates.Author != nil {
|
||||
updateParams.Author = pgtype.Text{String: *update.Updates.Author, Valid: true}
|
||||
overrides = utils.MergeOverrides(overrides, utils.OverrideAuthor)
|
||||
}
|
||||
if update.Updates.Genre != nil {
|
||||
updateParams.Genre = pgtype.Text{String: *update.Updates.Genre, Valid: true}
|
||||
overrides = utils.MergeOverrides(overrides, utils.OverrideGenre)
|
||||
}
|
||||
if update.Updates.Language != nil {
|
||||
updateParams.Language = pgtype.Text{String: *update.Updates.Language, Valid: true}
|
||||
overrides = utils.MergeOverrides(overrides, utils.OverrideLanguage)
|
||||
}
|
||||
if len(update.Updates.Tags) > 0 {
|
||||
normalizedTags := utils.NormalizeTags(update.Updates.Tags)
|
||||
updateParams.Tags = normalizedTags
|
||||
tagsSearch := utils.NormalizeTagsSearch(update.Updates.Tags)
|
||||
updateParams.TagsSearch = tagsSearch
|
||||
overrides = utils.MergeOverrides(overrides, utils.OverrideTags)
|
||||
}
|
||||
updateParams.MetadataOverrides = overrides
|
||||
|
||||
_, err = h.db.UpdateMediaItem(c.Request().Context(), updateParams)
|
||||
if err != nil {
|
||||
@@ -925,22 +934,22 @@ func (mh *MediaHandler) GetMediaReadingProgress(c *echo.Context) error {
|
||||
}
|
||||
|
||||
resp := map[string]interface{}{
|
||||
"id": progress.ID,
|
||||
"media_item_id": progress.MediaItemID,
|
||||
"user_id": progress.UserID,
|
||||
"current_page": progress.CurrentPage,
|
||||
"total_pages": progress.TotalPages,
|
||||
"last_read_at": progress.LastReadAt,
|
||||
"percentage": progress.Percentage,
|
||||
"character_offset": progress.CharacterOffset,
|
||||
"epubcfi": progress.Epubcfi,
|
||||
"chapter": progress.Chapter,
|
||||
"chapter_progress": progress.ChapterProgress,
|
||||
"format_group": progress.FormatGroup,
|
||||
"total_characters": progress.TotalCharacters,
|
||||
"chapter_count": progress.ChapterCount,
|
||||
"last_sync_device": progress.LastSyncDevice,
|
||||
"last_sync_source": progress.LastSyncSource,
|
||||
"id": progress.ID,
|
||||
"media_item_id": progress.MediaItemID,
|
||||
"user_id": progress.UserID,
|
||||
"current_page": progress.CurrentPage,
|
||||
"total_pages": progress.TotalPages,
|
||||
"last_read_at": progress.LastReadAt,
|
||||
"percentage": progress.Percentage,
|
||||
"character_offset": progress.CharacterOffset,
|
||||
"epubcfi": progress.Epubcfi,
|
||||
"chapter": progress.Chapter,
|
||||
"chapter_progress": progress.ChapterProgress,
|
||||
"format_group": progress.FormatGroup,
|
||||
"total_characters": progress.TotalCharacters,
|
||||
"chapter_count": progress.ChapterCount,
|
||||
"last_sync_device": progress.LastSyncDevice,
|
||||
"last_sync_source": progress.LastSyncSource,
|
||||
"last_sync_timestamp": progress.LastSyncTimestamp,
|
||||
}
|
||||
|
||||
@@ -1240,6 +1249,7 @@ func (mh *MediaHandler) UpdateMediaItem(c *echo.Context) error {
|
||||
}
|
||||
|
||||
coverPath := existing.CoverImagePath.String
|
||||
coverUploaded := false
|
||||
|
||||
if req.CoverAction == "remove" {
|
||||
coverPath = ""
|
||||
@@ -1251,6 +1261,7 @@ func (mh *MediaHandler) UpdateMediaItem(c *echo.Context) error {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to save cover image"})
|
||||
}
|
||||
coverPath = savedPath
|
||||
coverUploaded = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1259,7 +1270,7 @@ func (mh *MediaHandler) UpdateMediaItem(c *echo.Context) error {
|
||||
alternateInfoBytes = []byte(req.AlternateInfo)
|
||||
}
|
||||
|
||||
item, err := mh.db.UpdateMediaItem(c.Request().Context(), database.UpdateMediaItemParams{
|
||||
params := database.UpdateMediaItemParams{
|
||||
ID: pgtype.UUID{Bytes: mediaUUID, Valid: true},
|
||||
Title: req.Title,
|
||||
Author: pgtype.Text{String: req.Author, Valid: req.Author != ""},
|
||||
@@ -1297,7 +1308,17 @@ func (mh *MediaHandler) UpdateMediaItem(c *echo.Context) error {
|
||||
AlternateInfo: alternateInfoBytes,
|
||||
ScanInformation: pgtype.Text{String: req.ScanInformation, Valid: req.ScanInformation != ""},
|
||||
Summary: pgtype.Text{String: req.Summary, Valid: req.Summary != ""},
|
||||
})
|
||||
}
|
||||
|
||||
// Fields the user actually changed become overrides so future scans keep
|
||||
// the custom values. An explicit cover upload/removal always overrides.
|
||||
overrides := utils.DetectMetadataOverrides(params, existing)
|
||||
if req.CoverAction == "remove" || coverUploaded {
|
||||
overrides = utils.MergeOverrides(overrides, utils.OverrideCoverImagePath)
|
||||
}
|
||||
params.MetadataOverrides = overrides
|
||||
|
||||
item, err := mh.db.UpdateMediaItem(c.Request().Context(), params)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
}
|
||||
@@ -1333,7 +1354,11 @@ func (mh *MediaHandler) RescanMediaItem(c *echo.Context) error {
|
||||
scanner := services.NewMediaScanner(mh.db)
|
||||
defer scanner.Close()
|
||||
|
||||
if err := scanner.RescanMediaItem(c.Request().Context(), pgtype.UUID{Bytes: mediaUUID, Valid: true}); err != nil {
|
||||
// reset_overrides=true discards user customizations first, returning the
|
||||
// item to pure scanned defaults (the "Reset to Scanned" action).
|
||||
resetOverrides := c.QueryParam("reset_overrides") == "true"
|
||||
|
||||
if err := scanner.RescanMediaItem(c.Request().Context(), pgtype.UUID{Bytes: mediaUUID, Valid: true}, resetOverrides); err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
}
|
||||
|
||||
@@ -2224,7 +2249,17 @@ func (mh *MediaHandler) saveCoverImage(c echo.Context, mediaUUID uuid.UUID, file
|
||||
return "", fmt.Errorf("media item has no file path")
|
||||
}
|
||||
|
||||
coverRelPath := relativeFilePath + ".cover.jpg"
|
||||
// Custom covers live at a dedicated sidecar path (distinct from the
|
||||
// scanner-generated {file}.cover.jpg) so scans can never overwrite a
|
||||
// user-uploaded cover and the metadata_overrides set can protect it.
|
||||
ext := ".jpg"
|
||||
switch contentType {
|
||||
case "image/png":
|
||||
ext = ".png"
|
||||
case "image/webp":
|
||||
ext = ".webp"
|
||||
}
|
||||
coverRelPath := relativeFilePath + ".custom_cover" + ext
|
||||
|
||||
coverFullPath, err := mh.libraryService.ResolveMediaPath(c.Request().Context(), mediaItem.LibraryID, coverRelPath)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user