Skip to content

Commit 72206ac

Browse files
authored
feat(strm): keep local download file (#1707)
1 parent 62dedb2 commit 72206ac

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

drivers/strm/hook.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func UpdateLocalStrm(ctx context.Context, path string, objs []model.Obj) {
2727
localPath := stdpath.Join(localParentPath, obj.GetName())
2828
generateStrm(ctx, driver, obj, localPath)
2929
}
30-
deleteExtraFiles(localParentPath, objs)
30+
deleteExtraFiles(driver, localParentPath, objs)
3131
}
3232

3333
_ = strmTrie.VisitPrefixes(patricia.Prefix(path), func(needPathPrefix patricia.Prefix, item patricia.Item) error {
@@ -129,23 +129,37 @@ func generateStrm(ctx context.Context, driver *Strm, obj model.Obj, localPath st
129129
}
130130
}
131131

132-
func deleteExtraFiles(localPath string, objs []model.Obj) {
132+
func deleteExtraFiles(driver *Strm, localPath string, objs []model.Obj) {
133133
localFiles, err := getLocalFiles(localPath)
134134
if err != nil {
135135
log.Errorf("Failed to read local files from %s: %v", localPath, err)
136136
return
137137
}
138138

139139
objsSet := make(map[string]struct{})
140+
objsBaseNameSet := make(map[string]struct{})
140141
for _, obj := range objs {
141142
if obj.IsDir() {
142143
continue
143144
}
144-
objsSet[stdpath.Join(localPath, obj.GetName())] = struct{}{}
145+
objName := obj.GetName()
146+
objsSet[stdpath.Join(localPath, objName)] = struct{}{}
147+
148+
objBaseName := strings.TrimSuffix(objName, utils.SourceExt(objName))
149+
objsBaseNameSet[stdpath.Join(localPath, objBaseName[:len(objBaseName)-1])] = struct{}{}
145150
}
146151

147152
for _, localFile := range localFiles {
148153
if _, exists := objsSet[localFile]; !exists {
154+
ext := utils.Ext(localFile)
155+
localFileName := stdpath.Base(localFile)
156+
localFileBaseName := strings.TrimSuffix(localFile, utils.SourceExt(localFileName))
157+
_, nameExists := objsBaseNameSet[localFileBaseName[:len(localFileBaseName)-1]]
158+
_, downloadFile := driver.downloadSuffix[ext]
159+
if driver.KeepLocalDownloadFile && nameExists && downloadFile {
160+
continue
161+
}
162+
149163
err := os.Remove(localFile)
150164
if err != nil {
151165
log.Errorf("Failed to delete file: %s, error: %v\n", localFile, err)

drivers/strm/meta.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ import (
66
)
77

88
type Addition struct {
9-
Paths string `json:"paths" required:"true" type:"text"`
10-
SiteUrl string `json:"siteUrl" type:"text" required:"false" help:"The prefix URL of the strm file"`
11-
PathPrefix string `json:"PathPrefix" type:"text" required:"false" default:"/d" help:"Path prefix"`
12-
DownloadFileTypes string `json:"downloadFileTypes" type:"text" default:"ass,srt,vtt,sub,strm" required:"false" help:"Files need to download with strm (usally subtitles)"`
13-
FilterFileTypes string `json:"filterFileTypes" type:"text" default:"mp4,mkv,flv,avi,wmv,ts,rmvb,webm,mp3,flac,aac,wav,ogg,m4a,wma,alac" required:"false" help:"Supports suffix name of strm file"`
14-
EncodePath bool `json:"encodePath" default:"true" required:"true" help:"encode the path in the strm file"`
15-
WithoutUrl bool `json:"withoutUrl" default:"false" help:"strm file content without URL prefix"`
16-
SaveStrmToLocal bool `json:"SaveStrmToLocal" default:"false" help:"save strm file locally"`
17-
SaveStrmLocalPath string `json:"SaveStrmLocalPath" type:"text" help:"save strm file local path"`
18-
Version int
9+
Paths string `json:"paths" required:"true" type:"text"`
10+
SiteUrl string `json:"siteUrl" type:"text" required:"false" help:"The prefix URL of the strm file"`
11+
PathPrefix string `json:"PathPrefix" type:"text" required:"false" default:"/d" help:"Path prefix"`
12+
DownloadFileTypes string `json:"downloadFileTypes" type:"text" default:"ass,srt,vtt,sub,strm" required:"false" help:"Files need to download with strm (usally subtitles)"`
13+
FilterFileTypes string `json:"filterFileTypes" type:"text" default:"mp4,mkv,flv,avi,wmv,ts,rmvb,webm,mp3,flac,aac,wav,ogg,m4a,wma,alac" required:"false" help:"Supports suffix name of strm file"`
14+
EncodePath bool `json:"encodePath" default:"true" required:"true" help:"encode the path in the strm file"`
15+
WithoutUrl bool `json:"withoutUrl" default:"false" help:"strm file content without URL prefix"`
16+
SaveStrmToLocal bool `json:"SaveStrmToLocal" default:"false" help:"save strm file locally"`
17+
SaveStrmLocalPath string `json:"SaveStrmLocalPath" type:"text" help:"save strm file local path"`
18+
KeepLocalDownloadFile bool `json:"KeepLocalDownloadFile" default:"false" help:"keep local download files"`
19+
Version int
1920
}
2021

2122
var config = driver.Config{

0 commit comments

Comments
 (0)