fix(teambition): international upload (close #5360)

pull/5622/head
Andy Hsu 2023-11-29 22:51:03 +08:00
parent f4dcf4599c
commit b99e709bdb
3 changed files with 41 additions and 10 deletions

View File

@ -3,12 +3,12 @@ package teambition
import ( import (
"context" "context"
"errors" "errors"
"github.com/alist-org/alist/v3/pkg/utils"
"net/http" "net/http"
"github.com/alist-org/alist/v3/drivers/base" "github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/driver" "github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
) )
@ -128,11 +128,23 @@ func (d *Teambition) Put(ctx context.Context, dstDir model.Obj, stream model.Fil
if d.UseS3UploadMethod { if d.UseS3UploadMethod {
return d.newUpload(ctx, dstDir, stream, up) return d.newUpload(ctx, dstDir, stream, up)
} }
res, err := d.request("/api/v2/users/me", http.MethodGet, nil, nil) var (
if err != nil { token string
return err err error
)
if d.isInternational() {
res, err := d.request("/projects", http.MethodGet, nil, nil)
if err != nil {
return err
}
token = getBetweenStr(string(res), "strikerAuth":"", "","phoneForLogin")
} else {
res, err := d.request("/api/v2/users/me", http.MethodGet, nil, nil)
if err != nil {
return err
}
token = utils.Json.Get(res, "strikerAuth").ToString()
} }
token := utils.Json.Get(res, "strikerAuth").ToString()
var newFile *FileUpload var newFile *FileUpload
if stream.GetSize() <= 20971520 { if stream.GetSize() <= 20971520 {
// post upload // post upload

View File

@ -0,0 +1,18 @@
package teambition
import "strings"
func getBetweenStr(str, start, end string) string {
n := strings.Index(str, start)
if n == -1 {
return ""
}
n = n + len(start)
str = string([]byte(str)[n:])
m := strings.Index(str, end)
if m == -1 {
return ""
}
str = string([]byte(str)[:m])
return str
}

View File

@ -126,19 +126,20 @@ func (d *Teambition) upload(ctx context.Context, file model.FileStreamer, token
prefix = "us-tcs" prefix = "us-tcs"
} }
var newFile FileUpload var newFile FileUpload
_, err := base.RestyClient.R(). res, err := base.RestyClient.R().
SetContext(ctx). SetContext(ctx).
SetResult(&newFile).SetHeader("Authorization", token). SetResult(&newFile).SetHeader("Authorization", token).
SetMultipartFormData(map[string]string{ SetMultipartFormData(map[string]string{
"name": file.GetName(), "name": file.GetName(),
"type": file.GetMimetype(), "type": file.GetMimetype(),
"size": strconv.FormatInt(file.GetSize(), 10), "size": strconv.FormatInt(file.GetSize(), 10),
//"lastModifiedDate": "", "lastModifiedDate": time.Now().Format("Mon Jan 02 2006 15:04:05 GMT+0800 (中国标准时间)"),
}).SetMultipartField("file", file.GetName(), file.GetMimetype(), file). }).SetMultipartField("file", file.GetName(), file.GetMimetype(), file).
Post(fmt.Sprintf("https://%s.teambition.net/upload", prefix)) Post(fmt.Sprintf("https://%s.teambition.net/upload", prefix))
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Debugf("[teambition] upload response: %s", res.String())
return &newFile, nil return &newFile, nil
} }