refactor: init v3
This commit is contained in:
@ -1,286 +0,0 @@
|
||||
package teambition
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Teambition struct{}
|
||||
|
||||
func (driver Teambition) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "Teambition",
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Teambition) Items() []base.Item {
|
||||
return []base.Item{
|
||||
{
|
||||
Name: "internal_type",
|
||||
Label: "Teambition type",
|
||||
Type: base.TypeSelect,
|
||||
Required: true,
|
||||
Values: "China,International",
|
||||
},
|
||||
{
|
||||
Name: "access_token",
|
||||
Label: "Cookie",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
Description: "Unknown expiration time",
|
||||
},
|
||||
{
|
||||
Name: "zone",
|
||||
Label: "Project id",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "root_folder",
|
||||
Label: "root folder file_id",
|
||||
Type: base.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "order_by",
|
||||
Label: "order_by",
|
||||
Type: base.TypeSelect,
|
||||
Values: "fileName,fileSize,updated,created",
|
||||
Required: true,
|
||||
Default: "fileName",
|
||||
},
|
||||
{
|
||||
Name: "order_direction",
|
||||
Label: "order_direction",
|
||||
Type: base.TypeSelect,
|
||||
Values: "Asc,Desc",
|
||||
Required: true,
|
||||
Default: "Asc",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (driver Teambition) Save(account *model.Account, old *model.Account) error {
|
||||
if account == nil {
|
||||
return nil
|
||||
}
|
||||
_, err := driver.Request("/api/v2/roles", base.Get, nil, nil, nil, nil, nil, account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Teambition) File(path string, account *model.Account) (*model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
if path == "/" {
|
||||
return &model.File{
|
||||
Id: account.RootFolder,
|
||||
Name: account.Name,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: account.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
dir, name := filepath.Split(path)
|
||||
files, err := driver.Files(dir, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.Name == name {
|
||||
return &file, nil
|
||||
}
|
||||
}
|
||||
return nil, base.ErrPathNotFound
|
||||
}
|
||||
|
||||
func (driver Teambition) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var files []model.File
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
files, _ = cache.([]model.File)
|
||||
} else {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files, err = driver.GetFiles(file.Id, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(files) > 0 {
|
||||
_ = base.SetCache(path, files, account)
|
||||
}
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Teambition) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
path := args.Path
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
url := file.Url
|
||||
res, err := base.NoRedirectClient.R().Get(url)
|
||||
if res.StatusCode() == 302 {
|
||||
url = res.Header().Get("location")
|
||||
}
|
||||
return &base.Link{Url: url}, nil
|
||||
}
|
||||
|
||||
func (driver Teambition) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
log.Debugf("teambition path: %s", path)
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nil, files, nil
|
||||
}
|
||||
|
||||
//func (driver Teambition) Proxy(r *http.Request, account *model.Account) {
|
||||
//
|
||||
//}
|
||||
|
||||
func (driver Teambition) Preview(path string, account *model.Account) (interface{}, error) {
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
|
||||
func (driver Teambition) MakeDir(path string, account *model.Account) error {
|
||||
parentFile, err := driver.File(utils.Dir(path), account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data := base.Json{
|
||||
"objectType": "collection",
|
||||
"_projectId": account.Zone,
|
||||
"_creatorId": "",
|
||||
"created": "",
|
||||
"updated": "",
|
||||
"title": utils.Base(path),
|
||||
"color": "blue",
|
||||
"description": "",
|
||||
"workCount": 0,
|
||||
"collectionType": "",
|
||||
"recentWorks": []interface{}{},
|
||||
"_parentId": parentFile.Id,
|
||||
"subCount": nil,
|
||||
}
|
||||
_, err = driver.Request("/api/collections", base.Post, nil, nil, nil, &data, nil, account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Teambition) Move(src string, dst string, account *model.Account) error {
|
||||
srcFile, err := driver.File(src, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dstParentFile, err := driver.File(utils.Dir(dst), account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pre := "/api/works/"
|
||||
if srcFile.IsDir() {
|
||||
pre = "/api/collections/"
|
||||
}
|
||||
_, err = driver.Request(pre+srcFile.Id+"/move", base.Put, nil, nil, nil, &base.Json{
|
||||
"_parentId": dstParentFile.Id,
|
||||
}, nil, account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Teambition) Rename(src string, dst string, account *model.Account) error {
|
||||
srcFile, err := driver.File(src, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pre := "/api/works/"
|
||||
data := base.Json{
|
||||
"fileName": utils.Base(dst),
|
||||
}
|
||||
if srcFile.IsDir() {
|
||||
pre = "/api/collections/"
|
||||
data = base.Json{
|
||||
"title": utils.Base(dst),
|
||||
}
|
||||
}
|
||||
_, err = driver.Request(pre+srcFile.Id, base.Put, nil, nil, nil, &data, nil, account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Teambition) Copy(src string, dst string, account *model.Account) error {
|
||||
srcFile, err := driver.File(src, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dstParentFile, err := driver.File(utils.Dir(dst), account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pre := "/api/works/"
|
||||
if srcFile.IsDir() {
|
||||
pre = "/api/collections/"
|
||||
}
|
||||
_, err = driver.Request(pre+srcFile.Id+"/fork", base.Put, nil, nil, nil, &base.Json{
|
||||
"_parentId": dstParentFile.Id,
|
||||
}, nil, account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Teambition) Delete(path string, account *model.Account) error {
|
||||
srcFile, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pre := "/api/works/"
|
||||
if srcFile.IsDir() {
|
||||
pre = "/api/collections/"
|
||||
}
|
||||
_, err = driver.Request(pre+srcFile.Id+"/archive", base.Post, nil, nil, nil, nil, nil, account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (driver Teambition) Upload(file *model.FileStream, account *model.Account) error {
|
||||
if file == nil {
|
||||
return base.ErrEmptyFile
|
||||
}
|
||||
parentFile, err := driver.File(file.ParentPath, account)
|
||||
if !parentFile.IsDir() {
|
||||
return base.ErrNotFolder
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, err := driver.Request("/projects", base.Get, nil, nil, nil, nil, nil, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
token := GetBetweenStr(string(res), "strikerAuth":"", "","phoneForLogin")
|
||||
var newFile *FileUpload
|
||||
if file.Size <= 20971520 {
|
||||
// post upload
|
||||
newFile, err = driver.upload(file, token, account)
|
||||
} else {
|
||||
// chunk upload
|
||||
//err = base.ErrNotImplement
|
||||
newFile, err = driver.chunkUpload(file, token, account)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return driver.finishUpload(newFile, parentFile.Id, account)
|
||||
}
|
||||
|
||||
var _ base.Driver = (*Teambition)(nil)
|
@ -1,237 +0,0 @@
|
||||
package teambition
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/go-resty/resty/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ErrResp struct {
|
||||
Name string `json:"name"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (driver Teambition) Request(pathname string, method int, headers, query, form map[string]string, data interface{}, resp interface{}, account *model.Account) ([]byte, error) {
|
||||
url := "https://www.teambition.com" + pathname
|
||||
if account.InternalType == "International" {
|
||||
url = "https://us.teambition.com" + pathname
|
||||
}
|
||||
req := base.RestyClient.R()
|
||||
req.SetHeader("Cookie", account.AccessToken)
|
||||
if headers != nil {
|
||||
req.SetHeaders(headers)
|
||||
}
|
||||
if query != nil {
|
||||
req.SetQueryParams(query)
|
||||
}
|
||||
if form != nil {
|
||||
req.SetFormData(form)
|
||||
}
|
||||
if data != nil {
|
||||
req.SetBody(data)
|
||||
}
|
||||
if resp != nil {
|
||||
req.SetResult(resp)
|
||||
}
|
||||
var e ErrResp
|
||||
var err error
|
||||
var res *resty.Response
|
||||
req.SetError(&e)
|
||||
switch method {
|
||||
case base.Get:
|
||||
res, err = req.Get(url)
|
||||
case base.Post:
|
||||
res, err = req.Post(url)
|
||||
case base.Delete:
|
||||
res, err = req.Delete(url)
|
||||
case base.Patch:
|
||||
res, err = req.Patch(url)
|
||||
case base.Put:
|
||||
res, err = req.Put(url)
|
||||
default:
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if e.Name != "" {
|
||||
return nil, errors.New(e.Message)
|
||||
}
|
||||
return res.Body(), nil
|
||||
}
|
||||
|
||||
func (driver Teambition) GetFiles(parentId string, account *model.Account) ([]model.File, error) {
|
||||
files := make([]model.File, 0)
|
||||
page := 1
|
||||
for {
|
||||
var collections []Collection
|
||||
_, err := driver.Request("/api/collections", base.Get, nil, map[string]string{
|
||||
"_parentId": parentId,
|
||||
"_projectId": account.Zone,
|
||||
"order": account.OrderBy + account.OrderDirection,
|
||||
"count": "50",
|
||||
"page": strconv.Itoa(page),
|
||||
}, nil, nil, &collections, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(collections) == 0 {
|
||||
break
|
||||
}
|
||||
page++
|
||||
for _, collection := range collections {
|
||||
if collection.Title == "" {
|
||||
continue
|
||||
}
|
||||
files = append(files, model.File{
|
||||
Id: collection.ID,
|
||||
Name: collection.Title,
|
||||
Size: 0,
|
||||
Type: conf.FOLDER,
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: collection.Updated,
|
||||
})
|
||||
}
|
||||
}
|
||||
page = 1
|
||||
for {
|
||||
var works []Work
|
||||
_, err := driver.Request("/api/works", base.Get, nil, map[string]string{
|
||||
"_parentId": parentId,
|
||||
"_projectId": account.Zone,
|
||||
"order": account.OrderBy + account.OrderDirection,
|
||||
"count": "50",
|
||||
"page": strconv.Itoa(page),
|
||||
}, nil, nil, &works, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(works) == 0 {
|
||||
break
|
||||
}
|
||||
page++
|
||||
for _, work := range works {
|
||||
files = append(files, model.File{
|
||||
Id: work.ID,
|
||||
Name: work.FileName,
|
||||
Size: work.FileSize,
|
||||
Type: utils.GetFileType(path.Ext(work.FileName)),
|
||||
Driver: driver.Config().Name,
|
||||
UpdatedAt: work.Updated,
|
||||
Thumbnail: work.Thumbnail,
|
||||
Url: work.DownloadURL,
|
||||
})
|
||||
}
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Teambition) upload(file *model.FileStream, token string, account *model.Account) (*FileUpload, error) {
|
||||
prefix := "tcs"
|
||||
if account.InternalType == "International" {
|
||||
prefix = "us-tcs"
|
||||
}
|
||||
var newFile FileUpload
|
||||
_, err := base.RestyClient.R().SetResult(&newFile).SetHeader("Authorization", token).
|
||||
SetMultipartFormData(map[string]string{
|
||||
"name": file.GetFileName(),
|
||||
"type": file.GetMIMEType(),
|
||||
"size": strconv.FormatUint(file.GetSize(), 10),
|
||||
//"lastModifiedDate": "",
|
||||
}).SetMultipartField("file", file.GetFileName(), file.GetMIMEType(), file).
|
||||
Post(fmt.Sprintf("https://%s.teambition.net/upload", prefix))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &newFile, nil
|
||||
}
|
||||
|
||||
func (driver Teambition) chunkUpload(file *model.FileStream, token string, account *model.Account) (*FileUpload, error) {
|
||||
prefix := "tcs"
|
||||
referer := "https://www.teambition.com/"
|
||||
if account.InternalType == "International" {
|
||||
prefix = "us-tcs"
|
||||
referer = "https://us.teambition.com/"
|
||||
}
|
||||
var newChunk ChunkUpload
|
||||
_, err := base.RestyClient.R().SetResult(&newChunk).SetHeader("Authorization", token).
|
||||
SetBody(base.Json{
|
||||
"fileName": file.GetFileName(),
|
||||
"fileSize": file.GetSize(),
|
||||
"lastUpdated": time.Now(),
|
||||
}).Post(fmt.Sprintf("https://%s.teambition.net/upload/chunk", prefix))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := 0; i < newChunk.Chunks; i++ {
|
||||
chunkSize := newChunk.ChunkSize
|
||||
if i == newChunk.Chunks-1 {
|
||||
chunkSize = int(file.GetSize()) - i*chunkSize
|
||||
}
|
||||
log.Debugf("%d : %d", i, chunkSize)
|
||||
chunkData := make([]byte, chunkSize)
|
||||
_, err = io.ReadFull(file, chunkData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u := fmt.Sprintf("https://%s.teambition.net/upload/chunk/%s?chunk=%d&chunks=%d",
|
||||
prefix, newChunk.FileKey, i+1, newChunk.Chunks)
|
||||
log.Debugf("url: %s", u)
|
||||
res, err := base.RestyClient.R().SetHeaders(map[string]string{
|
||||
"Authorization": token,
|
||||
"Content-Type": "application/octet-stream",
|
||||
"Referer": referer,
|
||||
}).SetBody(chunkData).Post(u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debug(res.Status(), res.String())
|
||||
//req, err := http.NewRequest("POST",
|
||||
// u,
|
||||
// bytes.NewBuffer(chunkData))
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//req.Header.Set("Authorization", token)
|
||||
//req.Header.Set("Content-Type", "application/octet-stream")
|
||||
//req.Header.Set("Referer", "https://www.teambition.com/")
|
||||
//resp, err := base.HttpClient.Do(req)
|
||||
//res, _ := ioutil.ReadAll(resp.Body)
|
||||
//log.Debugf("chunk upload status: %s, res: %s", resp.Status, string(res))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
res, err := base.RestyClient.R().SetHeader("Authorization", token).Post(
|
||||
fmt.Sprintf("https://%s.teambition.net/upload/chunk/%s",
|
||||
prefix, newChunk.FileKey))
|
||||
log.Debug(res.Status(), res.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &newChunk.FileUpload, nil
|
||||
}
|
||||
|
||||
func (driver Teambition) finishUpload(file *FileUpload, parentId string, account *model.Account) error {
|
||||
file.InvolveMembers = []interface{}{}
|
||||
file.Visible = "members"
|
||||
file.ParentId = parentId
|
||||
_, err := driver.Request("/api/works", base.Post, nil, nil, nil, base.Json{
|
||||
"works": []FileUpload{*file},
|
||||
"_parentId": parentId,
|
||||
}, nil, account)
|
||||
return err
|
||||
}
|
||||
|
||||
func init() {
|
||||
base.RegisterDriver(&Teambition{})
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
package teambition
|
||||
|
||||
import "time"
|
||||
|
||||
type Collection struct {
|
||||
ID string `json:"_id"`
|
||||
Title string `json:"title"`
|
||||
Updated *time.Time `json:"updated"`
|
||||
}
|
||||
|
||||
type Work struct {
|
||||
ID string `json:"_id"`
|
||||
FileName string `json:"fileName"`
|
||||
FileSize int64 `json:"fileSize"`
|
||||
FileKey string `json:"fileKey"`
|
||||
FileCategory string `json:"fileCategory"`
|
||||
DownloadURL string `json:"downloadUrl"`
|
||||
ThumbnailURL string `json:"thumbnailUrl"`
|
||||
Thumbnail string `json:"thumbnail"`
|
||||
Updated *time.Time `json:"updated"`
|
||||
PreviewURL string `json:"previewUrl"`
|
||||
}
|
||||
|
||||
type FileUpload struct {
|
||||
FileKey string `json:"fileKey"`
|
||||
FileName string `json:"fileName"`
|
||||
FileType string `json:"fileType"`
|
||||
FileSize int `json:"fileSize"`
|
||||
FileCategory string `json:"fileCategory"`
|
||||
ImageWidth int `json:"imageWidth"`
|
||||
ImageHeight int `json:"imageHeight"`
|
||||
InvolveMembers []interface{} `json:"involveMembers"`
|
||||
Source string `json:"source"`
|
||||
Visible string `json:"visible"`
|
||||
ParentId string `json:"_parentId"`
|
||||
}
|
||||
|
||||
type ChunkUpload struct {
|
||||
FileUpload
|
||||
Storage string `json:"storage"`
|
||||
MimeType string `json:"mimeType"`
|
||||
Chunks int `json:"chunks"`
|
||||
ChunkSize int `json:"chunkSize"`
|
||||
Created time.Time `json:"created"`
|
||||
FileMD5 string `json:"fileMD5"`
|
||||
LastUpdated time.Time `json:"lastUpdated"`
|
||||
UploadedChunks []interface{} `json:"uploadedChunks"`
|
||||
Token struct {
|
||||
AppID string `json:"AppID"`
|
||||
OrganizationID string `json:"OrganizationID"`
|
||||
UserID string `json:"UserID"`
|
||||
Exp time.Time `json:"Exp"`
|
||||
Storage string `json:"Storage"`
|
||||
Resource string `json:"Resource"`
|
||||
Speed int `json:"Speed"`
|
||||
} `json:"token"`
|
||||
DownloadUrl string `json:"downloadUrl"`
|
||||
ThumbnailUrl string `json:"thumbnailUrl"`
|
||||
PreviewUrl string `json:"previewUrl"`
|
||||
ImmPreviewUrl string `json:"immPreviewUrl"`
|
||||
PreviewExt string `json:"previewExt"`
|
||||
LastUploadTime interface{} `json:"lastUploadTime"`
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user