88 "fmt"
99 "io"
1010 "net/http"
11+ "path"
1112 "strings"
1213 "sync"
1314 "time"
@@ -29,6 +30,31 @@ type AlistServer struct {
2930 cache * bigcache.BigCache
3031}
3132
33+ // 获得AlistServer实例
34+ func New (addr string , username string , password string , token * string ) * AlistServer {
35+ s := AlistServer {
36+ endpoint : utils .GetEndpoint (addr ),
37+ username : username ,
38+ password : password ,
39+ client : utils .GetHTTPClient (),
40+ }
41+ if token != nil {
42+ s .token = alistToken {
43+ value : * token ,
44+ expireAt : time.Time {},
45+ }
46+ }
47+ if config .Cache .Enable && config .Cache .AlistTTL > 0 {
48+ cache , err := bigcache .NewBigCache (bigcache .DefaultConfig (config .Cache .AlistTTL ))
49+ if err == nil {
50+ s .cache = cache
51+ } else {
52+ logging .Warning ("创建 Alist API 缓存失败: " , err )
53+ }
54+ }
55+ return & s
56+ }
57+
3258// 得到服务器入口
3359//
3460// 避免直接访问 endpoint 字段
@@ -171,31 +197,6 @@ func (alistServer *AlistServer) FsGet(path string) (*FsGetData, error) {
171197 return & data , nil
172198}
173199
174- // 获得AlistServer实例
175- func New (addr string , username string , password string , token * string ) * AlistServer {
176- s := AlistServer {
177- endpoint : utils .GetEndpoint (addr ),
178- username : username ,
179- password : password ,
180- client : utils .GetHTTPClient (),
181- }
182- if token != nil {
183- s .token = alistToken {
184- value : * token ,
185- expireAt : time.Time {},
186- }
187- }
188- if config .Cache .Enable && config .Cache .AlistTTL > 0 {
189- cache , err := bigcache .NewBigCache (bigcache .DefaultConfig (config .Cache .AlistTTL ))
190- if err == nil {
191- s .cache = cache
192- } else {
193- logging .Warning ("创建 Alist API 缓存失败: " , err )
194- }
195- }
196- return & s
197- }
198-
199200func (alistServer * AlistServer ) Me () (* UserInfoData , error ) {
200201 var data UserInfoData
201202 err := alistServer .doRequest (
@@ -211,3 +212,23 @@ func (alistServer *AlistServer) Me() (*UserInfoData, error) {
211212 }
212213 return & data , nil
213214}
215+
216+ // GetFileURL 获取文件的可访问 URL
217+ func (alistServer * AlistServer ) GetFileURL (p string , isRawURL bool ) (string , error ) {
218+ fileData , err := alistServer .FsGet (p )
219+ if err != nil {
220+ return "" , fmt .Errorf ("获取文件信息失败:%w" , err )
221+ }
222+ if isRawURL {
223+ return fileData .RawURL , nil
224+ }
225+ userInfo , err := alistServer .Me ()
226+ if err != nil {
227+ return "" , fmt .Errorf ("获取用户当前信息失败:%w" , err )
228+ }
229+ var sign string
230+ if fileData .Sign != "" {
231+ sign = "?sign=" + fileData .Sign
232+ }
233+ return alistServer .GetEndpoint () + path .Join (userInfo .BasePath , p ) + sign , nil
234+ }
0 commit comments