Merge pull request #109 from WithoutPants/optional_performer_image

Generate performer checksum from name instead of image. Use default performer image
This commit is contained in:
Leopere
2019-10-24 15:35:26 -04:00
committed by GitHub
51 changed files with 75 additions and 16 deletions

View File

@@ -15,7 +15,7 @@ mutation PerformerCreate(
$twitter: String, $twitter: String,
$instagram: String, $instagram: String,
$favorite: Boolean, $favorite: Boolean,
$image: String!) { $image: String) {
performerCreate(input: { performerCreate(input: {
name: $name, name: $name,

View File

@@ -1,7 +1,7 @@
mutation StudioCreate( mutation StudioCreate(
$name: String!, $name: String!,
$url: String, $url: String,
$image: String!) { $image: String) {
studioCreate(input: { name: $name, url: $url, image: $image }) { studioCreate(input: { name: $name, url: $url, image: $image }) {
...StudioData ...StudioData

View File

@@ -41,7 +41,7 @@ input PerformerCreateInput {
instagram: String instagram: String
favorite: Boolean favorite: Boolean
"""This should be base64 encoded""" """This should be base64 encoded"""
image: String! image: String
} }
input PerformerUpdateInput { input PerformerUpdateInput {

View File

@@ -12,7 +12,7 @@ input StudioCreateInput {
name: String! name: String!
url: String url: String
"""This should be base64 encoded""" """This should be base64 encoded"""
image: String! image: String
} }
input StudioUpdateInput { input StudioUpdateInput {

19
pkg/api/images.go Normal file
View File

@@ -0,0 +1,19 @@
package api
import (
"math/rand"
"github.com/gobuffalo/packr/v2"
)
var performerBox *packr.Box
func initialiseImages() {
performerBox = packr.New("Performer Box", "../../static/performer")
}
func getRandomPerformerImage() ([]byte, error) {
imageFiles := performerBox.List()
index := rand.Intn(len(imageFiles))
return performerBox.Find(imageFiles[index])
}

View File

@@ -3,16 +3,27 @@ package api
import ( import (
"context" "context"
"database/sql" "database/sql"
"strconv"
"time"
"github.com/stashapp/stash/pkg/database" "github.com/stashapp/stash/pkg/database"
"github.com/stashapp/stash/pkg/models" "github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/utils" "github.com/stashapp/stash/pkg/utils"
"strconv"
"time"
) )
func (r *mutationResolver) PerformerCreate(ctx context.Context, input models.PerformerCreateInput) (*models.Performer, error) { func (r *mutationResolver) PerformerCreate(ctx context.Context, input models.PerformerCreateInput) (*models.Performer, error) {
// Process the base 64 encoded image string // generate checksum from performer name rather than image
checksum, imageData, err := utils.ProcessBase64Image(input.Image) checksum := utils.MD5FromString(*input.Name)
var imageData []byte
var err error
if input.Image == nil {
imageData, err = getRandomPerformerImage()
} else {
_, imageData, err = utils.ProcessBase64Image(*input.Image)
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -101,15 +112,18 @@ func (r *mutationResolver) PerformerUpdate(ctx context.Context, input models.Per
UpdatedAt: models.SQLiteTimestamp{Timestamp: time.Now()}, UpdatedAt: models.SQLiteTimestamp{Timestamp: time.Now()},
} }
if input.Image != nil { if input.Image != nil {
checksum, imageData, err := utils.ProcessBase64Image(*input.Image) _, imageData, err := utils.ProcessBase64Image(*input.Image)
if err != nil { if err != nil {
return nil, err return nil, err
} }
updatedPerformer.Image = imageData updatedPerformer.Image = imageData
updatedPerformer.Checksum = checksum
} }
if input.Name != nil { if input.Name != nil {
// generate checksum from performer name rather than image
checksum := utils.MD5FromString(*input.Name)
updatedPerformer.Name = sql.NullString{String: *input.Name, Valid: true} updatedPerformer.Name = sql.NullString{String: *input.Name, Valid: true}
updatedPerformer.Checksum = checksum
} }
if input.URL != nil { if input.URL != nil {
updatedPerformer.URL = sql.NullString{String: *input.URL, Valid: true} updatedPerformer.URL = sql.NullString{String: *input.URL, Valid: true}
@@ -188,4 +202,3 @@ func (r *mutationResolver) PerformerDestroy(ctx context.Context, input models.Pe
} }
return true, nil return true, nil
} }

View File

@@ -12,8 +12,18 @@ import (
) )
func (r *mutationResolver) StudioCreate(ctx context.Context, input models.StudioCreateInput) (*models.Studio, error) { func (r *mutationResolver) StudioCreate(ctx context.Context, input models.StudioCreateInput) (*models.Studio, error) {
// generate checksum from studio name rather than image
checksum := utils.MD5FromString(input.Name)
var imageData []byte
var err error
if input.Image == nil {
input.Image = &models.DefaultStudioImage
}
// Process the base 64 encoded image string // Process the base 64 encoded image string
checksum, imageData, err := utils.ProcessBase64Image(input.Image) _, imageData, err = utils.ProcessBase64Image(*input.Image)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -56,15 +66,17 @@ func (r *mutationResolver) StudioUpdate(ctx context.Context, input models.Studio
UpdatedAt: models.SQLiteTimestamp{Timestamp: time.Now()}, UpdatedAt: models.SQLiteTimestamp{Timestamp: time.Now()},
} }
if input.Image != nil { if input.Image != nil {
checksum, imageData, err := utils.ProcessBase64Image(*input.Image) _, imageData, err := utils.ProcessBase64Image(*input.Image)
if err != nil { if err != nil {
return nil, err return nil, err
} }
updatedStudio.Image = imageData updatedStudio.Image = imageData
updatedStudio.Checksum = checksum
} }
if input.Name != nil { if input.Name != nil {
// generate checksum from studio name rather than image
checksum := utils.MD5FromString(*input.Name)
updatedStudio.Name = sql.NullString{String: *input.Name, Valid: true} updatedStudio.Name = sql.NullString{String: *input.Name, Valid: true}
updatedStudio.Checksum = checksum
} }
if input.URL != nil { if input.URL != nil {
updatedStudio.URL = sql.NullString{String: *input.URL, Valid: true} updatedStudio.URL = sql.NullString{String: *input.URL, Valid: true}

View File

@@ -66,6 +66,8 @@ func Start() {
//legacyUiBox = packr.New("UI Box", "../../ui/v1/dist/stash-frontend") //legacyUiBox = packr.New("UI Box", "../../ui/v1/dist/stash-frontend")
setupUIBox = packr.New("Setup UI Box", "../../ui/setup") setupUIBox = packr.New("Setup UI Box", "../../ui/setup")
initialiseImages()
r := chi.NewRouter() r := chi.NewRouter()
r.Use(authenticateHandler()) r.Use(authenticateHandler())

View File

@@ -64,8 +64,11 @@ func (t *ImportTask) ImportPerformers(ctx context.Context) {
logger.Progressf("[performers] %d of %d", index, len(t.Mappings.Performers)) logger.Progressf("[performers] %d of %d", index, len(t.Mappings.Performers))
// generate checksum from performer name rather than image
checksum := utils.MD5FromString(performerJSON.Name)
// Process the base 64 encoded image string // Process the base 64 encoded image string
checksum, imageData, err := utils.ProcessBase64Image(performerJSON.Image) _, imageData, err := utils.ProcessBase64Image(performerJSON.Image)
if err != nil { if err != nil {
_ = tx.Rollback() _ = tx.Rollback()
logger.Errorf("[performers] <%s> invalid image: %s", mappingJSON.Checksum, err.Error()) logger.Errorf("[performers] <%s> invalid image: %s", mappingJSON.Checksum, err.Error())
@@ -159,8 +162,11 @@ func (t *ImportTask) ImportStudios(ctx context.Context) {
logger.Progressf("[studios] %d of %d", index, len(t.Mappings.Studios)) logger.Progressf("[studios] %d of %d", index, len(t.Mappings.Studios))
// generate checksum from studio name rather than image
checksum := utils.MD5FromString(studioJSON.Name)
// Process the base 64 encoded image string // Process the base 64 encoded image string
checksum, imageData, err := utils.ProcessBase64Image(studioJSON.Image) _, imageData, err := utils.ProcessBase64Image(studioJSON.Image)
if err != nil { if err != nil {
_ = tx.Rollback() _ = tx.Rollback()
logger.Errorf("[studios] <%s> invalid image: %s", mappingJSON.Checksum, err.Error()) logger.Errorf("[studios] <%s> invalid image: %s", mappingJSON.Checksum, err.Error())

View File

@@ -13,3 +13,5 @@ type Studio struct {
CreatedAt SQLiteTimestamp `db:"created_at" json:"created_at"` CreatedAt SQLiteTimestamp `db:"created_at" json:"created_at"`
UpdatedAt SQLiteTimestamp `db:"updated_at" json:"updated_at"` UpdatedAt SQLiteTimestamp `db:"updated_at" json:"updated_at"`
} }
var DefaultStudioImage string = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4wgVBQsJl1CMZAAAASJJREFUeNrt3N0JwyAYhlEj3cj9R3Cm5rbkqtAP+qrnGaCYHPwJpLlaa++mmLpbAERAgAgIEAEBIiBABERAgAgIEAEBIiBABERAgAgIEAHZuVflj40x4i94zhk9vqsVvEq6AsQqMP1EjORx20OACAgQRRx7T+zzcFBxcjNDfoB4ntQqTm5Awo7MlqywZxcgYQ+RlqywJ3ozJAQCSBiEJSsQA0gYBpDAgAARECACAkRAgAgIEAERECACAmSjUv6eAOSB8m8YIGGzBUjYbAESBgMkbBkDEjZbgITBAClcxiqQvEoatreYIWEBASIgJ4Gkf11ntXH3nS9uxfGWfJ5J9hAgAgJEQAQEiIAAERAgAgJEQAQEiIAAERAgAgJEQAQEiL7qBuc6RKLHxr0CAAAAAElFTkSuQmCC"

View File

@@ -12,6 +12,11 @@ func MD5FromBytes(data []byte) string {
return fmt.Sprintf("%x", result) return fmt.Sprintf("%x", result)
} }
func MD5FromString(str string) string {
data := []byte(str)
return MD5FromBytes(data)
}
func MD5FromFilePath(filePath string) (string, error) { func MD5FromFilePath(filePath string) (string, error) {
f, err := os.Open(filePath) f, err := os.Open(filePath)
if err != nil { if err != nil {

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB