feat: only accept video files and subtitles of known formats

This commit is contained in:
Simon Ding
2024-11-20 12:03:56 +08:00
parent c833f6fab6
commit 7b6dba1afe
9 changed files with 164 additions and 34 deletions

View File

@@ -10,20 +10,22 @@ import (
"github.com/gabriel-vasile/mimetype"
)
func NewAlist(cfg *alist.Config, dir string) (*Alist, error) {
func NewAlist(cfg *alist.Config, dir string, videoFormats []string, subtitleFormats []string) (*Alist, error) {
cl := alist.New(cfg)
_, err := cl.Login()
if err != nil {
return nil, err
}
return &Alist{baseDir: dir, cfg: cfg, client: cl}, nil
return &Alist{baseDir: dir, cfg: cfg, client: cl, videoFormats: videoFormats, subtitleFormats: subtitleFormats}, nil
}
type Alist struct {
baseDir string
cfg *alist.Config
client *alist.Client
progresser func() float64
baseDir string
cfg *alist.Config
client *alist.Client
progresser func() float64
videoFormats []string
subtitleFormats []string
}
func (a *Alist) Move(src, dest string) error {
@@ -34,7 +36,7 @@ func (a *Alist) Move(src, dest string) error {
}
func (a *Alist) Copy(src, dest string) error {
b, err := NewBase(src)
b, err := NewBase(src, a.videoFormats, a.subtitleFormats)
if err != nil {
return err
}

View File

@@ -7,10 +7,12 @@ import (
"path/filepath"
"polaris/log"
"polaris/pkg/utils"
"strings"
"github.com/gabriel-vasile/mimetype"
"github.com/pkg/errors"
)
type Storage interface {
Move(src, dest string) error
Copy(src, dest string) error
@@ -20,22 +22,66 @@ type Storage interface {
UploadProgress() float64
}
type uploadFunc func(destPath string, destInfo fs.FileInfo, srcReader io.Reader, mimeType *mimetype.MIME) error
type Base struct {
src string
totalSize int64
uploadedSize int64
src string
videoFormats []string
subtitleFormats []string
totalSize int64
uploadedSize int64
}
func NewBase(src string) (*Base, error) {
b := &Base{src: src}
func NewBase(src string, videoFormats []string, subtitleFormats []string) (*Base, error) {
b := &Base{src: src, videoFormats: videoFormats, subtitleFormats: subtitleFormats}
err := b.calculateSize()
return b, err
}
func (b *Base) checkVideoFilesExist() bool {
if len(b.videoFormats) == 0 { // do not check
return true
}
hasVideo := false
filepath.Walk(b.src, func(path string, info fs.FileInfo, err error) error {
ext := filepath.Ext(strings.ToLower(info.Name()))
for _, f := range b.videoFormats {
if f == ext {
hasVideo = true
}
}
return nil
})
return hasVideo
}
func (b *Base) isFileNeeded(name string) bool {
ext := filepath.Ext(strings.ToLower(name))
if len(b.videoFormats) == 0 {
return true
} else {
for _, f := range b.videoFormats {
if f == ext {
return true
}
}
}
if len(b.subtitleFormats) > 0 {
for _, f := range b.subtitleFormats {
if f == ext {
return true
}
}
}
return false
}
func (b *Base) Upload(destDir string, tryLink, detectMime, changeMediaHash bool, upload uploadFunc, mkdir func(string) error) error {
if !b.checkVideoFilesExist() {
return errors.Errorf("no video file")
}
os.MkdirAll(destDir, os.ModePerm)
targetBase := filepath.Join(destDir, filepath.Base(b.src)) //文件的场景,要加上文件名, move filename ./dir/
@@ -61,6 +107,10 @@ func (b *Base) Upload(destDir string, tryLink, detectMime, changeMediaHash bool,
if info.IsDir() {
mkdir(destName)
} else { //is file
if !b.isFileNeeded(info.Name()) {
log.Debugf("file is not needed, skip: %s", info.Name())
return nil
}
if tryLink {
if err := os.Link(path, destName); err == nil {
return nil //link success
@@ -116,12 +166,11 @@ func (b *Base) calculateSize() error {
}
func (b *Base) Progress() float64 {
return float64(b.uploadedSize)/float64(b.totalSize)
return float64(b.uploadedSize) / float64(b.totalSize)
}
type progressReader struct {
R io.Reader
R io.Reader
Add func(int)
}
@@ -129,4 +178,4 @@ func (pr *progressReader) Read(p []byte) (int, error) {
n, err := pr.R.Read(p)
pr.Add(n)
return n, err
}
}

View File

@@ -11,18 +11,19 @@ import (
"github.com/pkg/errors"
)
func NewLocalStorage(dir string) (*LocalStorage, error) {
func NewLocalStorage(dir string, videoFormats []string, subtitleFormats []string) (*LocalStorage, error) {
os.MkdirAll(dir, 0655)
return &LocalStorage{dir: dir}, nil
return &LocalStorage{dir: dir, videoFormats: videoFormats, subtitleFormats: subtitleFormats}, nil
}
type LocalStorage struct {
dir string
dir string
videoFormats []string
subtitleFormats []string
}
func (l *LocalStorage) Copy(src, destDir string) error {
b, err := NewBase(src)
b, err := NewBase(src, l.videoFormats, l.subtitleFormats)
if err != nil {
return err
}
@@ -69,4 +70,4 @@ func (l *LocalStorage) WriteFile(name string, data []byte) error {
func (l *LocalStorage) UploadProgress() float64 {
return 0
}
}

View File

@@ -16,10 +16,12 @@ type WebdavStorage struct {
fs *gowebdav.Client
dir string
changeMediaHash bool
progresser func() float64
progresser func() float64
videoFormats []string
subtitleFormats []string
}
func NewWebdavStorage(url, user, password, path string, changeMediaHash bool) (*WebdavStorage, error) {
func NewWebdavStorage(url, user, password, path string, changeMediaHash bool, videoFormats []string, subtitleFormats []string) (*WebdavStorage, error) {
c := gowebdav.NewClient(url, user, password)
if err := c.Connect(); err != nil {
return nil, errors.Wrap(err, "connect webdav")
@@ -27,11 +29,13 @@ func NewWebdavStorage(url, user, password, path string, changeMediaHash bool) (*
return &WebdavStorage{
fs: c,
dir: path,
videoFormats: videoFormats,
subtitleFormats: subtitleFormats,
}, nil
}
func (w *WebdavStorage) Copy(local, remoteDir string) error {
b, err := NewBase(local)
b, err := NewBase(local, w.videoFormats, w.subtitleFormats)
if err != nil {
return err
}
@@ -80,4 +84,4 @@ func (w *WebdavStorage) UploadProgress() float64 {
return 0
}
return w.progresser()
}
}