Skip to content

Commit 8a4f376

Browse files
authored
feat(strm): add save local mode (#1814)
* feat(strm): add KeepSameNameOnly logic * chore(strm): skip update strm file when keepLocalDownloadFile * feat(strm): add save local mode
1 parent cc5172e commit 8a4f376

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

drivers/strm/driver.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ func (d *Strm) Init(ctx context.Context) error {
117117
d.PathPrefix = "/d"
118118
d.Version = 5
119119
}
120+
if len(d.SaveLocalMode) == 0 {
121+
d.SaveLocalMode = SaveLocalInsertMode
122+
}
120123
return nil
121124
}
122125

drivers/strm/hook.go

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package strm
22

33
import (
4+
"bytes"
45
"context"
6+
"crypto/sha256"
57
"errors"
8+
"io"
69
"os"
710
stdpath "path"
811
"strings"
@@ -90,6 +93,9 @@ func RemoveStrm(dstPath string, d *Strm) {
9093

9194
func generateStrm(ctx context.Context, driver *Strm, obj model.Obj, localPath string) {
9295
if !obj.IsDir() {
96+
if utils.Exists(localPath) && driver.SaveLocalMode == SaveLocalInsertMode {
97+
return
98+
}
9399
link, err := driver.Link(ctx, obj, model.LinkArgs{})
94100
if err != nil {
95101
log.Warnf("failed to generate strm of obj %s: failed to link: %v", localPath, err)
@@ -111,6 +117,20 @@ func generateStrm(ctx context.Context, driver *Strm, obj model.Obj, localPath st
111117
return
112118
}
113119
defer rc.Close()
120+
same, err := isSameContent(localPath, size, rc)
121+
if err != nil {
122+
log.Warnf("failed to compare content of obj %s: %v", localPath, err)
123+
return
124+
}
125+
if same {
126+
return
127+
}
128+
rc, err = rrf.RangeRead(ctx, http_range.Range{Length: -1})
129+
if err != nil {
130+
log.Warnf("failed to generate strm of obj %s: failed to reread range: %v", localPath, err)
131+
return
132+
}
133+
defer rc.Close()
114134
file, err := utils.CreateNestedFile(localPath)
115135
if err != nil {
116136
log.Warnf("failed to generate strm of obj %s: failed to create local file: %v", localPath, err)
@@ -123,7 +143,38 @@ func generateStrm(ctx context.Context, driver *Strm, obj model.Obj, localPath st
123143
}
124144
}
125145

146+
func isSameContent(localPath string, size int64, rc io.Reader) (bool, error) {
147+
info, err := os.Stat(localPath)
148+
if err != nil {
149+
if os.IsNotExist(err) {
150+
return false, nil
151+
}
152+
return false, err
153+
}
154+
155+
if info.Size() != size {
156+
return false, nil
157+
}
158+
localFile, err := os.Open(localPath)
159+
if err != nil {
160+
return false, err
161+
}
162+
defer localFile.Close()
163+
h1 := sha256.New()
164+
h2 := sha256.New()
165+
if _, err := io.Copy(h1, localFile); err != nil {
166+
return false, err
167+
}
168+
if _, err := io.Copy(h2, rc); err != nil {
169+
return false, err
170+
}
171+
return bytes.Equal(h1.Sum(nil), h2.Sum(nil)), nil
172+
}
173+
126174
func deleteExtraFiles(driver *Strm, localPath string, objs []model.Obj) {
175+
if driver.SaveLocalMode != SaveLocalSyncMode {
176+
return
177+
}
127178
localFiles, err := getLocalFiles(localPath)
128179
if err != nil {
129180
log.Errorf("Failed to read local files from %s: %v", localPath, err)
@@ -145,15 +196,6 @@ func deleteExtraFiles(driver *Strm, localPath string, objs []model.Obj) {
145196

146197
for _, localFile := range localFiles {
147198
if _, exists := objsSet[localFile]; !exists {
148-
ext := utils.Ext(localFile)
149-
localFileName := stdpath.Base(localFile)
150-
localFileBaseName := strings.TrimSuffix(localFile, utils.SourceExt(localFileName))
151-
_, nameExists := objsBaseNameSet[localFileBaseName[:len(localFileBaseName)-1]]
152-
_, downloadFile := driver.downloadSuffix[ext]
153-
if driver.KeepLocalDownloadFile && nameExists && downloadFile {
154-
continue
155-
}
156-
157199
err := os.Remove(localFile)
158200
if err != nil {
159201
log.Errorf("Failed to delete file: %s, error: %v\n", localFile, err)

drivers/strm/meta.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import (
55
"github.com/OpenListTeam/OpenList/v4/internal/op"
66
)
77

8+
const (
9+
SaveLocalInsertMode = "insert"
10+
SaveLocalUpdateMode = "update"
11+
SaveLocalSyncMode = "sync"
12+
)
13+
814
type Addition struct {
915
Paths string `json:"paths" required:"true" type:"text"`
1016
SiteUrl string `json:"siteUrl" type:"text" required:"false" help:"The prefix URL of the strm file"`
@@ -17,6 +23,7 @@ type Addition struct {
1723
SaveStrmToLocal bool `json:"SaveStrmToLocal" default:"false" help:"save strm file locally"`
1824
SaveStrmLocalPath string `json:"SaveStrmLocalPath" type:"text" help:"save strm file local path"`
1925
KeepLocalDownloadFile bool `json:"KeepLocalDownloadFile" default:"false" help:"keep local download files"`
26+
SaveLocalMode string `json:"SaveLocalMode" type:"select" help:"save strm file locally mode" options:"insert,update,sync" default:"insert"`
2027
Version int
2128
}
2229

0 commit comments

Comments
 (0)