2020-10-12 23:12:46 +00:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"strings"
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/internal/manager/config"
|
|
|
|
"github.com/stashapp/stash/pkg/file"
|
|
|
|
|
2020-10-12 23:12:46 +00:00
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
func walkGalleryZip(path string, walkFunc func(file *zip.File) error) error {
|
|
|
|
readCloser, err := zip.OpenReader(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-10 09:19:13 +00:00
|
|
|
defer readCloser.Close()
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2022-02-16 02:46:35 +00:00
|
|
|
excludeImgRegex := generateRegexps(config.GetInstance().GetImageExcludes())
|
|
|
|
|
|
|
|
for _, f := range readCloser.File {
|
|
|
|
if f.FileInfo().IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(f.Name, "__MACOSX") {
|
2020-10-12 23:12:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-16 02:46:35 +00:00
|
|
|
if !isImage(f.Name) {
|
2020-10-12 23:12:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-16 02:46:35 +00:00
|
|
|
if matchFileRegex(file.ZipFile(path, f).Path(), excludeImgRegex) {
|
2020-10-12 23:12:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-16 02:46:35 +00:00
|
|
|
err := walkFunc(f)
|
2020-10-12 23:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func countImagesInZip(path string) int {
|
|
|
|
ret := 0
|
2021-09-20 23:34:25 +00:00
|
|
|
err := walkGalleryZip(path, func(file *zip.File) error {
|
2020-10-12 23:12:46 +00:00
|
|
|
ret++
|
|
|
|
return nil
|
|
|
|
})
|
2021-09-20 23:34:25 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Warnf("Error while walking gallery zip: %v", err)
|
|
|
|
}
|
2020-10-12 23:12:46 +00:00
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|