Move image blobs into separate tables (#618)

* Scene cover fallback to database
* Fix panic if studio not found
* Fix movie studio not being imported/exported
This commit is contained in:
WithoutPants
2020-06-23 09:19:19 +10:00
committed by GitHub
parent f8048dc27c
commit 7a74658a73
31 changed files with 1456 additions and 131 deletions

View File

@@ -85,7 +85,6 @@ func (t *ImportTask) ImportPerformers(ctx context.Context) {
// Populate a new performer from the input
newPerformer := models.Performer{
Image: imageData,
Checksum: checksum,
Favorite: sql.NullBool{Bool: performerJSON.Favorite, Valid: true},
CreatedAt: models.SQLiteTimestamp{Timestamp: t.getTimeFromJSONTime(performerJSON.CreatedAt)},
@@ -141,12 +140,21 @@ func (t *ImportTask) ImportPerformers(ctx context.Context) {
newPerformer.Instagram = sql.NullString{String: performerJSON.Instagram, Valid: true}
}
_, err = qb.Create(newPerformer, tx)
createdPerformer, err := qb.Create(newPerformer, tx)
if err != nil {
_ = tx.Rollback()
logger.Errorf("[performers] <%s> failed to create: %s", mappingJSON.Checksum, err.Error())
return
}
// Add the performer image if set
if len(imageData) > 0 {
if err := qb.UpdatePerformerImage(createdPerformer.ID, imageData, tx); err != nil {
_ = tx.Rollback()
logger.Errorf("[performers] <%s> error setting performer image: %s", mappingJSON.Checksum, err.Error())
return
}
}
}
logger.Info("[performers] importing")
@@ -217,7 +225,6 @@ func (t *ImportTask) ImportStudio(studioJSON *jsonschema.Studio, pendingParent m
// Populate a new studio from the input
newStudio := models.Studio{
Image: imageData,
Checksum: checksum,
Name: sql.NullString{String: studioJSON.Name, Valid: true},
URL: sql.NullString{String: studioJSON.URL, Valid: true},
@@ -251,11 +258,17 @@ func (t *ImportTask) ImportStudio(studioJSON *jsonschema.Studio, pendingParent m
}
}
_, err = qb.Create(newStudio, tx)
createdStudio, err := qb.Create(newStudio, tx)
if err != nil {
return err
}
if len(imageData) > 0 {
if err := qb.UpdateStudioImage(createdStudio.ID, imageData, tx); err != nil {
return fmt.Errorf("error setting studio image: %s", err.Error())
}
}
// now create the studios pending this studios creation
s := pendingParent[studioJSON.Name]
for _, childStudioJSON := range s {
@@ -307,17 +320,15 @@ func (t *ImportTask) ImportMovies(ctx context.Context) {
// Populate a new movie from the input
newMovie := models.Movie{
FrontImage: frontimageData,
BackImage: backimageData,
Checksum: checksum,
Name: sql.NullString{String: movieJSON.Name, Valid: true},
Aliases: sql.NullString{String: movieJSON.Aliases, Valid: true},
Date: models.SQLiteDate{String: movieJSON.Date, Valid: true},
Director: sql.NullString{String: movieJSON.Director, Valid: true},
Synopsis: sql.NullString{String: movieJSON.Synopsis, Valid: true},
URL: sql.NullString{String: movieJSON.URL, Valid: true},
CreatedAt: models.SQLiteTimestamp{Timestamp: t.getTimeFromJSONTime(movieJSON.CreatedAt)},
UpdatedAt: models.SQLiteTimestamp{Timestamp: t.getTimeFromJSONTime(movieJSON.UpdatedAt)},
Checksum: checksum,
Name: sql.NullString{String: movieJSON.Name, Valid: true},
Aliases: sql.NullString{String: movieJSON.Aliases, Valid: true},
Date: models.SQLiteDate{String: movieJSON.Date, Valid: true},
Director: sql.NullString{String: movieJSON.Director, Valid: true},
Synopsis: sql.NullString{String: movieJSON.Synopsis, Valid: true},
URL: sql.NullString{String: movieJSON.URL, Valid: true},
CreatedAt: models.SQLiteTimestamp{Timestamp: t.getTimeFromJSONTime(movieJSON.CreatedAt)},
UpdatedAt: models.SQLiteTimestamp{Timestamp: t.getTimeFromJSONTime(movieJSON.UpdatedAt)},
}
if movieJSON.Rating != 0 {
@@ -327,12 +338,34 @@ func (t *ImportTask) ImportMovies(ctx context.Context) {
newMovie.Duration = sql.NullInt64{Int64: int64(movieJSON.Duration), Valid: true}
}
_, err = qb.Create(newMovie, tx)
// Populate the studio ID
if movieJSON.Studio != "" {
sqb := models.NewStudioQueryBuilder()
studio, err := sqb.FindByName(movieJSON.Studio, tx, false)
if err != nil {
logger.Warnf("[movies] error getting studio <%s>: %s", movieJSON.Studio, err.Error())
} else if studio == nil {
logger.Warnf("[movies] studio <%s> does not exist", movieJSON.Studio)
} else {
newMovie.StudioID = sql.NullInt64{Int64: int64(studio.ID), Valid: true}
}
}
createdMovie, err := qb.Create(newMovie, tx)
if err != nil {
_ = tx.Rollback()
logger.Errorf("[movies] <%s> failed to create: %s", mappingJSON.Checksum, err.Error())
return
}
// Add the performer image if set
if len(frontimageData) > 0 {
if err := qb.UpdateMovieImages(createdMovie.ID, frontimageData, backimageData, tx); err != nil {
_ = tx.Rollback()
logger.Errorf("[movies] <%s> error setting movie images: %s", mappingJSON.Checksum, err.Error())
return
}
}
}
logger.Info("[movies] importing")
@@ -521,17 +554,18 @@ func (t *ImportTask) ImportScenes(ctx context.Context) {
}
// Process the base 64 encoded cover image string
var coverImageData []byte
if sceneJSON.Cover != "" {
_, coverImageData, err := utils.ProcessBase64Image(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())
} else {
newScene.Cover = coverImageData
}
// write the cover image data after creating the scene
}
}
@@ -597,7 +631,9 @@ func (t *ImportTask) ImportScenes(ctx context.Context) {
sqb := models.NewStudioQueryBuilder()
studio, err := sqb.FindByName(sceneJSON.Studio, tx, false)
if err != nil {
logger.Warnf("[scenes] studio <%s> does not exist: %s", sceneJSON.Studio, err.Error())
logger.Warnf("[scenes] error getting studio <%s>: %s", sceneJSON.Studio, err.Error())
} else if studio == nil {
logger.Warnf("[scenes] studio <%s> does not exist", sceneJSON.Studio)
} else {
newScene.StudioID = sql.NullInt64{Int64: int64(studio.ID), Valid: true}
}
@@ -616,6 +652,15 @@ func (t *ImportTask) ImportScenes(ctx context.Context) {
return
}
// Add the scene cover if set
if len(coverImageData) > 0 {
if err := qb.UpdateSceneCover(scene.ID, coverImageData, tx); err != nil {
_ = tx.Rollback()
logger.Errorf("[scenes] <%s> error setting scene cover: %s", mappingJSON.Checksum, err.Error())
return
}
}
// Relate the scene to the gallery
if sceneJSON.Gallery != "" {
gqb := models.NewGalleryQueryBuilder()