2021-10-14 23:39:48 +00:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-06 01:59:28 +00:00
|
|
|
"path/filepath"
|
2021-10-14 23:39:48 +00:00
|
|
|
|
2022-05-06 01:59:28 +00:00
|
|
|
"github.com/stashapp/stash/internal/manager/config"
|
2022-04-18 00:50:10 +00:00
|
|
|
"github.com/stashapp/stash/pkg/ffmpeg"
|
2021-10-14 23:39:48 +00:00
|
|
|
"github.com/stashapp/stash/pkg/file"
|
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
"github.com/stashapp/stash/pkg/scene"
|
2022-04-18 00:50:10 +00:00
|
|
|
"github.com/stashapp/stash/pkg/scene/generate"
|
2021-10-14 23:39:48 +00:00
|
|
|
)
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
type sceneScreenshotter struct {
|
|
|
|
g *generate.Generator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *sceneScreenshotter) GenerateScreenshot(ctx context.Context, probeResult *ffmpeg.VideoFile, hash string) error {
|
|
|
|
return ss.g.Screenshot(ctx, probeResult.Path, hash, probeResult.Width, probeResult.Duration, generate.ScreenshotOptions{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *sceneScreenshotter) GenerateThumbnail(ctx context.Context, probeResult *ffmpeg.VideoFile, hash string) error {
|
2022-05-05 02:11:10 +00:00
|
|
|
return ss.g.Thumbnail(ctx, probeResult.Path, hash, probeResult.Duration, generate.ScreenshotOptions{})
|
2022-04-18 00:50:10 +00:00
|
|
|
}
|
|
|
|
|
Hoist context, enable errchkjson (#2488)
* Make the script scraper context-aware
Connect the context to the command execution. This means command
execution can be aborted if the context is canceled. The context is
usually bound to user-interaction, i.e., a scraper operation issued
by the user. Hence, it seems correct to abort a command if the user
aborts.
* Enable errchkjson
Some json marshal calls are *safe* in that they can never fail. This is
conditional on the types of the the data being encoded. errchkjson finds
those calls which are unsafe, and also not checked for errors.
Add logging warnings to the place where unsafe encodings might happen.
This can help uncover usage bugs early in stash if they are tripped,
making debugging easier.
While here, keep the checker enabled in the linter to capture future
uses of json marshalling.
* Pass the context for zip file scanning.
* Pass the context in scanning
* Pass context, replace context.TODO()
Where applicable, pass the context down toward the lower functions in
the call stack. Replace uses of context.TODO() with the passed context.
This makes the code more context-aware, and you can rely on aborting
contexts to clean up subsystems to a far greater extent now.
I've left the cases where there is a context in a struct. My gut feeling
is that they have solutions that are nice, but they require more deep
thinking to unveil how to handle it.
* Remove context from task-structs
As a rule, contexts are better passed explicitly to functions than they
are passed implicitly via structs. In the case of tasks, we already
have a valid context in scope when creating the struct, so remove ctx
from the struct and use the scoped context instead.
With this change it is clear that the scanning functions are under a
context, and the task-starting caller has jurisdiction over the context
and its lifetime. A reader of the code don't have to figure out where
the context are coming from anymore.
While here, connect context.TODO() to the newly scoped context in most
of the scan code.
* Remove context from autotag struct too
* Make more context-passing explicit
In all of these cases, there is an applicable context which is close
in the call-tree. Hook up to this context.
* Simplify context passing in manager
The managers context handling generally wants to use an outer context
if applicable. However, the code doesn't pass it explicitly, but stores
it in a struct. Pull out the context from the struct and use it to
explicitly pass it.
At a later point in time, we probably want to handle this by handing
over the job to a different (program-lifetime) context for background
jobs, but this will do for a start.
2022-04-15 01:34:53 +00:00
|
|
|
func (t *ScanTask) scanScene(ctx context.Context) *models.Scene {
|
2021-10-14 23:39:48 +00:00
|
|
|
logError := func(err error) *models.Scene {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var retScene *models.Scene
|
|
|
|
var s *models.Scene
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := t.TxnManager.WithTxn(ctx, func(ctx context.Context) error {
|
2021-10-14 23:39:48 +00:00
|
|
|
var err error
|
2022-05-19 07:49:32 +00:00
|
|
|
s, err = t.TxnManager.Scene.FindByPath(ctx, t.file.Path())
|
2021-10-14 23:39:48 +00:00
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
g := &generate.Generator{
|
|
|
|
Encoder: instance.FFMPEG,
|
|
|
|
LockManager: instance.ReadLockManager,
|
|
|
|
ScenePaths: instance.Paths.Scene,
|
|
|
|
}
|
|
|
|
|
2021-10-14 23:39:48 +00:00
|
|
|
scanner := scene.Scanner{
|
|
|
|
Scanner: scene.FileScanner(&file.FSHasher{}, t.fileNamingAlgorithm, t.calculateMD5),
|
|
|
|
StripFileExtension: t.StripFileExtension,
|
|
|
|
FileNamingAlgorithm: t.fileNamingAlgorithm,
|
|
|
|
TxnManager: t.TxnManager,
|
2022-05-19 07:49:32 +00:00
|
|
|
CreatorUpdater: t.TxnManager.Scene,
|
2021-10-14 23:39:48 +00:00
|
|
|
Paths: GetInstance().Paths,
|
2022-05-19 07:49:32 +00:00
|
|
|
CaseSensitiveFs: t.CaseSensitiveFs,
|
2022-04-18 00:50:10 +00:00
|
|
|
Screenshotter: &sceneScreenshotter{
|
|
|
|
g: g,
|
|
|
|
},
|
|
|
|
VideoFileCreator: &instance.FFProbe,
|
|
|
|
PluginCache: instance.PluginCache,
|
|
|
|
MutexManager: t.mutexManager,
|
|
|
|
UseFileMetadata: t.UseFileMetadata,
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if s != nil {
|
Hoist context, enable errchkjson (#2488)
* Make the script scraper context-aware
Connect the context to the command execution. This means command
execution can be aborted if the context is canceled. The context is
usually bound to user-interaction, i.e., a scraper operation issued
by the user. Hence, it seems correct to abort a command if the user
aborts.
* Enable errchkjson
Some json marshal calls are *safe* in that they can never fail. This is
conditional on the types of the the data being encoded. errchkjson finds
those calls which are unsafe, and also not checked for errors.
Add logging warnings to the place where unsafe encodings might happen.
This can help uncover usage bugs early in stash if they are tripped,
making debugging easier.
While here, keep the checker enabled in the linter to capture future
uses of json marshalling.
* Pass the context for zip file scanning.
* Pass the context in scanning
* Pass context, replace context.TODO()
Where applicable, pass the context down toward the lower functions in
the call stack. Replace uses of context.TODO() with the passed context.
This makes the code more context-aware, and you can rely on aborting
contexts to clean up subsystems to a far greater extent now.
I've left the cases where there is a context in a struct. My gut feeling
is that they have solutions that are nice, but they require more deep
thinking to unveil how to handle it.
* Remove context from task-structs
As a rule, contexts are better passed explicitly to functions than they
are passed implicitly via structs. In the case of tasks, we already
have a valid context in scope when creating the struct, so remove ctx
from the struct and use the scoped context instead.
With this change it is clear that the scanning functions are under a
context, and the task-starting caller has jurisdiction over the context
and its lifetime. A reader of the code don't have to figure out where
the context are coming from anymore.
While here, connect context.TODO() to the newly scoped context in most
of the scan code.
* Remove context from autotag struct too
* Make more context-passing explicit
In all of these cases, there is an applicable context which is close
in the call-tree. Hook up to this context.
* Simplify context passing in manager
The managers context handling generally wants to use an outer context
if applicable. However, the code doesn't pass it explicitly, but stores
it in a struct. Pull out the context from the struct and use it to
explicitly pass it.
At a later point in time, we probably want to handle this by handing
over the job to a different (program-lifetime) context for background
jobs, but this will do for a start.
2022-04-15 01:34:53 +00:00
|
|
|
if err := scanner.ScanExisting(ctx, s, t.file); err != nil {
|
2021-10-14 23:39:48 +00:00
|
|
|
return logError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
Hoist context, enable errchkjson (#2488)
* Make the script scraper context-aware
Connect the context to the command execution. This means command
execution can be aborted if the context is canceled. The context is
usually bound to user-interaction, i.e., a scraper operation issued
by the user. Hence, it seems correct to abort a command if the user
aborts.
* Enable errchkjson
Some json marshal calls are *safe* in that they can never fail. This is
conditional on the types of the the data being encoded. errchkjson finds
those calls which are unsafe, and also not checked for errors.
Add logging warnings to the place where unsafe encodings might happen.
This can help uncover usage bugs early in stash if they are tripped,
making debugging easier.
While here, keep the checker enabled in the linter to capture future
uses of json marshalling.
* Pass the context for zip file scanning.
* Pass the context in scanning
* Pass context, replace context.TODO()
Where applicable, pass the context down toward the lower functions in
the call stack. Replace uses of context.TODO() with the passed context.
This makes the code more context-aware, and you can rely on aborting
contexts to clean up subsystems to a far greater extent now.
I've left the cases where there is a context in a struct. My gut feeling
is that they have solutions that are nice, but they require more deep
thinking to unveil how to handle it.
* Remove context from task-structs
As a rule, contexts are better passed explicitly to functions than they
are passed implicitly via structs. In the case of tasks, we already
have a valid context in scope when creating the struct, so remove ctx
from the struct and use the scoped context instead.
With this change it is clear that the scanning functions are under a
context, and the task-starting caller has jurisdiction over the context
and its lifetime. A reader of the code don't have to figure out where
the context are coming from anymore.
While here, connect context.TODO() to the newly scoped context in most
of the scan code.
* Remove context from autotag struct too
* Make more context-passing explicit
In all of these cases, there is an applicable context which is close
in the call-tree. Hook up to this context.
* Simplify context passing in manager
The managers context handling generally wants to use an outer context
if applicable. However, the code doesn't pass it explicitly, but stores
it in a struct. Pull out the context from the struct and use it to
explicitly pass it.
At a later point in time, we probably want to handle this by handing
over the job to a different (program-lifetime) context for background
jobs, but this will do for a start.
2022-04-15 01:34:53 +00:00
|
|
|
retScene, err = scanner.ScanNew(ctx, t.file)
|
2021-10-14 23:39:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return logError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return retScene
|
|
|
|
}
|
2022-05-06 01:59:28 +00:00
|
|
|
|
|
|
|
// associates captions to scene/s with the same basename
|
|
|
|
func (t *ScanTask) associateCaptions(ctx context.Context) {
|
|
|
|
vExt := config.GetInstance().GetVideoExtensions()
|
|
|
|
captionPath := t.file.Path()
|
|
|
|
captionLang := scene.GetCaptionsLangFromPath(captionPath)
|
|
|
|
|
|
|
|
relatedFiles := scene.GenerateCaptionCandidates(captionPath, vExt)
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := t.TxnManager.WithTxn(ctx, func(ctx context.Context) error {
|
2022-05-06 01:59:28 +00:00
|
|
|
var err error
|
2022-05-19 07:49:32 +00:00
|
|
|
sqb := t.TxnManager.Scene
|
2022-05-06 01:59:28 +00:00
|
|
|
|
|
|
|
for _, scenePath := range relatedFiles {
|
2022-05-19 07:49:32 +00:00
|
|
|
s, er := sqb.FindByPath(ctx, scenePath)
|
2022-05-06 01:59:28 +00:00
|
|
|
|
|
|
|
if er != nil {
|
|
|
|
logger.Errorf("Error searching for scene %s: %v", scenePath, er)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if s != nil { // found related Scene
|
|
|
|
logger.Debugf("Matched captions to scene %s", s.Path)
|
2022-05-19 07:49:32 +00:00
|
|
|
captions, er := sqb.GetCaptions(ctx, s.ID)
|
2022-05-06 01:59:28 +00:00
|
|
|
if er == nil {
|
|
|
|
fileExt := filepath.Ext(captionPath)
|
|
|
|
ext := fileExt[1:]
|
|
|
|
if !scene.IsLangInCaptions(captionLang, ext, captions) { // only update captions if language code is not present
|
|
|
|
newCaption := &models.SceneCaption{
|
|
|
|
LanguageCode: captionLang,
|
|
|
|
Filename: filepath.Base(captionPath),
|
|
|
|
CaptionType: ext,
|
|
|
|
}
|
|
|
|
captions = append(captions, newCaption)
|
2022-05-19 07:49:32 +00:00
|
|
|
er = sqb.UpdateCaptions(ctx, s.ID, captions)
|
2022-05-06 01:59:28 +00:00
|
|
|
if er == nil {
|
|
|
|
logger.Debugf("Updated captions for scene %s. Added %s", s.Path, captionLang)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|