Skip to content

Commit 2cd08c2

Browse files
author
cyk
committed
fix: 修复500 panic和NaN问题
- 修复HashInfo nil pointer导致的500 panic (fsread.go) - 修复StorageDetails为nil导致的NaN显示 (storage.go, op/storage.go) - 添加DiskUsage.MarshalJSON()确保返回used_space字段
1 parent ffe6514 commit 2cd08c2

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

internal/model/storage.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package model
22

33
import (
4+
"encoding/json"
45
"time"
56
)
67

@@ -62,6 +63,21 @@ type DiskUsage struct {
6263
FreeSpace uint64 `json:"free_space"`
6364
}
6465

66+
func (d DiskUsage) UsedSpace() uint64 {
67+
if d.TotalSpace > d.FreeSpace {
68+
return d.TotalSpace - d.FreeSpace
69+
}
70+
return 0
71+
}
72+
73+
func (d DiskUsage) MarshalJSON() ([]byte, error) {
74+
return json.Marshal(map[string]interface{}{
75+
"total_space": d.TotalSpace,
76+
"free_space": d.FreeSpace,
77+
"used_space": d.UsedSpace(),
78+
})
79+
}
80+
6581
type StorageDetails struct {
6682
DiskUsage
6783
}

internal/op/storage.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func GetStorageVirtualFilesWithDetailsByPath(ctx context.Context, prefix string,
355355
ret := &model.ObjStorageDetails{
356356
Obj: obj,
357357
StorageDetailsWithName: model.StorageDetailsWithName{
358-
StorageDetails: nil,
358+
StorageDetails: &model.StorageDetails{}, // 初始化为空结构体而不是nil
359359
DriverName: d.Config().Name,
360360
},
361361
}
@@ -371,7 +371,9 @@ func GetStorageVirtualFilesWithDetailsByPath(ctx context.Context, prefix string,
371371
}(d)
372372
select {
373373
case r := <-resultChan:
374-
ret.StorageDetails = r
374+
if r != nil {
375+
ret.StorageDetails = r
376+
}
375377
case <-time.After(time.Second):
376378
}
377379
return ret

server/handles/fsread.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,18 @@ func toObjsResp(objs []model.Obj, parent string, encrypt bool) []ObjResp {
231231
for _, obj := range objs {
232232
thumb, _ := model.GetThumb(obj)
233233
mountDetails, _ := model.GetStorageDetails(obj)
234+
hashInfo := obj.GetHash().Export()
235+
if hashInfo == nil {
236+
hashInfo = make(map[*utils.HashType]string)
237+
}
234238
resp = append(resp, ObjResp{
235239
Name: obj.GetName(),
236240
Size: obj.GetSize(),
237241
IsDir: obj.IsDir(),
238242
Modified: obj.ModTime(),
239243
Created: obj.CreateTime(),
240244
HashInfoStr: obj.GetHash().String(),
241-
HashInfo: obj.GetHash().Export(),
245+
HashInfo: hashInfo,
242246
Sign: common.Sign(obj, parent, encrypt),
243247
Thumb: thumb,
244248
Type: utils.GetObjType(obj.GetName(), obj.IsDir()),

0 commit comments

Comments
 (0)