2020-10-12 23:12:46 +00:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
|
|
|
|
2020-10-12 23:12:46 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2022-04-18 00:50:10 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models/json"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models/jsonschema"
|
2020-10-12 23:12:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ToBasicJSON converts a image object into its JSON object equivalent. It
|
|
|
|
// does not convert the relationships to other objects, with the exception
|
|
|
|
// of cover image.
|
|
|
|
func ToBasicJSON(image *models.Image) *jsonschema.Image {
|
|
|
|
newImageJSON := jsonschema.Image{
|
2022-07-13 06:30:54 +00:00
|
|
|
Title: image.Title,
|
2023-09-12 03:31:53 +00:00
|
|
|
URLs: image.URLs.List(),
|
2022-07-13 06:30:54 +00:00
|
|
|
CreatedAt: json.JSONTime{Time: image.CreatedAt},
|
|
|
|
UpdatedAt: json.JSONTime{Time: image.UpdatedAt},
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
if image.Rating != nil {
|
|
|
|
newImageJSON.Rating = *image.Rating
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 00:13:41 +00:00
|
|
|
if image.Date != nil {
|
|
|
|
newImageJSON.Date = image.Date.String()
|
|
|
|
}
|
|
|
|
|
2020-12-17 21:06:49 +00:00
|
|
|
newImageJSON.Organized = image.Organized
|
2020-10-12 23:12:46 +00:00
|
|
|
newImageJSON.OCounter = image.OCounter
|
|
|
|
|
2022-09-01 07:54:34 +00:00
|
|
|
for _, f := range image.Files.List() {
|
2022-08-30 02:17:15 +00:00
|
|
|
newImageJSON.Files = append(newImageJSON.Files, f.Base().Path)
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
|
|
|
|
return &newImageJSON
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStudioName returns the name of the provided image's studio. It returns an
|
|
|
|
// empty string if there is no studio assigned to the image.
|
2023-09-01 00:39:29 +00:00
|
|
|
func GetStudioName(ctx context.Context, reader models.StudioGetter, image *models.Image) (string, error) {
|
2022-07-13 06:30:54 +00:00
|
|
|
if image.StudioID != nil {
|
|
|
|
studio, err := reader.Find(ctx, *image.StudioID)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if studio != nil {
|
2023-06-15 02:46:09 +00:00
|
|
|
return studio.Name, nil
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGalleryChecksum returns the checksum of the provided image. It returns an
|
|
|
|
// empty string if there is no gallery assigned to the image.
|
|
|
|
// func GetGalleryChecksum(reader models.GalleryReader, image *models.Image) (string, error) {
|
|
|
|
// gallery, err := reader.FindByImageID(image.ID)
|
|
|
|
// 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 getting image gallery: %v", err)
|
2020-10-12 23:12:46 +00:00
|
|
|
// }
|
|
|
|
|
|
|
|
// if gallery != nil {
|
|
|
|
// return gallery.Checksum, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return "", nil
|
|
|
|
// }
|