feat: check all in dir

This commit is contained in:
Simon Ding
2024-07-14 20:24:33 +08:00
parent 6a5ff9707e
commit 6f91157d40
5 changed files with 122 additions and 10 deletions

View File

@@ -2,16 +2,21 @@ package server
import (
"path/filepath"
"polaris/ent"
storage1 "polaris/ent/storage"
"polaris/log"
"polaris/pkg"
"polaris/pkg/storage"
storage1 "polaris/ent/storage"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
)
func (s *Server) scheduler() {
s.mustAddCron("@every 1m", s.checkTasks)
s.mustAddCron("@every 10m", s.checkAllFiles)
s.cron.Start()
}
@@ -85,7 +90,75 @@ func (s *Server) updateSeriesEpisodes(seriesId int) {
}
func (s *Server) checkFileExists() {}
func (s *Server) checkAllFiles() {
var tvs = s.db.GetWatchlist()
for _, se := range tvs {
if err := s.checkFileExists(se); err != nil {
log.Errorf("check files for %s error: %v", se.NameCn, err)
}
}
}
func (s *Server) checkFileExists(series *ent.Series) error{
log.Infof("check files in directory: %s", series.TargetDir)
st := s.db.GetStorage(series.StorageID)
var storageImpl storage.Storage
switch st.Implementation {
case storage1.ImplementationLocal:
ls := st.ToLocalSetting()
storageImpl1, err := storage.NewLocalStorage(ls.Path)
if err != nil {
return errors.Wrap(err, "new local")
}
storageImpl = storageImpl1
case storage1.ImplementationWebdav:
ws := st.ToWebDavSetting()
storageImpl1, err := storage.NewWebdavStorage(ws.Path, ws.User, ws.Password)
if err != nil {
return errors.Wrap(err, "new webdav")
}
storageImpl = storageImpl1
}
files, err := storageImpl.ReadDir(series.TargetDir)
if err != nil {
return errors.Wrapf(err, "read dir %s", series.TargetDir)
}
numRe := regexp.MustCompile("[0-9]+")
epRe := regexp.MustCompile("E[0-9]+")
for _, in := range files {
if !in.IsDir() {//season dir, ignore file
continue
}
nums := numRe.FindAllString(in.Name(), -1)
if len(nums) == 0 {
continue
}
seasonNum := nums[0]
seasonNum1, _ := strconv.Atoi(seasonNum)
dir := filepath.Join(series.TargetDir, in.Name())
epFiles, err := storageImpl.ReadDir(dir)
if err != nil {
log.Errorf("read dir %s error: %v", dir, err)
continue
}
for _, ep := range epFiles {
match := epRe.FindAllString(ep.Name(), -1)
if len(match) == 0 {
continue
}
epNum := strings.TrimPrefix(match[0], "E")
epNum1, _ := strconv.Atoi(epNum)
err := s.db.UpdateEpisodeFile(series.ID, seasonNum1, epNum1, filepath.Join(in.Name(), ep.Name()))
if err != nil {
log.Error("update episode: %v", err)
}
}
}
return nil
}
type Task struct {
Processing bool