Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(139): update family cloud API #1

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions drivers/139/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ func (d *Yun139) Link(ctx context.Context, file model.Obj, args model.LinkArgs)
case MetaPersonalNew:
url, err = d.personalGetLink(file.GetID())
case MetaPersonal:
fallthrough
case MetaFamily:
url, err = d.getLink(file.GetID())
case MetaFamily:
url, err = d.familyGetLink(file.GetID(), file.GetPath())
default:
return nil, errs.NotImplement
}
Expand Down
16 changes: 15 additions & 1 deletion drivers/139/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,11 @@ func (d *Yun139) familyGetFiles(catalogID string) ([]model.Obj, error) {
"sortDirection": 1,
})
var resp QueryContentListResp
_, err := d.post("/orchestration/familyCloud/content/v1.0/queryContentList", data, &resp)
_, err := d.post("/orchestration/familyCloud-rebuild/content/v1.2/queryContentList", data, &resp)
if err != nil {
return nil, err
}
path := resp.Data.Path
for _, catalog := range resp.Data.CloudCatalogList {
f := model.Object{
ID: catalog.CatalogID,
Expand All @@ -243,6 +244,7 @@ func (d *Yun139) familyGetFiles(catalogID string) ([]model.Obj, error) {
Size: content.ContentSize,
Modified: getTime(content.LastUpdateTime),
Ctime: getTime(content.CreateTime),
Path: path,
},
Thumbnail: model.Thumbnail{Thumbnail: content.ThumbnailURL},
//Thumbnail: content.BigthumbnailURL,
Expand Down Expand Up @@ -273,6 +275,18 @@ func (d *Yun139) getLink(contentId string) (string, error) {
}
return jsoniter.Get(res, "data", "downloadURL").ToString(), nil
}
func (d *Yun139) familyGetLink(contentId string, path string) (string, error) {
data := d.newJson(base.Json{
"contentID": contentId,
"path": path,
})
res, err := d.post("/orchestration/personalCloud/uploadAndDownload/v1.0/downloadRequest",
data, nil)
if err != nil {
return "", err
}
return jsoniter.Get(res, "data", "downloadURL").ToString(), nil
}

func unicode(str string) string {
textQuoted := strconv.QuoteToASCII(str)
Expand Down
66 changes: 56 additions & 10 deletions drivers/terabox/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
stdpath "path"
"strconv"
"strings"
"time"

"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/pkg/utils"
Expand All @@ -23,7 +24,9 @@ import (
type Terabox struct {
model.Storage
Addition
JsToken string
JsToken string
url_domain_prefix string
base_url string
}

func (d *Terabox) Config() driver.Config {
Expand All @@ -36,6 +39,8 @@ func (d *Terabox) GetAddition() driver.Additional {

func (d *Terabox) Init(ctx context.Context) error {
var resp CheckLoginResp
d.base_url = "https://www.terabox.com"
d.url_domain_prefix = "jp"
_, err := d.get("/api/check/login", nil, &resp)
if err != nil {
return err
Expand Down Expand Up @@ -71,7 +76,16 @@ func (d *Terabox) Link(ctx context.Context, file model.Obj, args model.LinkArgs)
}

func (d *Terabox) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
_, err := d.create(stdpath.Join(parentDir.GetPath(), dirName), 0, 1, "", "")
params := map[string]string{
"a": "commit",
}
data := map[string]string{
"path": stdpath.Join(parentDir.GetPath(), dirName),
"isdir": "1",
"block_list": "[]",
}
res, err := d.post_form("/api/create", params, data, nil)
log.Debugln(string(res))
return err
}

Expand Down Expand Up @@ -117,6 +131,20 @@ func (d *Terabox) Remove(ctx context.Context, obj model.Obj) error {
}

func (d *Terabox) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
resp, err := base.RestyClient.R().
SetContext(ctx).
Get("https://" + d.url_domain_prefix + "-data.terabox.com/rest/2.0/pcs/file?method=locateupload")
if err != nil {
return err
}
var locateupload_resp LocateUploadResp
err = utils.Json.Unmarshal(resp.Body(), &locateupload_resp)
if err != nil {
log.Debugln(resp)
return err
}
log.Debugln(locateupload_resp)

tempFile, err := stream.CacheFullInTempFile()
if err != nil {
return err
Expand Down Expand Up @@ -157,23 +185,28 @@ func (d *Terabox) Put(ctx context.Context, dstDir model.Obj, stream model.FileSt
rawPath := stdpath.Join(dstDir.GetPath(), stream.GetName())
path := encodeURIComponent(rawPath)
block_list_str := fmt.Sprintf("[%s]", strings.Join(block_list, ","))
data := fmt.Sprintf("path=%s&size=%d&isdir=0&autoinit=1&block_list=%s",
path, stream.GetSize(),
block_list_str)
params := map[string]string{}
data := map[string]string{
"path": rawPath,
"autoinit": "1",
"target_path": dstDir.GetPath(),
"block_list": block_list_str,
"local_mtime": strconv.FormatInt(time.Now().Unix(), 10),
}
var precreateResp PrecreateResp
_, err = d.post("/api/precreate", params, data, &precreateResp)
log.Debugln(data)
res, err := d.post_form("/api/precreate", nil, data, &precreateResp)
if err != nil {
return err
}
log.Debugf("%+v", precreateResp)
if precreateResp.Errno != 0 {
log.Debugln(string(res))
return fmt.Errorf("[terabox] failed to precreate file, errno: %d", precreateResp.Errno)
}
if precreateResp.ReturnType == 2 {
return nil
}
params = map[string]string{
params := map[string]string{
"method": "upload",
"path": path,
"uploadid": precreateResp.Uploadid,
Expand All @@ -200,7 +233,7 @@ func (d *Terabox) Put(ctx context.Context, dstDir model.Obj, stream model.FileSt
if err != nil {
return err
}
u := "https://c-jp.terabox.com/rest/2.0/pcs/superfile2"
u := "https://" + locateupload_resp.Host + "/rest/2.0/pcs/superfile2"
params["partseq"] = strconv.Itoa(partseq)
res, err := base.RestyClient.R().
SetContext(ctx).
Expand All @@ -216,7 +249,20 @@ func (d *Terabox) Put(ctx context.Context, dstDir model.Obj, stream model.FileSt
up(float64(i) * 100 / float64(len(precreateResp.BlockList)))
}
}
_, err = d.create(rawPath, stream.GetSize(), 0, precreateResp.Uploadid, block_list_str)
params = map[string]string{
"isdir": "0",
"rtype": "1",
}
data = map[string]string{
"path": rawPath,
"size": strconv.FormatInt(stream.GetSize(), 10),
"uploadid": precreateResp.Uploadid,
"target_path": dstDir.GetPath(),
"block_list": block_list_str,
"local_mtime": strconv.FormatInt(time.Now().Unix(), 10),
}
res, err = d.post_form("/api/create", params, data, nil)
log.Debugln(string(res))
return err
}

Expand Down
4 changes: 4 additions & 0 deletions drivers/terabox/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@ type PrecreateResp struct {
type CheckLoginResp struct {
Errno int `json:"errno"`
}

type LocateUploadResp struct {
Host string `json:"host"`
}
41 changes: 24 additions & 17 deletions drivers/terabox/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
)

func getStrBetween(raw, start, end string) string {
Expand All @@ -28,11 +29,11 @@ func getStrBetween(raw, start, end string) string {
}

func (d *Terabox) resetJsToken() error {
u := "https://www.terabox.com/main"
u := d.base_url
res, err := base.RestyClient.R().SetHeaders(map[string]string{
"Cookie": d.Cookie,
"Accept": "application/json, text/plain, */*",
"Referer": "https://www.terabox.com/",
"Referer": d.base_url,
"User-Agent": base.UserAgent,
"X-Requested-With": "XMLHttpRequest",
}).Get(u)
Expand All @@ -48,12 +49,12 @@ func (d *Terabox) resetJsToken() error {
return nil
}

func (d *Terabox) request(furl string, method string, callback base.ReqCallback, resp interface{}, noRetry ...bool) ([]byte, error) {
func (d *Terabox) request(rurl string, method string, callback base.ReqCallback, resp interface{}, noRetry ...bool) ([]byte, error) {
req := base.RestyClient.R()
req.SetHeaders(map[string]string{
"Cookie": d.Cookie,
"Accept": "application/json, text/plain, */*",
"Referer": "https://www.terabox.com/",
"Referer": d.base_url,
"User-Agent": base.UserAgent,
"X-Requested-With": "XMLHttpRequest",
})
Expand All @@ -70,7 +71,7 @@ func (d *Terabox) request(furl string, method string, callback base.ReqCallback,
if resp != nil {
req.SetResult(resp)
}
res, err := req.Execute(method, furl)
res, err := req.Execute(method, d.base_url+rurl)
if err != nil {
return nil, err
}
Expand All @@ -82,29 +83,44 @@ func (d *Terabox) request(furl string, method string, callback base.ReqCallback,
return nil, err
}
if !utils.IsBool(noRetry...) {
return d.request(furl, method, callback, resp, true)
return d.request(rurl, method, callback, resp, true)
}
} else if errno == -6 {
log.Debugln(res.Header())
d.url_domain_prefix = res.Header()["Url-Domain-Prefix"][0]
d.base_url = "https://" + d.url_domain_prefix + ".terabox.com"
log.Debugln("Redirect base_url to", d.base_url)
return d.request(rurl, method, callback, resp, noRetry...)
}
return res.Body(), nil
}

func (d *Terabox) get(pathname string, params map[string]string, resp interface{}) ([]byte, error) {
return d.request("https://www.terabox.com"+pathname, http.MethodGet, func(req *resty.Request) {
return d.request(pathname, http.MethodGet, func(req *resty.Request) {
if params != nil {
req.SetQueryParams(params)
}
}, resp)
}

func (d *Terabox) post(pathname string, params map[string]string, data interface{}, resp interface{}) ([]byte, error) {
return d.request("https://www.terabox.com"+pathname, http.MethodPost, func(req *resty.Request) {
return d.request(pathname, http.MethodPost, func(req *resty.Request) {
if params != nil {
req.SetQueryParams(params)
}
req.SetBody(data)
}, resp)
}

func (d *Terabox) post_form(pathname string, params map[string]string, data map[string]string, resp interface{}) ([]byte, error) {
return d.request(pathname, http.MethodPost, func(req *resty.Request) {
if params != nil {
req.SetQueryParams(params)
}
req.SetFormData(data)
}, resp)
}

func (d *Terabox) getFiles(dir string) ([]File, error) {
page := 1
num := 100
Expand Down Expand Up @@ -237,15 +253,6 @@ func (d *Terabox) manage(opera string, filelist interface{}) ([]byte, error) {
return d.post("/api/filemanager", params, data, nil)
}

func (d *Terabox) create(path string, size int64, isdir int, uploadid, block_list string) ([]byte, error) {
params := map[string]string{}
data := fmt.Sprintf("path=%s&size=%d&isdir=%d", encodeURIComponent(path), size, isdir)
if uploadid != "" {
data += fmt.Sprintf("&uploadid=%s&block_list=%s", uploadid, block_list)
}
return d.post("/api/create", params, data, nil)
}

func encodeURIComponent(str string) string {
r := url.QueryEscape(str)
r = strings.ReplaceAll(r, "+", "%20")
Expand Down
Loading