2020-09-20 08:36:02 +00:00
|
|
|
package studio
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2020-09-20 08:36:02 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models/jsonschema"
|
2020-09-20 08:36:02 +00:00
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
type ImporterReaderWriter interface {
|
|
|
|
models.StudioCreatorUpdater
|
|
|
|
FindByName(ctx context.Context, name string, nocase bool) (*models.Studio, error)
|
2022-05-19 07:49:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 08:36:02 +00:00
|
|
|
var ErrParentStudioNotExist = errors.New("parent studio does not exist")
|
|
|
|
|
|
|
|
type Importer struct {
|
2023-09-01 00:39:29 +00:00
|
|
|
ReaderWriter ImporterReaderWriter
|
2020-09-20 08:36:02 +00:00
|
|
|
Input jsonschema.Studio
|
|
|
|
MissingRefBehaviour models.ImportMissingRefEnum
|
|
|
|
|
2023-07-30 23:50:24 +00:00
|
|
|
ID int
|
2020-09-20 08:36:02 +00:00
|
|
|
studio models.Studio
|
|
|
|
imageData []byte
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) PreImport(ctx context.Context) error {
|
2023-07-30 23:50:24 +00:00
|
|
|
i.studio = studioJSONtoStudio(i.Input)
|
2020-09-20 08:36:02 +00:00
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := i.populateParentStudio(ctx); err != nil {
|
2020-09-20 08:36:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if len(i.Input.Image) > 0 {
|
2022-03-17 00:33:59 +00:00
|
|
|
i.imageData, err = utils.ProcessBase64Image(i.Input.Image)
|
2020-09-20 08:36:02 +00:00
|
|
|
if err != nil {
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
return fmt.Errorf("invalid image: %v", err)
|
2020-09-20 08:36:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) populateParentStudio(ctx context.Context) error {
|
2020-09-20 08:36:02 +00:00
|
|
|
if i.Input.ParentStudio != "" {
|
2022-05-19 07:49:32 +00:00
|
|
|
studio, err := i.ReaderWriter.FindByName(ctx, i.Input.ParentStudio, false)
|
2020-09-20 08:36:02 +00:00
|
|
|
if err != nil {
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
return fmt.Errorf("error finding studio by name: %v", err)
|
2020-09-20 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if studio == nil {
|
|
|
|
if i.MissingRefBehaviour == models.ImportMissingRefEnumFail {
|
|
|
|
return ErrParentStudioNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.MissingRefBehaviour == models.ImportMissingRefEnumIgnore {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.MissingRefBehaviour == models.ImportMissingRefEnumCreate {
|
2022-05-19 07:49:32 +00:00
|
|
|
parentID, err := i.createParentStudio(ctx, i.Input.ParentStudio)
|
2020-09-20 08:36:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-15 02:46:09 +00:00
|
|
|
i.studio.ParentID = &parentID
|
2020-09-20 08:36:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-06-15 02:46:09 +00:00
|
|
|
i.studio.ParentID = &studio.ID
|
2020-09-20 08:36:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) createParentStudio(ctx context.Context, name string) (int, error) {
|
2023-09-11 02:24:15 +00:00
|
|
|
newStudio := models.NewStudio()
|
|
|
|
newStudio.Name = name
|
2020-09-20 08:36:02 +00:00
|
|
|
|
2023-09-11 02:24:15 +00:00
|
|
|
err := i.ReaderWriter.Create(ctx, &newStudio)
|
2020-09-20 08:36:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
return newStudio.ID, nil
|
2020-09-20 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) PostImport(ctx context.Context, id int) error {
|
2020-09-20 08:36:02 +00:00
|
|
|
if len(i.imageData) > 0 {
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := i.ReaderWriter.UpdateImage(ctx, id, i.imageData); err != nil {
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
return fmt.Errorf("error setting studio image: %v", err)
|
2020-09-20 08:36:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Importer) Name() string {
|
|
|
|
return i.Input.Name
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) FindExistingID(ctx context.Context) (*int, error) {
|
2020-09-20 08:36:02 +00:00
|
|
|
const nocase = false
|
2022-05-19 07:49:32 +00:00
|
|
|
existing, err := i.ReaderWriter.FindByName(ctx, i.Name(), nocase)
|
2020-09-20 08:36:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing != nil {
|
|
|
|
id := existing.ID
|
|
|
|
return &id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) Create(ctx context.Context) (*int, error) {
|
2023-06-15 02:46:09 +00:00
|
|
|
err := i.ReaderWriter.Create(ctx, &i.studio)
|
2020-09-20 08:36:02 +00:00
|
|
|
if err != nil {
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
return nil, fmt.Errorf("error creating studio: %v", err)
|
2020-09-20 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
id := i.studio.ID
|
2020-09-20 08:36:02 +00:00
|
|
|
return &id, nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (i *Importer) Update(ctx context.Context, id int) error {
|
2020-09-20 08:36:02 +00:00
|
|
|
studio := i.studio
|
|
|
|
studio.ID = id
|
2023-06-15 02:46:09 +00:00
|
|
|
err := i.ReaderWriter.Update(ctx, &studio)
|
2020-09-20 08:36:02 +00:00
|
|
|
if err != nil {
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
return fmt.Errorf("error updating existing studio: %v", err)
|
2020-09-20 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-30 23:50:24 +00:00
|
|
|
|
|
|
|
func studioJSONtoStudio(studioJSON jsonschema.Studio) models.Studio {
|
|
|
|
newStudio := models.Studio{
|
|
|
|
Name: studioJSON.Name,
|
|
|
|
URL: studioJSON.URL,
|
|
|
|
Aliases: models.NewRelatedStrings(studioJSON.Aliases),
|
|
|
|
Details: studioJSON.Details,
|
2024-03-14 00:17:44 +00:00
|
|
|
Favorite: studioJSON.Favorite,
|
2023-07-30 23:50:24 +00:00
|
|
|
IgnoreAutoTag: studioJSON.IgnoreAutoTag,
|
|
|
|
CreatedAt: studioJSON.CreatedAt.GetTime(),
|
|
|
|
UpdatedAt: studioJSON.UpdatedAt.GetTime(),
|
|
|
|
|
|
|
|
StashIDs: models.NewRelatedStashIDs(studioJSON.StashIDs),
|
|
|
|
}
|
|
|
|
|
|
|
|
if studioJSON.Rating != 0 {
|
|
|
|
newStudio.Rating = &studioJSON.Rating
|
|
|
|
}
|
|
|
|
|
|
|
|
return newStudio
|
|
|
|
}
|