2021-10-14 23:39:48 +00:00
|
|
|
package scene
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-04-18 00:50:10 +00:00
|
|
|
"path/filepath"
|
2021-10-14 23:39:48 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/ffmpeg"
|
|
|
|
"github.com/stashapp/stash/pkg/file"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/fsutil"
|
2021-10-14 23:39:48 +00:00
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models/paths"
|
2021-10-14 23:39:48 +00:00
|
|
|
"github.com/stashapp/stash/pkg/plugin"
|
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
const mutexType = "scene"
|
|
|
|
|
|
|
|
type videoFileCreator interface {
|
2022-04-18 00:50:10 +00:00
|
|
|
NewVideoFile(path string) (*ffmpeg.VideoFile, error)
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Scanner struct {
|
|
|
|
file.Scanner
|
|
|
|
|
|
|
|
StripFileExtension bool
|
|
|
|
UseFileMetadata bool
|
|
|
|
FileNamingAlgorithm models.HashAlgorithm
|
|
|
|
|
|
|
|
CaseSensitiveFs bool
|
|
|
|
TxnManager models.TransactionManager
|
|
|
|
Paths *paths.Paths
|
|
|
|
Screenshotter screenshotter
|
|
|
|
VideoFileCreator videoFileCreator
|
|
|
|
PluginCache *plugin.Cache
|
|
|
|
MutexManager *utils.MutexManager
|
|
|
|
}
|
|
|
|
|
|
|
|
func FileScanner(hasher file.Hasher, fileNamingAlgorithm models.HashAlgorithm, calculateMD5 bool) file.Scanner {
|
|
|
|
return file.Scanner{
|
|
|
|
Hasher: hasher,
|
|
|
|
CalculateOSHash: true,
|
|
|
|
CalculateMD5: fileNamingAlgorithm == models.HashAlgorithmMd5 || calculateMD5,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 (scanner *Scanner) ScanExisting(ctx context.Context, existing file.FileBased, file file.SourceFile) (err error) {
|
2021-10-14 23:39:48 +00:00
|
|
|
scanned, err := scanner.Scanner.ScanExisting(existing, file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := existing.(*models.Scene)
|
|
|
|
|
|
|
|
path := scanned.New.Path
|
|
|
|
interactive := getInteractive(path)
|
|
|
|
|
|
|
|
oldHash := s.GetHash(scanner.FileNamingAlgorithm)
|
|
|
|
changed := false
|
|
|
|
|
|
|
|
var videoFile *ffmpeg.VideoFile
|
|
|
|
|
|
|
|
if scanned.ContentsChanged() {
|
|
|
|
logger.Infof("%s has been updated: rescanning", path)
|
|
|
|
|
|
|
|
s.SetFile(*scanned.New)
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
videoFile, err = scanner.VideoFileCreator.NewVideoFile(path)
|
2021-10-14 23:39:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
if err := videoFileToScene(s, videoFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-14 23:39:48 +00:00
|
|
|
changed = true
|
|
|
|
} else if scanned.FileUpdated() || s.Interactive != interactive {
|
|
|
|
logger.Infof("Updated scene file %s", path)
|
|
|
|
|
|
|
|
// update fields as needed
|
|
|
|
s.SetFile(*scanned.New)
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for container
|
|
|
|
if !s.Format.Valid {
|
|
|
|
if videoFile == nil {
|
2022-04-18 00:50:10 +00:00
|
|
|
videoFile, err = scanner.VideoFileCreator.NewVideoFile(path)
|
2021-10-14 23:39:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 00:50:10 +00:00
|
|
|
container, err := ffmpeg.MatchContainer(videoFile.Container, path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting container for %s: %w", path, err)
|
|
|
|
}
|
2021-10-14 23:39:48 +00:00
|
|
|
logger.Infof("Adding container %s to file %s", container, path)
|
|
|
|
s.Format = models.NullString(string(container))
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
|
2022-05-06 01:59:28 +00:00
|
|
|
if err := scanner.TxnManager.WithTxn(context.TODO(), func(r models.Repository) error {
|
|
|
|
var err error
|
|
|
|
sqb := r.Scene()
|
|
|
|
|
|
|
|
captions, er := sqb.GetCaptions(s.ID)
|
|
|
|
if er == nil {
|
|
|
|
if len(captions) > 0 {
|
|
|
|
clean, altered := CleanCaptions(s.Path, captions)
|
|
|
|
if altered {
|
|
|
|
er = sqb.UpdateCaptions(s.ID, clean)
|
|
|
|
if er == nil {
|
|
|
|
logger.Debugf("Captions for %s cleaned: %s -> %s", path, captions, clean)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
}
|
|
|
|
|
2021-10-14 23:39:48 +00:00
|
|
|
if changed {
|
|
|
|
// we are operating on a checksum now, so grab a mutex on the checksum
|
|
|
|
done := make(chan struct{})
|
|
|
|
if scanned.New.OSHash != "" {
|
|
|
|
scanner.MutexManager.Claim(mutexType, scanned.New.OSHash, done)
|
|
|
|
}
|
|
|
|
if scanned.New.Checksum != "" {
|
|
|
|
scanner.MutexManager.Claim(mutexType, scanned.New.Checksum, done)
|
|
|
|
}
|
|
|
|
|
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.TxnManager.WithTxn(ctx, func(r models.Repository) error {
|
2021-10-14 23:39:48 +00:00
|
|
|
defer close(done)
|
|
|
|
qb := r.Scene()
|
|
|
|
|
|
|
|
// ensure no clashes of hashes
|
|
|
|
if scanned.New.Checksum != "" && scanned.Old.Checksum != scanned.New.Checksum {
|
|
|
|
dupe, _ := qb.FindByChecksum(s.Checksum.String)
|
|
|
|
if dupe != nil {
|
|
|
|
return fmt.Errorf("MD5 for file %s is the same as that of %s", path, dupe.Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if scanned.New.OSHash != "" && scanned.Old.OSHash != scanned.New.OSHash {
|
|
|
|
dupe, _ := qb.FindByOSHash(scanned.New.OSHash)
|
|
|
|
if dupe != nil {
|
|
|
|
return fmt.Errorf("OSHash for file %s is the same as that of %s", path, dupe.Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:41:07 +00:00
|
|
|
s.Interactive = interactive
|
2021-10-14 23:39:48 +00:00
|
|
|
s.UpdatedAt = models.SQLiteTimestamp{Timestamp: time.Now()}
|
|
|
|
|
|
|
|
_, err := qb.UpdateFull(*s)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate any generated files if the hash has changed
|
2022-03-17 00:33:59 +00:00
|
|
|
newHash := s.GetHash(scanner.FileNamingAlgorithm)
|
2021-10-14 23:39:48 +00:00
|
|
|
if newHash != oldHash {
|
|
|
|
MigrateHash(scanner.Paths, oldHash, newHash)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
scanner.PluginCache.ExecutePostHooks(ctx, s.ID, plugin.SceneUpdatePost, nil, nil)
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We already have this item in the database
|
|
|
|
// check for thumbnails, screenshots
|
|
|
|
scanner.makeScreenshots(path, videoFile, s.GetHash(scanner.FileNamingAlgorithm))
|
|
|
|
|
|
|
|
return 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
|
|
|
func (scanner *Scanner) ScanNew(ctx context.Context, file file.SourceFile) (retScene *models.Scene, err error) {
|
2021-10-14 23:39:48 +00:00
|
|
|
scanned, err := scanner.Scanner.ScanNew(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := file.Path()
|
|
|
|
checksum := scanned.Checksum
|
|
|
|
oshash := scanned.OSHash
|
|
|
|
|
|
|
|
// grab a mutex on the checksum and oshash
|
|
|
|
done := make(chan struct{})
|
|
|
|
if oshash != "" {
|
|
|
|
scanner.MutexManager.Claim(mutexType, oshash, done)
|
|
|
|
}
|
|
|
|
if checksum != "" {
|
|
|
|
scanner.MutexManager.Claim(mutexType, checksum, done)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
// check for scene by checksum and oshash - MD5 should be
|
|
|
|
// redundant, but check both
|
|
|
|
var s *models.Scene
|
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.TxnManager.WithReadTxn(ctx, func(r models.ReaderRepository) error {
|
2021-10-14 23:39:48 +00:00
|
|
|
qb := r.Scene()
|
|
|
|
if checksum != "" {
|
|
|
|
s, _ = qb.FindByChecksum(checksum)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s == nil {
|
|
|
|
s, _ = qb.FindByOSHash(oshash)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sceneHash := oshash
|
|
|
|
|
|
|
|
if scanner.FileNamingAlgorithm == models.HashAlgorithmMd5 {
|
|
|
|
sceneHash = checksum
|
|
|
|
}
|
|
|
|
|
|
|
|
interactive := getInteractive(file.Path())
|
|
|
|
|
|
|
|
if s != nil {
|
2022-03-17 00:33:59 +00:00
|
|
|
exists, _ := fsutil.FileExists(s.Path)
|
2021-10-14 23:39:48 +00:00
|
|
|
if !scanner.CaseSensitiveFs {
|
|
|
|
// #1426 - if file exists but is a case-insensitive match for the
|
|
|
|
// original filename, then treat it as a move
|
|
|
|
if exists && strings.EqualFold(path, s.Path) {
|
|
|
|
exists = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
logger.Infof("%s already exists. Duplicate of %s", path, s.Path)
|
|
|
|
} else {
|
|
|
|
logger.Infof("%s already exists. Updating path...", path)
|
|
|
|
scenePartial := models.ScenePartial{
|
|
|
|
ID: s.ID,
|
|
|
|
Path: &path,
|
|
|
|
Interactive: &interactive,
|
|
|
|
}
|
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.TxnManager.WithTxn(ctx, func(r models.Repository) error {
|
2021-10-14 23:39:48 +00:00
|
|
|
_, err := r.Scene().Update(scenePartial)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner.makeScreenshots(path, nil, sceneHash)
|
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
|
|
|
scanner.PluginCache.ExecutePostHooks(ctx, s.ID, plugin.SceneUpdatePost, nil, nil)
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.Infof("%s doesn't exist. Creating new item...", path)
|
|
|
|
currentTime := time.Now()
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
videoFile, err := scanner.VideoFileCreator.NewVideoFile(path)
|
2021-10-14 23:39:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
title := filepath.Base(path)
|
|
|
|
if scanner.StripFileExtension {
|
|
|
|
title = stripExtension(title)
|
|
|
|
}
|
|
|
|
|
|
|
|
if scanner.UseFileMetadata && videoFile.Title != "" {
|
|
|
|
title = videoFile.Title
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newScene := models.Scene{
|
|
|
|
Checksum: sql.NullString{String: checksum, Valid: checksum != ""},
|
|
|
|
OSHash: sql.NullString{String: oshash, Valid: oshash != ""},
|
|
|
|
Path: path,
|
|
|
|
FileModTime: models.NullSQLiteTimestamp{
|
|
|
|
Timestamp: scanned.FileModTime,
|
|
|
|
Valid: true,
|
|
|
|
},
|
2022-04-18 00:50:10 +00:00
|
|
|
Title: sql.NullString{String: title, Valid: true},
|
2021-10-14 23:39:48 +00:00
|
|
|
CreatedAt: models.SQLiteTimestamp{Timestamp: currentTime},
|
|
|
|
UpdatedAt: models.SQLiteTimestamp{Timestamp: currentTime},
|
|
|
|
Interactive: interactive,
|
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
if err := videoFileToScene(&newScene, videoFile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-14 23:39:48 +00:00
|
|
|
|
|
|
|
if scanner.UseFileMetadata {
|
|
|
|
newScene.Details = sql.NullString{String: videoFile.Comment, Valid: true}
|
2021-12-19 03:03:46 +00:00
|
|
|
_ = newScene.Date.Scan(videoFile.CreationTime)
|
2021-10-14 23:39:48 +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
|
|
|
if err := scanner.TxnManager.WithTxn(ctx, func(r models.Repository) error {
|
2021-10-14 23:39:48 +00:00
|
|
|
var err error
|
|
|
|
retScene, err = r.Scene().Create(newScene)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner.makeScreenshots(path, videoFile, sceneHash)
|
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
|
|
|
scanner.PluginCache.ExecutePostHooks(ctx, retScene.ID, plugin.SceneCreatePost, nil, nil)
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retScene, nil
|
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
func stripExtension(path string) string {
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
return strings.TrimSuffix(path, ext)
|
|
|
|
}
|
|
|
|
|
|
|
|
func videoFileToScene(s *models.Scene, videoFile *ffmpeg.VideoFile) error {
|
|
|
|
container, err := ffmpeg.MatchContainer(videoFile.Container, s.Path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("matching container: %w", err)
|
|
|
|
}
|
2021-10-14 23:39:48 +00:00
|
|
|
|
|
|
|
s.Duration = sql.NullFloat64{Float64: videoFile.Duration, Valid: true}
|
|
|
|
s.VideoCodec = sql.NullString{String: videoFile.VideoCodec, Valid: true}
|
|
|
|
s.AudioCodec = sql.NullString{String: videoFile.AudioCodec, Valid: true}
|
|
|
|
s.Format = sql.NullString{String: string(container), Valid: true}
|
|
|
|
s.Width = sql.NullInt64{Int64: int64(videoFile.Width), Valid: true}
|
|
|
|
s.Height = sql.NullInt64{Int64: int64(videoFile.Height), Valid: true}
|
|
|
|
s.Framerate = sql.NullFloat64{Float64: videoFile.FrameRate, Valid: true}
|
|
|
|
s.Bitrate = sql.NullInt64{Int64: videoFile.Bitrate, Valid: true}
|
|
|
|
s.Size = sql.NullString{String: strconv.FormatInt(videoFile.Size, 10), Valid: true}
|
2022-04-18 00:50:10 +00:00
|
|
|
|
|
|
|
return nil
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (scanner *Scanner) makeScreenshots(path string, probeResult *ffmpeg.VideoFile, checksum string) {
|
|
|
|
thumbPath := scanner.Paths.Scene.GetThumbnailScreenshotPath(checksum)
|
|
|
|
normalPath := scanner.Paths.Scene.GetScreenshotPath(checksum)
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
thumbExists, _ := fsutil.FileExists(thumbPath)
|
|
|
|
normalExists, _ := fsutil.FileExists(normalPath)
|
2021-10-14 23:39:48 +00:00
|
|
|
|
|
|
|
if thumbExists && normalExists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if probeResult == nil {
|
|
|
|
var err error
|
2022-04-18 00:50:10 +00:00
|
|
|
probeResult, err = scanner.VideoFileCreator.NewVideoFile(path)
|
2021-10-14 23:39:48 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Infof("Regenerating images for %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !thumbExists {
|
|
|
|
logger.Debugf("Creating thumbnail for %s", path)
|
2022-04-18 00:50:10 +00:00
|
|
|
if err := scanner.Screenshotter.GenerateThumbnail(context.TODO(), probeResult, checksum); err != nil {
|
|
|
|
logger.Errorf("Error creating thumbnail for %s: %v", err)
|
|
|
|
}
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !normalExists {
|
|
|
|
logger.Debugf("Creating screenshot for %s", path)
|
2022-04-18 00:50:10 +00:00
|
|
|
if err := scanner.Screenshotter.GenerateScreenshot(context.TODO(), probeResult, checksum); err != nil {
|
|
|
|
logger.Errorf("Error creating screenshot for %s: %v", err)
|
|
|
|
}
|
2021-10-14 23:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInteractive(path string) bool {
|
2022-03-17 00:33:59 +00:00
|
|
|
_, err := os.Stat(GetFunscriptPath(path))
|
2021-10-14 23:39:48 +00:00
|
|
|
return err == nil
|
|
|
|
}
|