github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/serializer/share.go (about) 1 package serializer 2 3 import ( 4 "time" 5 6 model "github.com/cloudreve/Cloudreve/v3/models" 7 "github.com/cloudreve/Cloudreve/v3/pkg/hashid" 8 ) 9 10 // Share 分享信息序列化 11 type Share struct { 12 Key string `json:"key"` 13 Locked bool `json:"locked"` 14 IsDir bool `json:"is_dir"` 15 CreateDate time.Time `json:"create_date,omitempty"` 16 Downloads int `json:"downloads"` 17 Views int `json:"views"` 18 Expire int64 `json:"expire"` 19 Preview bool `json:"preview"` 20 Creator *shareCreator `json:"creator,omitempty"` 21 Source *shareSource `json:"source,omitempty"` 22 } 23 24 type shareCreator struct { 25 Key string `json:"key"` 26 Nick string `json:"nick"` 27 GroupName string `json:"group_name"` 28 } 29 30 type shareSource struct { 31 Name string `json:"name"` 32 Size uint64 `json:"size"` 33 } 34 35 // myShareItem 我的分享列表条目 36 type myShareItem struct { 37 Key string `json:"key"` 38 IsDir bool `json:"is_dir"` 39 Password string `json:"password"` 40 CreateDate time.Time `json:"create_date,omitempty"` 41 Downloads int `json:"downloads"` 42 RemainDownloads int `json:"remain_downloads"` 43 Views int `json:"views"` 44 Expire int64 `json:"expire"` 45 Preview bool `json:"preview"` 46 Source *shareSource `json:"source,omitempty"` 47 } 48 49 // BuildShareList 构建我的分享列表响应 50 func BuildShareList(shares []model.Share, total int) Response { 51 res := make([]myShareItem, 0, total) 52 now := time.Now().Unix() 53 for i := 0; i < len(shares); i++ { 54 item := myShareItem{ 55 Key: hashid.HashID(shares[i].ID, hashid.ShareID), 56 IsDir: shares[i].IsDir, 57 Password: shares[i].Password, 58 CreateDate: shares[i].CreatedAt, 59 Downloads: shares[i].Downloads, 60 Views: shares[i].Views, 61 Preview: shares[i].PreviewEnabled, 62 Expire: -1, 63 RemainDownloads: shares[i].RemainDownloads, 64 } 65 if shares[i].Expires != nil { 66 item.Expire = shares[i].Expires.Unix() - now 67 if item.Expire == 0 { 68 item.Expire = 0 69 } 70 } 71 if shares[i].File.ID != 0 { 72 item.Source = &shareSource{ 73 Name: shares[i].File.Name, 74 Size: shares[i].File.Size, 75 } 76 } else if shares[i].Folder.ID != 0 { 77 item.Source = &shareSource{ 78 Name: shares[i].Folder.Name, 79 } 80 } 81 82 res = append(res, item) 83 } 84 85 return Response{Data: map[string]interface{}{ 86 "total": total, 87 "items": res, 88 }} 89 } 90 91 // BuildShareResponse 构建获取分享信息响应 92 func BuildShareResponse(share *model.Share, unlocked bool) Share { 93 creator := share.Creator() 94 resp := Share{ 95 Key: hashid.HashID(share.ID, hashid.ShareID), 96 Locked: !unlocked, 97 Creator: &shareCreator{ 98 Key: hashid.HashID(creator.ID, hashid.UserID), 99 Nick: creator.Nick, 100 GroupName: creator.Group.Name, 101 }, 102 CreateDate: share.CreatedAt, 103 } 104 105 // 未解锁时只返回基本信息 106 if !unlocked { 107 return resp 108 } 109 110 resp.IsDir = share.IsDir 111 resp.Downloads = share.Downloads 112 resp.Views = share.Views 113 resp.Preview = share.PreviewEnabled 114 115 if share.Expires != nil { 116 resp.Expire = share.Expires.Unix() - time.Now().Unix() 117 } 118 119 if share.IsDir { 120 source := share.SourceFolder() 121 resp.Source = &shareSource{ 122 Name: source.Name, 123 Size: 0, 124 } 125 } else { 126 source := share.SourceFile() 127 resp.Source = &shareSource{ 128 Name: source.Name, 129 Size: source.Size, 130 } 131 } 132 133 return resp 134 135 }