mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-20 04:36:09 +08:00

https://github.com/tgdrive/teldrive https://teldrive-docs.pages.dev/docs/api 实现: * copy * move * link (302 share and local proxy) * chunked uploads * rename 未实现: - openlist扫码登陆 - refresh token https://github.com/OpenListTeam/OpenList-Docs/pull/155 * feat(Teldrive): Add driver Teldrive * fix(teldrive): force webproxy and memory optimized * chore(teldrive): go fmt * chore(teldrive): remove TODO * chore(teldrive): organize code * feat(teldrive): add UseShareLink option and support 302 * fix(teldrive): standardize API path construction * fix(teldrive): trim trailing slash from Address in Init method * chore(teldrive): update help text for UseShareLink field in Addition struct * fix(teldrive): set 10 MiB as default chunk size --------- Co-authored-by: MadDogOwner <xiaoran@xrgzs.top> Co-authored-by: ILoveScratch <ilovescratch@foxmail.com>
110 lines
2.3 KiB
Go
110 lines
2.3 KiB
Go
package teldrive
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/OpenListTeam/OpenList/v4/drivers/base"
|
|
"github.com/OpenListTeam/OpenList/v4/internal/model"
|
|
"github.com/go-resty/resty/v2"
|
|
)
|
|
|
|
// do others that not defined in Driver interface
|
|
|
|
func (d *Teldrive) request(method string, pathname string, callback base.ReqCallback, resp interface{}) error {
|
|
url := d.Address + pathname
|
|
req := base.RestyClient.R()
|
|
req.SetHeader("Cookie", d.Cookie)
|
|
if callback != nil {
|
|
callback(req)
|
|
}
|
|
if resp != nil {
|
|
req.SetResult(resp)
|
|
}
|
|
var e ErrResp
|
|
req.SetError(&e)
|
|
_req, err := req.Execute(method, url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if _req.IsError() {
|
|
return &e
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *Teldrive) getFile(path, name string, isFolder bool) (model.Obj, error) {
|
|
resp := &ListResp{}
|
|
err := d.request(http.MethodGet, "/api/files", func(req *resty.Request) {
|
|
req.SetQueryParams(map[string]string{
|
|
"path": path,
|
|
"name": name,
|
|
"type": func() string {
|
|
if isFolder {
|
|
return "folder"
|
|
}
|
|
return "file"
|
|
}(),
|
|
"operation": "find",
|
|
})
|
|
}, resp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(resp.Items) == 0 {
|
|
return nil, fmt.Errorf("file not found: %s/%s", path, name)
|
|
}
|
|
obj := resp.Items[0]
|
|
return &model.Object{
|
|
ID: obj.ID,
|
|
Name: obj.Name,
|
|
Size: obj.Size,
|
|
IsFolder: obj.Type == "folder",
|
|
}, err
|
|
}
|
|
|
|
func (err *ErrResp) Error() string {
|
|
if err == nil {
|
|
return ""
|
|
}
|
|
|
|
return fmt.Sprintf("[Teldrive] message:%s Error code:%d", err.Message, err.Code)
|
|
}
|
|
|
|
func (d *Teldrive) createShareFile(fileId string) error {
|
|
var errResp ErrResp
|
|
if err := d.request(http.MethodPost, "/api/files/{id}/share", func(req *resty.Request) {
|
|
req.SetPathParam("id", fileId)
|
|
req.SetBody(base.Json{
|
|
"expiresAt": getDateTime(),
|
|
})
|
|
}, &errResp); err != nil {
|
|
return err
|
|
}
|
|
|
|
if errResp.Message != "" {
|
|
return &errResp
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *Teldrive) getShareFileById(fileId string) (*ShareObj, error) {
|
|
var shareObj ShareObj
|
|
if err := d.request(http.MethodGet, "/api/files/{id}/share", func(req *resty.Request) {
|
|
req.SetPathParam("id", fileId)
|
|
}, &shareObj); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &shareObj, nil
|
|
}
|
|
|
|
func getDateTime() string {
|
|
now := time.Now().UTC()
|
|
formattedWithMs := now.Add(time.Hour * 1).Format("2006-01-02T15:04:05.000Z")
|
|
return formattedWithMs
|
|
}
|