stash/internal/manager/checksum.go

81 lines
2.4 KiB
Go
Raw Normal View History

2020-08-06 01:21:14 +00:00
package manager
import (
"context"
2020-08-06 01:21:14 +00:00
"errors"
"github.com/stashapp/stash/internal/manager/config"
2020-08-06 01:21:14 +00:00
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/txn"
2020-08-06 01:21:14 +00:00
)
type SceneCounter interface {
Count(ctx context.Context) (int, error)
}
func setInitialMD5Config(ctx context.Context, txnManager txn.Manager, counter SceneCounter) {
2020-08-06 01:21:14 +00:00
// if there are no scene files in the database, then default the
// VideoFileNamingAlgorithm config setting to oshash and calculateMD5 to
// false, otherwise set them to true for backwards compatibility purposes
var count int
if err := txn.WithTxn(ctx, txnManager, func(ctx context.Context) error {
var err error
count, err = counter.Count(ctx)
return err
}); err != nil {
2020-08-06 01:21:14 +00:00
logger.Errorf("Error while counting scenes: %s", err.Error())
return
}
usingMD5 := count != 0
defaultAlgorithm := models.HashAlgorithmOshash
if usingMD5 {
defaultAlgorithm = models.HashAlgorithmMd5
}
config := config.GetInstance()
config.SetChecksumDefaultValues(defaultAlgorithm, usingMD5)
2020-08-06 01:21:14 +00:00
if err := config.Write(); err != nil {
logger.Errorf("Error while writing configuration file: %s", err.Error())
}
}
type SceneMissingHashCounter interface {
CountMissingChecksum(ctx context.Context) (int, error)
CountMissingOSHash(ctx context.Context) (int, error)
}
2020-08-06 01:21:14 +00:00
// ValidateVideoFileNamingAlgorithm validates changing the
// VideoFileNamingAlgorithm configuration flag.
//
// If setting VideoFileNamingAlgorithm to MD5, then this function will ensure
// that all checksum values are set on all scenes.
//
// Likewise, if VideoFileNamingAlgorithm is set to oshash, then this function
// will ensure that all oshash values are set on all scenes.
func ValidateVideoFileNamingAlgorithm(ctx context.Context, qb SceneMissingHashCounter, newValue models.HashAlgorithm) error {
2020-08-06 01:21:14 +00:00
// if algorithm is being set to MD5, then all checksums must be present
if newValue == models.HashAlgorithmMd5 {
missingMD5, err := qb.CountMissingChecksum(ctx)
if err != nil {
return err
}
2020-08-06 01:21:14 +00:00
if missingMD5 > 0 {
return errors.New("some checksums are missing on scenes. Run Scan with calculateMD5 set to true")
}
} else if newValue == models.HashAlgorithmOshash {
missingOSHash, err := qb.CountMissingOSHash(ctx)
if err != nil {
return err
}
2020-08-06 01:21:14 +00:00
if missingOSHash > 0 {
return errors.New("some oshash values are missing on scenes. Run Scan to populate")
2020-08-06 01:21:14 +00:00
}
}
2020-08-06 01:21:14 +00:00
return nil
2020-08-06 01:21:14 +00:00
}