diff --git a/pkg/api/resolver_mutation_scene.go b/pkg/api/resolver_mutation_scene.go index 9fe3b8674..9b86af534 100644 --- a/pkg/api/resolver_mutation_scene.go +++ b/pkg/api/resolver_mutation_scene.go @@ -60,6 +60,9 @@ func (r *mutationResolver) ScenesUpdate(ctx context.Context, input []*models.Sce func (r *mutationResolver) sceneUpdate(input models.SceneUpdateInput, tx *sqlx.Tx) (*models.Scene, error) { // Populate scene from the input sceneID, _ := strconv.Atoi(input.ID) + + var coverImageData []byte + updatedTime := time.Now() updatedScene := models.ScenePartial{ ID: sceneID, @@ -77,6 +80,14 @@ func (r *mutationResolver) sceneUpdate(input models.SceneUpdateInput, tx *sqlx.T if input.Date != nil { updatedScene.Date = &models.SQLiteDate{String: *input.Date, Valid: true} } + if input.CoverImage != nil && *input.CoverImage != "" { + var err error + _, coverImageData, err = utils.ProcessBase64Image(*input.CoverImage) + if err != nil { + return nil, err + } + updatedScene.Cover = &coverImageData + } if input.Rating != nil { updatedScene.Rating = &sql.NullInt64{Int64: int64(*input.Rating), Valid: true} @@ -151,18 +162,13 @@ func (r *mutationResolver) sceneUpdate(input models.SceneUpdateInput, tx *sqlx.T } // only update the cover image if provided and everything else was successful - if input.CoverImage != nil && *input.CoverImage != "" { - _, imageData, err := utils.ProcessBase64Image(*input.CoverImage) - if err != nil { - return nil, err - } - + if coverImageData != nil { scene, err := qb.Find(sceneID) if err != nil { return nil, err } - err = manager.SetSceneScreenshot(scene.Checksum, imageData) + err = manager.SetSceneScreenshot(scene.Checksum, coverImageData) if err != nil { return nil, err } diff --git a/pkg/database/database.go b/pkg/database/database.go index 51ff3a21e..83c5c0fdc 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -17,7 +17,7 @@ import ( ) var DB *sqlx.DB -var appSchemaVersion uint = 1 +var appSchemaVersion uint = 2 const sqlite3Driver = "sqlite3_regexp" diff --git a/pkg/database/migrations/2_cover_image.up.sql b/pkg/database/migrations/2_cover_image.up.sql new file mode 100644 index 000000000..590ae7198 --- /dev/null +++ b/pkg/database/migrations/2_cover_image.up.sql @@ -0,0 +1 @@ +ALTER TABLE `scenes` ADD COLUMN `cover` blob; \ No newline at end of file diff --git a/pkg/manager/jsonschema/scene.go b/pkg/manager/jsonschema/scene.go index c13ad2693..0f4098571 100644 --- a/pkg/manager/jsonschema/scene.go +++ b/pkg/manager/jsonschema/scene.go @@ -39,6 +39,7 @@ type Scene struct { Tags []string `json:"tags,omitempty"` Markers []SceneMarker `json:"markers,omitempty"` File *SceneFile `json:"file,omitempty"` + Cover string `json:"cover,omitempty"` CreatedAt models.JSONTime `json:"created_at,omitempty"` UpdatedAt models.JSONTime `json:"updated_at,omitempty"` } diff --git a/pkg/manager/task_export.go b/pkg/manager/task_export.go index f0bfcf50f..f9c883b26 100644 --- a/pkg/manager/task_export.go +++ b/pkg/manager/task_export.go @@ -161,6 +161,10 @@ func (t *ExportTask) ExportScenes(ctx context.Context) { newSceneJSON.File.Bitrate = int(scene.Bitrate.Int64) } + if len(scene.Cover) > 0 { + newSceneJSON.Cover = utils.GetBase64StringFromData(scene.Cover) + } + sceneJSON, err := instance.JSON.getScene(scene.Checksum) if err != nil { logger.Debugf("[scenes] error reading scene json: %s", err.Error()) diff --git a/pkg/manager/task_import.go b/pkg/manager/task_import.go index c353db432..0df2891f6 100644 --- a/pkg/manager/task_import.go +++ b/pkg/manager/task_import.go @@ -382,6 +382,19 @@ func (t *ImportTask) ImportScenes(ctx context.Context) { continue } + // Process the base 64 encoded cover image string + if sceneJSON.Cover != "" { + _, coverImageData, err := utils.ProcessBase64Image(sceneJSON.Cover) + if err != nil { + logger.Warnf("[scenes] <%s> invalid cover image: %s", mappingJSON.Checksum, err.Error()) + } + if len(coverImageData) > 0 { + if err = SetSceneScreenshot(mappingJSON.Checksum, coverImageData); err != nil { + logger.Warnf("[scenes] <%s> failed to create cover image: %s", mappingJSON.Checksum, err.Error()) + } + } + } + // Populate scene fields if sceneJSON != nil { if sceneJSON.Title != "" { diff --git a/pkg/models/model_scene.go b/pkg/models/model_scene.go index dac6a086b..e025a95cc 100644 --- a/pkg/models/model_scene.go +++ b/pkg/models/model_scene.go @@ -9,6 +9,7 @@ type Scene struct { ID int `db:"id" json:"id"` Checksum string `db:"checksum" json:"checksum"` Path string `db:"path" json:"path"` + Cover []byte `db:"cover" json:"cover"` Title sql.NullString `db:"title" json:"title"` Details sql.NullString `db:"details" json:"details"` URL sql.NullString `db:"url" json:"url"` @@ -31,6 +32,7 @@ type ScenePartial struct { ID int `db:"id" json:"id"` Checksum *string `db:"checksum" json:"checksum"` Path *string `db:"path" json:"path"` + Cover *[]byte `db:"cover" json:"cover"` Title *sql.NullString `db:"title" json:"title"` Details *sql.NullString `db:"details" json:"details"` URL *sql.NullString `db:"url" json:"url"`