Skip to content

Commit 60a489e

Browse files
authored
feat(archive): support non-overwrite decompress (#1701)
1 parent b22e211 commit 60a489e

File tree

8 files changed

+25
-25
lines changed

8 files changed

+25
-25
lines changed

drivers/openlist/driver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ func (d *OpenList) ArchiveDecompress(ctx context.Context, srcObj, dstDir model.O
360360
Name: []string{name},
361361
PutIntoNewDir: args.PutIntoNewDir,
362362
SrcDir: dir,
363+
Overwrite: args.Overwrite,
363364
})
364365
})
365366
return err

drivers/openlist/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,5 @@ type DecompressReq struct {
167167
Name []string `json:"name"`
168168
PutIntoNewDir bool `json:"put_into_new_dir"`
169169
SrcDir string `json:"src_dir"`
170+
Overwrite bool `json:"overwrite"`
170171
}

drivers/url_tree/meta.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var config = driver.Config{
2020
LocalSort: true,
2121
NoCache: true,
2222
CheckStatus: true,
23+
OnlyIndices: true,
2324
}
2425

2526
func init() {

internal/driver/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type Config struct {
2222
// - LinkCacheNone: no extra info added to cache key (default)
2323
// - flags (OR-able) can add more attributes to cache key (IP, UA, ...)
2424
LinkCacheMode `json:"-"`
25+
// if the driver only store indices of files (e.g. UrlTree)
26+
OnlyIndices bool `json:"only_indices"`
2527
}
2628
type LinkCacheMode int8
2729

internal/fs/archive.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func (t *ArchiveDownloadTask) RunWithoutPushUploadTask() (*ArchiveContentUploadT
125125
DstActualPath: t.DstActualPath,
126126
dstStorage: t.DstStorage,
127127
DstStorageMp: t.DstStorageMp,
128+
overwrite: t.Overwrite,
128129
}
129130
return uploadTask, nil
130131
}
@@ -142,6 +143,7 @@ type ArchiveContentUploadTask struct {
142143
DstStorageMp string
143144
finalized bool
144145
groupID string
146+
overwrite bool
145147
}
146148

147149
func (t *ArchiveContentUploadTask) GetName() string {
@@ -232,6 +234,7 @@ func (t *ArchiveContentUploadTask) RunWithNextTaskCallback(f func(nextTask *Arch
232234
dstStorage: t.dstStorage,
233235
DstStorageMp: t.DstStorageMp,
234236
groupID: t.groupID,
237+
overwrite: t.overwrite,
235238
})
236239
if err != nil {
237240
es = stderrors.Join(es, err)
@@ -241,6 +244,12 @@ func (t *ArchiveContentUploadTask) RunWithNextTaskCallback(f func(nextTask *Arch
241244
return es
242245
}
243246
} else {
247+
if !t.overwrite {
248+
dstPath := stdpath.Join(t.DstActualPath, t.ObjName)
249+
if res, _ := op.Get(t.Ctx(), t.dstStorage, dstPath); res != nil {
250+
return errs.ObjectAlreadyExists
251+
}
252+
}
244253
file, err := os.Open(t.FilePath)
245254
if err != nil {
246255
return err

internal/model/args.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type ArchiveDecompressArgs struct {
7777
ArchiveInnerArgs
7878
CacheFull bool
7979
PutIntoNewDir bool
80+
Overwrite bool
8081
}
8182

8283
type SharingListArgs struct {

internal/op/fs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file mod
509509
return errors.WithMessagef(errs.StorageNotInit, "storage status: %s", storage.GetStorage().Status)
510510
}
511511
// UrlTree PUT
512-
if storage.GetStorage().Driver == "UrlTree" {
512+
if storage.Config().OnlyIndices {
513513
var link string
514514
dstDirPath, link = urlTreeSplitLineFormPath(stdpath.Join(dstDirPath, file.GetName()))
515515
file = &stream.FileStream{Obj: &model.Object{Name: link}}

server/handles/archive.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handles
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"io"
76
stdpath "path"
@@ -229,30 +228,15 @@ func FsArchiveList(c *gin.Context, req *ArchiveListReq, user *model.User) {
229228
})
230229
}
231230

232-
type StringOrArray []string
233-
234-
func (s *StringOrArray) UnmarshalJSON(data []byte) error {
235-
var value string
236-
if err := json.Unmarshal(data, &value); err == nil {
237-
*s = []string{value}
238-
return nil
239-
}
240-
var sliceValue []string
241-
if err := json.Unmarshal(data, &sliceValue); err != nil {
242-
return err
243-
}
244-
*s = sliceValue
245-
return nil
246-
}
247-
248231
type ArchiveDecompressReq struct {
249-
SrcDir string `json:"src_dir" form:"src_dir"`
250-
DstDir string `json:"dst_dir" form:"dst_dir"`
251-
Name StringOrArray `json:"name" form:"name"`
252-
ArchivePass string `json:"archive_pass" form:"archive_pass"`
253-
InnerPath string `json:"inner_path" form:"inner_path"`
254-
CacheFull bool `json:"cache_full" form:"cache_full"`
255-
PutIntoNewDir bool `json:"put_into_new_dir" form:"put_into_new_dir"`
232+
SrcDir string `json:"src_dir" form:"src_dir"`
233+
DstDir string `json:"dst_dir" form:"dst_dir"`
234+
Name []string `json:"name" form:"name"`
235+
ArchivePass string `json:"archive_pass" form:"archive_pass"`
236+
InnerPath string `json:"inner_path" form:"inner_path"`
237+
CacheFull bool `json:"cache_full" form:"cache_full"`
238+
PutIntoNewDir bool `json:"put_into_new_dir" form:"put_into_new_dir"`
239+
Overwrite bool `json:"overwrite" form:"overwrite"`
256240
}
257241

258242
func FsArchiveDecompress(c *gin.Context) {
@@ -295,6 +279,7 @@ func FsArchiveDecompress(c *gin.Context) {
295279
},
296280
CacheFull: req.CacheFull,
297281
PutIntoNewDir: req.PutIntoNewDir,
282+
Overwrite: req.Overwrite,
298283
})
299284
if e != nil {
300285
if errors.Is(e, errs.WrongArchivePassword) {

0 commit comments

Comments
 (0)