2019-02-09 12:30:49 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
Enable gocritic (#1848)
* Don't capitalize local variables
ValidCodecs -> validCodecs
* Capitalize deprecation markers
A deprecated marker should be capitalized.
* Use re.MustCompile for static regexes
If the regex fails to compile, it's a programmer error, and should be
treated as such. The regex is entirely static.
* Simplify else-if constructions
Rewrite
else { if cond {}}
to
else if cond {}
* Use a switch statement to analyze formats
Break an if-else chain. While here, simplify code flow.
Also introduce a proper static error for unsupported image formats,
paving the way for being able to check against the error.
* Rewrite ifElse chains into switch statements
The "Effective Go" https://golang.org/doc/effective_go#switch document
mentions it is more idiomatic to write if-else chains as switches when
it is possible.
Find all the plain rewrite occurrences in the code base and rewrite.
In some cases, the if-else chains are replaced by a switch scrutinizer.
That is, the code sequence
if x == 1 {
..
} else if x == 2 {
..
} else if x == 3 {
...
}
can be rewritten into
switch x {
case 1:
..
case 2:
..
case 3:
..
}
which is clearer for the compiler: it can decide if the switch is
better served by a jump-table then a branch-chain.
* Rewrite switches, introduce static errors
Introduce two new static errors:
* `ErrNotImplmented`
* `ErrNotSupported`
And use these rather than forming new generative errors whenever the
code is called. Code can now test on the errors (since they are static
and the pointers to them wont change).
Also rewrite ifElse chains into switches in this part of the code base.
* Introduce a StashBoxError in configuration
Since all stashbox errors are the same, treat them as such in the code
base. While here, rewrite an ifElse chain.
In the future, it might be beneifical to refactor configuration errors
into one error which can handle missing fields, which context the error
occurs in and so on. But for now, try to get an overview of the error
categories by hoisting them into static errors.
* Get rid of an else-block in transaction handling
If we succesfully `recover()`, we then always `panic()`. This means the
rest of the code is not reachable, so we can avoid having an else-block
here.
It also solves an ifElse-chain style check in the code base.
* Use strings.ReplaceAll
Rewrite
strings.Replace(s, o, n, -1)
into
strings.ReplaceAll(s, o, n)
To make it consistent and clear that we are doing an all-replace in the
string rather than replacing parts of it. It's more of a nitpick since
there are no implementation differences: the stdlib implementation is
just to supply -1.
* Rewrite via gocritic's assignOp
Statements of the form
x = x + e
is rewritten into
x += e
where applicable.
* Formatting
* Review comments handled
Stash-box is a proper noun.
Rewrite a switch into an if-chain which returns on the first error
encountered.
* Use context.TODO() over context.Background()
Patch in the same vein as everything else: use the TODO() marker so we
can search for it later and link it into the context tree/tentacle once
it reaches down to this level in the code base.
* Tell the linter to ignore a section in manager_tasks.go
The section is less readable, so mark it with a nolint for now. Because
the rewrite enables a ifElseChain, also mark that as nolint for now.
* Use strings.ReplaceAll over strings.Replace
* Apply an ifElse rewrite
else { if .. { .. } } rewrite into else if { .. }
* Use switch-statements over ifElseChains
Rewrite chains of if-else into switch statements. Where applicable,
add an early nil-guard to simplify case analysis. Also, in
ScanTask's Start(..), invert the logic to outdent the whole block, and
help the reader: if it's not a scene, the function flow is now far more
local to the top of the function, and it's clear that the rest of the
function has to do with scene management.
* Enable gocritic on the code base.
Disable appendAssign for now since we aren't passing that check yet.
* Document the nolint additions
* Document StashBoxBatchPerformerTagInput
2021-10-18 03:12:40 +00:00
|
|
|
"errors"
|
2019-02-09 12:30:49 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
2019-08-21 04:47:48 +00:00
|
|
|
|
2020-01-07 22:21:23 +00:00
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
2019-08-21 04:47:48 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2021-06-11 07:24:58 +00:00
|
|
|
"github.com/stashapp/stash/pkg/plugin"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
Enable gocritic (#1848)
* Don't capitalize local variables
ValidCodecs -> validCodecs
* Capitalize deprecation markers
A deprecated marker should be capitalized.
* Use re.MustCompile for static regexes
If the regex fails to compile, it's a programmer error, and should be
treated as such. The regex is entirely static.
* Simplify else-if constructions
Rewrite
else { if cond {}}
to
else if cond {}
* Use a switch statement to analyze formats
Break an if-else chain. While here, simplify code flow.
Also introduce a proper static error for unsupported image formats,
paving the way for being able to check against the error.
* Rewrite ifElse chains into switch statements
The "Effective Go" https://golang.org/doc/effective_go#switch document
mentions it is more idiomatic to write if-else chains as switches when
it is possible.
Find all the plain rewrite occurrences in the code base and rewrite.
In some cases, the if-else chains are replaced by a switch scrutinizer.
That is, the code sequence
if x == 1 {
..
} else if x == 2 {
..
} else if x == 3 {
...
}
can be rewritten into
switch x {
case 1:
..
case 2:
..
case 3:
..
}
which is clearer for the compiler: it can decide if the switch is
better served by a jump-table then a branch-chain.
* Rewrite switches, introduce static errors
Introduce two new static errors:
* `ErrNotImplmented`
* `ErrNotSupported`
And use these rather than forming new generative errors whenever the
code is called. Code can now test on the errors (since they are static
and the pointers to them wont change).
Also rewrite ifElse chains into switches in this part of the code base.
* Introduce a StashBoxError in configuration
Since all stashbox errors are the same, treat them as such in the code
base. While here, rewrite an ifElse chain.
In the future, it might be beneifical to refactor configuration errors
into one error which can handle missing fields, which context the error
occurs in and so on. But for now, try to get an overview of the error
categories by hoisting them into static errors.
* Get rid of an else-block in transaction handling
If we succesfully `recover()`, we then always `panic()`. This means the
rest of the code is not reachable, so we can avoid having an else-block
here.
It also solves an ifElse-chain style check in the code base.
* Use strings.ReplaceAll
Rewrite
strings.Replace(s, o, n, -1)
into
strings.ReplaceAll(s, o, n)
To make it consistent and clear that we are doing an all-replace in the
string rather than replacing parts of it. It's more of a nitpick since
there are no implementation differences: the stdlib implementation is
just to supply -1.
* Rewrite via gocritic's assignOp
Statements of the form
x = x + e
is rewritten into
x += e
where applicable.
* Formatting
* Review comments handled
Stash-box is a proper noun.
Rewrite a switch into an if-chain which returns on the first error
encountered.
* Use context.TODO() over context.Background()
Patch in the same vein as everything else: use the TODO() marker so we
can search for it later and link it into the context tree/tentacle once
it reaches down to this level in the code base.
* Tell the linter to ignore a section in manager_tasks.go
The section is less readable, so mark it with a nolint for now. Because
the rewrite enables a ifElseChain, also mark that as nolint for now.
* Use strings.ReplaceAll over strings.Replace
* Apply an ifElse rewrite
else { if .. { .. } } rewrite into else if { .. }
* Use switch-statements over ifElseChains
Rewrite chains of if-else into switch statements. Where applicable,
add an early nil-guard to simplify case analysis. Also, in
ScanTask's Start(..), invert the logic to outdent the whole block, and
help the reader: if it's not a scene, the function flow is now far more
local to the top of the function, and it's clear that the rest of the
function has to do with scene management.
* Enable gocritic on the code base.
Disable appendAssign for now since we aren't passing that check yet.
* Document the nolint additions
* Document StashBoxBatchPerformerTagInput
2021-10-18 03:12:40 +00:00
|
|
|
var (
|
|
|
|
ErrNotImplemented = errors.New("not implemented")
|
|
|
|
ErrNotSupported = errors.New("not supported")
|
|
|
|
)
|
|
|
|
|
2021-06-11 07:24:58 +00:00
|
|
|
type hookExecutor interface {
|
|
|
|
ExecutePostHooks(ctx context.Context, id int, hookType plugin.HookTriggerEnum, input interface{}, inputFields []string)
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
type Resolver struct {
|
2021-06-11 07:24:58 +00:00
|
|
|
txnManager models.TransactionManager
|
|
|
|
hookExecutor hookExecutor
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
|
|
|
func (r *Resolver) Gallery() models.GalleryResolver {
|
|
|
|
return &galleryResolver{r}
|
|
|
|
}
|
|
|
|
func (r *Resolver) Mutation() models.MutationResolver {
|
|
|
|
return &mutationResolver{r}
|
|
|
|
}
|
|
|
|
func (r *Resolver) Performer() models.PerformerResolver {
|
|
|
|
return &performerResolver{r}
|
|
|
|
}
|
|
|
|
func (r *Resolver) Query() models.QueryResolver {
|
|
|
|
return &queryResolver{r}
|
|
|
|
}
|
|
|
|
func (r *Resolver) Scene() models.SceneResolver {
|
|
|
|
return &sceneResolver{r}
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
func (r *Resolver) Image() models.ImageResolver {
|
|
|
|
return &imageResolver{r}
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
func (r *Resolver) SceneMarker() models.SceneMarkerResolver {
|
|
|
|
return &sceneMarkerResolver{r}
|
|
|
|
}
|
|
|
|
func (r *Resolver) Studio() models.StudioResolver {
|
|
|
|
return &studioResolver{r}
|
|
|
|
}
|
2020-03-10 03:28:15 +00:00
|
|
|
func (r *Resolver) Movie() models.MovieResolver {
|
|
|
|
return &movieResolver{r}
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
func (r *Resolver) Subscription() models.SubscriptionResolver {
|
|
|
|
return &subscriptionResolver{r}
|
|
|
|
}
|
|
|
|
func (r *Resolver) Tag() models.TagResolver {
|
|
|
|
return &tagResolver{r}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mutationResolver struct{ *Resolver }
|
|
|
|
type queryResolver struct{ *Resolver }
|
|
|
|
type subscriptionResolver struct{ *Resolver }
|
|
|
|
|
|
|
|
type galleryResolver struct{ *Resolver }
|
|
|
|
type performerResolver struct{ *Resolver }
|
|
|
|
type sceneResolver struct{ *Resolver }
|
|
|
|
type sceneMarkerResolver struct{ *Resolver }
|
2020-10-12 23:12:46 +00:00
|
|
|
type imageResolver struct{ *Resolver }
|
2019-02-09 12:30:49 +00:00
|
|
|
type studioResolver struct{ *Resolver }
|
2020-03-10 03:28:15 +00:00
|
|
|
type movieResolver struct{ *Resolver }
|
2019-02-09 12:30:49 +00:00
|
|
|
type tagResolver struct{ *Resolver }
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *Resolver) withTxn(ctx context.Context, fn func(r models.Repository) error) error {
|
|
|
|
return r.txnManager.WithTxn(ctx, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver) withReadTxn(ctx context.Context, fn func(r models.ReaderRepository) error) error {
|
|
|
|
return r.txnManager.WithReadTxn(ctx, fn)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *queryResolver) MarkerWall(ctx context.Context, q *string) (ret []*models.SceneMarker, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
ret, err = repo.SceneMarker().Wall(q)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ret, nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (r *queryResolver) SceneWall(ctx context.Context, q *string) (ret []*models.Scene, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
ret, err = repo.Scene().Wall(q)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *queryResolver) MarkerStrings(ctx context.Context, q *string, sort *string) (ret []*models.MarkerStringsResultType, err error) {
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
ret, err = repo.SceneMarker().GetMarkerStrings(q, sort)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 15:35:06 +00:00
|
|
|
func (r *queryResolver) Stats(ctx context.Context) (*models.StatsResultType, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
var ret models.StatsResultType
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
scenesQB := repo.Scene()
|
|
|
|
imageQB := repo.Image()
|
|
|
|
galleryQB := repo.Gallery()
|
|
|
|
studiosQB := repo.Studio()
|
|
|
|
performersQB := repo.Performer()
|
|
|
|
moviesQB := repo.Movie()
|
|
|
|
tagsQB := repo.Tag()
|
|
|
|
scenesCount, _ := scenesQB.Count()
|
|
|
|
scenesSize, _ := scenesQB.Size()
|
2021-08-26 03:37:08 +00:00
|
|
|
scenesDuration, _ := scenesQB.Duration()
|
2021-01-18 01:23:20 +00:00
|
|
|
imageCount, _ := imageQB.Count()
|
|
|
|
imageSize, _ := imageQB.Size()
|
|
|
|
galleryCount, _ := galleryQB.Count()
|
|
|
|
performersCount, _ := performersQB.Count()
|
|
|
|
studiosCount, _ := studiosQB.Count()
|
|
|
|
moviesCount, _ := moviesQB.Count()
|
|
|
|
tagsCount, _ := tagsQB.Count()
|
|
|
|
|
|
|
|
ret = models.StatsResultType{
|
|
|
|
SceneCount: scenesCount,
|
|
|
|
ScenesSize: scenesSize,
|
2021-08-26 03:37:08 +00:00
|
|
|
ScenesDuration: scenesDuration,
|
2021-01-18 01:23:20 +00:00
|
|
|
ImageCount: imageCount,
|
|
|
|
ImagesSize: imageSize,
|
|
|
|
GalleryCount: galleryCount,
|
|
|
|
PerformerCount: performersCount,
|
|
|
|
StudioCount: studiosCount,
|
|
|
|
MovieCount: moviesCount,
|
|
|
|
TagCount: tagsCount,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ret, nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 04:47:48 +00:00
|
|
|
func (r *queryResolver) Version(ctx context.Context) (*models.Version, error) {
|
2019-11-17 21:41:08 +00:00
|
|
|
version, hash, buildtime := GetVersion()
|
2019-08-21 04:47:48 +00:00
|
|
|
|
|
|
|
return &models.Version{
|
2019-11-17 21:41:08 +00:00
|
|
|
Version: &version,
|
2019-08-21 04:47:48 +00:00
|
|
|
Hash: hash,
|
|
|
|
BuildTime: buildtime,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-07 22:21:23 +00:00
|
|
|
//Gets latest version (git shorthash commit for now)
|
|
|
|
func (r *queryResolver) Latestversion(ctx context.Context) (*models.ShortVersion, error) {
|
Toward better context handling (#1835)
* Use the request context
The code uses context.Background() in a flow where there is a
http.Request. Use the requests context instead.
* Use a true context in the plugin example
Let AddTag/RemoveTag take a context and use that context throughout
the example.
* Avoid the use of context.Background
Prefer context.TODO over context.Background deep in the call chain.
This marks the site as something which we need to context-handle
later, and also makes it clear to the reader that the context is
sort-of temporary in the code base.
While here, be consistent in handling the `act` variable in each
branch of the if .. { .. } .. check.
* Prefer context.TODO over context.Background
For the different scraping operations here, there is a context
higher up the call chain, which we ought to use. Mark the call-sites
as TODO for now, so we can come back later on a sweep of which parts
can be context-lifted.
* Thread context upwards
Initialization requires context for transactions. Thread the context
upward the call chain.
At the intialization call, add a context.TODO since we can't break this
yet. The singleton assumption prevents us from pulling it up into main for
now.
* make tasks context-aware
Change the task interface to understand contexts.
Pass the context down in some of the branches where it is needed.
* Make QueryStashBoxScene context-aware
This call naturally sits inside the request-context. Use it.
* Introduce a context in the JS plugin code
This allows us to use a context for HTTP calls inside the system.
Mark the context with a TODO at top level for now.
* Nitpick error formatting
Use %v rather than %s for error interfaces.
Do not begin an error strong with a capital letter.
* Avoid the use of http.Get in FFMPEG download chain
Since http.Get has no context, it isn't possible to break out or have
policy induced. The call will block until the GET completes. Rewrite
to use a http Request and provide a context.
Thread the context through the call chain for now. provide
context.TODO() at the top level of the initialization chain.
* Make getRemoteCDPWSAddress aware of contexts
Eliminate a call to http.Get and replace it with a context-aware
variant.
Push the context upwards in the call chain, but plug it before the
scraper interface so we don't have to rewrite said interface yet.
Plugged with context.TODO()
* Scraper: make the getImage function context-aware
Use a context, and pass it upwards. Plug it with context.TODO()
up the chain before the rewrite gets too much out of hand for now.
Minor tweaks along the way, remove a call to context.Background()
deep in the call chain.
* Make NOTIFY request context-aware
The call sits inside a Request-handler. So it's natural to use the
requests context as the context for the outgoing HTTP request.
* Use a context in the url scraper code
We are sitting in code which has a context, so utilize it for the
request as well.
* Use a context when checking versions
When we check the version of stash on Github, use a context. Thread
the context up to the initialization routine of the HTTP/GraphQL
server and plug it with a context.TODO() for now.
This paves the way for providing a context to the HTTP server code in a
future patch.
* Make utils func ReadImage context-aware
In almost all of the cases, there is a context in the call chain which
is a natural use. This is true for all the GraphQL mutations.
The exception is in task_stash_box_tag, so plug that task with
context.TODO() for now.
* Make stash-box get context-aware
Thread a context through the call chain until we hit the Client API.
Plug it with context.TODO() there for now.
* Enable the noctx linter
The code is now free of any uncontexted HTTP request. This means we
pass the noctx linter, and we can enable it in the code base.
2021-10-14 04:32:41 +00:00
|
|
|
ver, url, err := GetLatestVersion(ctx, true)
|
2020-01-07 22:21:23 +00:00
|
|
|
if err == nil {
|
|
|
|
logger.Infof("Retrieved latest hash: %s", ver)
|
|
|
|
} else {
|
|
|
|
logger.Errorf("Error while retrieving latest hash: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &models.ShortVersion{
|
|
|
|
Shorthash: ver,
|
|
|
|
URL: url,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
// Get scene marker tags which show up under the video.
|
2019-05-27 19:34:26 +00:00
|
|
|
func (r *queryResolver) SceneMarkerTags(ctx context.Context, scene_id string) ([]*models.SceneMarkerTag, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
sceneID, err := strconv.Atoi(scene_id)
|
2019-02-09 12:30:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var keys []int
|
2021-01-18 01:23:20 +00:00
|
|
|
tags := make(map[int]*models.SceneMarkerTag)
|
|
|
|
|
|
|
|
if err := r.withReadTxn(ctx, func(repo models.ReaderRepository) error {
|
|
|
|
sceneMarkers, err := repo.SceneMarker().FindBySceneID(sceneID)
|
2019-02-09 12:30:49 +00:00
|
|
|
if err != nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
return err
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
tqb := repo.Tag()
|
|
|
|
for _, sceneMarker := range sceneMarkers {
|
|
|
|
markerPrimaryTag, err := tqb.Find(sceneMarker.PrimaryTagID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, hasKey := tags[markerPrimaryTag.ID]
|
|
|
|
if !hasKey {
|
2021-05-25 01:03:09 +00:00
|
|
|
sceneMarkerTag := &models.SceneMarkerTag{Tag: markerPrimaryTag}
|
2021-01-18 01:23:20 +00:00
|
|
|
tags[markerPrimaryTag.ID] = sceneMarkerTag
|
|
|
|
keys = append(keys, markerPrimaryTag.ID)
|
|
|
|
}
|
|
|
|
tags[markerPrimaryTag.ID].SceneMarkers = append(tags[markerPrimaryTag.ID].SceneMarkers, sceneMarker)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort so that primary tags that show up earlier in the video are first.
|
|
|
|
sort.Slice(keys, func(i, j int) bool {
|
|
|
|
a := tags[keys[i]]
|
|
|
|
b := tags[keys[j]]
|
|
|
|
return a.SceneMarkers[0].Seconds < b.SceneMarkers[0].Seconds
|
|
|
|
})
|
|
|
|
|
2019-05-27 19:34:26 +00:00
|
|
|
var result []*models.SceneMarkerTag
|
2019-02-09 12:30:49 +00:00
|
|
|
for _, key := range keys {
|
2019-05-27 19:34:26 +00:00
|
|
|
result = append(result, tags[key])
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|