Scan refactor (#1816)

* Add file scanner
* Scan scene changes
* Split scan files
* Generalise scan
* Refactor ffprobe
* Refactor ffmpeg encoder
* Move scene scan code to scene package
* Move matchExtension to utils
* Refactor gallery scanning
* Refactor image scanning
* Prevent race conditions on identical hashes
* Refactor image thumbnail generation
* Perform count concurrently
* Allow progress increment before total set
* Make progress updates more frequent
This commit is contained in:
WithoutPants
2021-10-15 10:39:48 +11:00
committed by GitHub
parent 3d5ee16e90
commit 39fdde273d
55 changed files with 2172 additions and 1429 deletions

View File

@@ -12,14 +12,13 @@ import (
"strings"
"time"
"github.com/stashapp/stash/pkg/file"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/utils"
_ "golang.org/x/image/webp"
)
const zipSeparator = "\x00"
func GetSourceImage(i *models.Image) (image.Image, error) {
f, err := openSourceImage(i.Path)
if err != nil {
@@ -67,17 +66,6 @@ func FileExists(path string) bool {
return true
}
func ZipFilename(zipFilename, filenameInZip string) string {
return zipFilename + zipSeparator + filenameInZip
}
// IsZipPath returns true if the path includes the zip separator byte,
// indicating it is within a zip file.
// TODO - this should be moved to utils
func IsZipPath(p string) bool {
return strings.Contains(p, zipSeparator)
}
type imageReadCloser struct {
src io.ReadCloser
zrc *zip.ReadCloser
@@ -102,7 +90,7 @@ func (i *imageReadCloser) Close() error {
func openSourceImage(path string) (io.ReadCloser, error) {
// may need to read from a zip file
zipFilename, filename := getFilePath(path)
zipFilename, filename := file.ZipFilePath(path)
if zipFilename != "" {
r, err := zip.OpenReader(zipFilename)
if err != nil {
@@ -134,17 +122,6 @@ func openSourceImage(path string) (io.ReadCloser, error) {
return os.Open(filename)
}
func getFilePath(path string) (zipFilename, filename string) {
nullIndex := strings.Index(path, zipSeparator)
if nullIndex != -1 {
zipFilename = path[0:nullIndex]
filename = path[nullIndex+1:]
} else {
filename = path
}
return
}
// GetFileDetails returns a pointer to an Image object with the
// width, height and size populated.
func GetFileDetails(path string) (*models.Image, error) {
@@ -203,7 +180,7 @@ func GetFileModTime(path string) (time.Time, error) {
func stat(path string) (os.FileInfo, error) {
// may need to read from a zip file
zipFilename, filename := getFilePath(path)
zipFilename, filename := file.ZipFilePath(path)
if zipFilename != "" {
r, err := zip.OpenReader(zipFilename)
if err != nil {
@@ -224,16 +201,8 @@ func stat(path string) (os.FileInfo, error) {
return os.Stat(filename)
}
// PathDisplayName converts an image path for display. It translates the zip
// file separator character into '/', since this character is also used for
// path separators within zip files. It returns the original provided path
// if it does not contain the zip file separator character.
func PathDisplayName(path string) string {
return strings.Replace(path, zipSeparator, "/", -1)
}
func Serve(w http.ResponseWriter, r *http.Request, path string) {
zipFilename, _ := getFilePath(path)
zipFilename, _ := file.ZipFilePath(path)
w.Header().Add("Cache-Control", "max-age=604800000") // 1 Week
if zipFilename == "" {
http.ServeFile(w, r, path)
@@ -259,7 +228,7 @@ func Serve(w http.ResponseWriter, r *http.Request, path string) {
}
func IsCover(img *models.Image) bool {
_, fn := getFilePath(img.Path)
_, fn := file.ZipFilePath(img.Path)
return strings.HasSuffix(fn, "cover.jpg")
}
@@ -268,13 +237,13 @@ func GetTitle(s *models.Image) string {
return s.Title.String
}
_, fn := getFilePath(s.Path)
_, fn := file.ZipFilePath(s.Path)
return filepath.Base(fn)
}
// GetFilename gets the base name of the image file
// If stripExt is set the file extension is omitted from the name
func GetFilename(s *models.Image, stripExt bool) string {
_, fn := getFilePath(s.Path)
_, fn := file.ZipFilePath(s.Path)
return utils.GetNameFromPath(fn, stripExt)
}