Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions drivers/alias/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ func (d *Alias) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([
for _, obj := range objMap {
objs = append(objs, obj)
}
if d.OrderBy == "" {
sort := getAllSort(dirs)
if sort.OrderBy != "" {
model.SortFiles(objs, sort.OrderBy, sort.OrderDirection)
}
if d.ExtractFolder == "" && sort.ExtractFolder != "" {
model.ExtractFolder(objs, sort.ExtractFolder)
}
}
return objs, nil
}

Expand Down Expand Up @@ -276,20 +285,36 @@ func (d *Alias) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (
}, nil
}

reqPath := d.getBalancedPath(ctx, file)
link, fi, err := d.link(ctx, reqPath, args)
var link *model.Link
var fi model.Obj
var err error
files := file.(BalancedObjs)
if d.ReadConflictPolicy == RandomBalancedRP || d.ReadConflictPolicy == AllRWP {
rand.Shuffle(len(files), func(i, j int) {
files[i], files[j] = files[j], files[i]
})
}
for _, f := range files {
if f == nil {
continue
}
link, fi, err = d.link(ctx, f.GetPath(), args)
if err == nil {
if link == nil {
// 重定向且需要通过代理
return &model.Link{
URL: fmt.Sprintf("%s/p%s?sign=%s",
common.GetApiUrl(ctx),
utils.EncodePath(f.GetPath(), true),
sign.Sign(f.GetPath())),
}, nil
}
break
}
}
if err != nil {
return nil, err
}
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential nil pointer dereference. If all files in the BalancedObjs array are nil or all attempts to get a link fail, the code will reach line 318 with 'link' still being nil, causing a panic when dereferencing it. Add a check to ensure 'link' is not nil before dereferencing, or handle this edge case appropriately.

Suggested change
}
}
if link == nil {
return nil, errors.New("alias: no valid link obtained for balanced object")
}

Copilot uses AI. Check for mistakes.
if link == nil {
// 重定向且需要通过代理
return &model.Link{
URL: fmt.Sprintf("%s/p%s?sign=%s",
common.GetApiUrl(ctx),
utils.EncodePath(reqPath, true),
sign.Sign(reqPath)),
}, nil
}
resultLink := *link // 复制一份,避免修改到原始link
resultLink.SyncClosers = utils.NewSyncClosers(link)
if args.Redirect {
Expand Down
40 changes: 40 additions & 0 deletions drivers/alias/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,43 @@ func (d *Alias) extract(ctx context.Context, reqPath string, args model.ArchiveI
link, _, err := op.DriverExtract(ctx, storage, reqActualPath, args)
return link, err
}

func getAllSort(dirs []model.Obj) model.Sort {
ret := model.Sort{}
noSort := false
noExtractFolder := false
for _, dir := range dirs {
if dir == nil {
continue
}
storage, err := fs.GetStorage(dir.GetPath(), &fs.GetStoragesArgs{})
if err != nil {
continue
}
if !noSort && storage.GetStorage().OrderBy != "" {
if ret.OrderBy == "" {
ret.OrderBy = storage.GetStorage().OrderBy
ret.OrderDirection = storage.GetStorage().OrderDirection
if ret.OrderDirection == "" {
ret.OrderDirection = "asc"
}
} else if ret.OrderBy != storage.GetStorage().OrderBy || ret.OrderDirection != storage.GetStorage().OrderDirection {
ret.OrderBy = ""
ret.OrderDirection = ""
noSort = true
}
}
if !noExtractFolder && storage.GetStorage().ExtractFolder != "" {
if ret.ExtractFolder == "" {
ret.ExtractFolder = storage.GetStorage().ExtractFolder
} else if ret.ExtractFolder != storage.GetStorage().ExtractFolder {
ret.ExtractFolder = ""
noExtractFolder = true
}
}
if !noSort && !noExtractFolder {
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The early break condition is incorrect. The loop should only break when BOTH noSort and noExtractFolder are true, not when both are false. Currently, the loop breaks immediately after processing the first storage with OrderBy and ExtractFolder settings, preventing it from checking if other storages have conflicting settings. This should be changed to check if both flags are true.

Suggested change
if !noSort && !noExtractFolder {
if noSort && noExtractFolder {

Copilot uses AI. Check for mistakes.
break
}
}
return ret
}