Skip to content

Commit

Permalink
feat: add teambition driver
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Sep 2, 2022
1 parent bc155af commit 0f2425c
Show file tree
Hide file tree
Showing 22 changed files with 523 additions and 30 deletions.
2 changes: 1 addition & 1 deletion drivers/123/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Addition struct {
Password string `json:"password" required:"true"`
OrderBy string `json:"order_by" type:"select" options:"name,fileId,updateAt,createAt" default:"name"`
OrderDirection string `json:"order_direction" type:"select" options:"asc,desc" default:"asc"`
driver.RootFolderId
driver.RootFolderID
// define other
StreamUpload bool `json:"stream_upload"`
//Field string `json:"field" type:"select" required:"true" options:"a,b,c" default:"a"`
Expand Down
2 changes: 1 addition & 1 deletion drivers/aliyundrive/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type Addition struct {
driver.RootFolderId
driver.RootFolderID
RefreshToken string `json:"refresh_token" required:"true"`
OrderBy string `json:"order_by" type:"select" options:"name,size,updated_at,created_at"`
OrderDirection string `json:"order_direction" type:"select" options:"ASC,DESC"`
Expand Down
4 changes: 2 additions & 2 deletions drivers/aliyundrive/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type File struct {
Url string `json:"url"`
}

func fileToObj(f File) model.ObjectThumbnail {
return model.ObjectThumbnail{
func fileToObj(f File) *model.ObjThumb {
return &model.ObjThumb{
Object: model.Object{
ID: f.FileId,
Name: f.Name,
Expand Down
1 change: 1 addition & 0 deletions drivers/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
_ "github.com/alist-org/alist/v3/drivers/local"
_ "github.com/alist-org/alist/v3/drivers/onedrive"
_ "github.com/alist-org/alist/v3/drivers/pikpak"
_ "github.com/alist-org/alist/v3/drivers/teambition"
_ "github.com/alist-org/alist/v3/drivers/virtual"
)

Expand Down
2 changes: 1 addition & 1 deletion drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (d *Local) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([
thumb = utils.EncodePath(thumb, true)
thumb += "?type=thumb"
}
file := model.ObjectThumbnail{
file := model.ObjThumb{
Object: model.Object{
Name: f.Name(),
Modified: f.ModTime(),
Expand Down
5 changes: 3 additions & 2 deletions drivers/onedrive/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ type File struct {
} `json:"parentReference"`
}

func fileToObj(f File) *model.ObjectThumbnail {
func fileToObj(f File) *model.ObjThumbURL {
thumb := ""
if len(f.Thumbnails) > 0 {
thumb = f.Thumbnails[0].Medium.Url
}
return &model.ObjectThumbnail{
return &model.ObjThumbURL{
Object: model.Object{
//ID: f.Id,
Name: f.Name,
Expand All @@ -56,6 +56,7 @@ func fileToObj(f File) *model.ObjectThumbnail {
IsFolder: f.File == nil,
},
Thumbnail: model.Thumbnail{Thumbnail: thumb},
Url: model.Url{Url: f.Url},
}
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/pikpak/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type Addition struct {
driver.RootFolderId
driver.RootFolderID
Username string `json:"username" required:"true"`
Password string `json:"password" required:"true"`
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/pikpak/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ type File struct {
Medias []Media `json:"medias"`
}

func fileToObj(f File) model.ObjectThumbnail {
func fileToObj(f File) *model.ObjThumb {
size, _ := strconv.ParseInt(f.Size, 10, 64)
return model.ObjectThumbnail{
return &model.ObjThumb{
Object: model.Object{
ID: f.Id,
Name: f.Name,
Expand Down
163 changes: 163 additions & 0 deletions drivers/teambition/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package teambition

import (
"context"
"errors"
"net/http"

"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/go-resty/resty/v2"
)

type Teambition struct {
model.Storage
Addition
}

func (d *Teambition) Config() driver.Config {
return config
}

func (d *Teambition) GetAddition() driver.Additional {
return d.Addition
}

func (d *Teambition) Init(ctx context.Context, storage model.Storage) error {
d.Storage = storage
err := utils.Json.UnmarshalFromString(d.Storage.Addition, &d.Addition)
if err != nil {
return err
}
_, err = d.request("/api/v2/roles", http.MethodGet, nil, nil)
return err
}

func (d *Teambition) Drop(ctx context.Context) error {
return nil
}

func (d *Teambition) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
return d.getFiles(dir.GetID())
}

//func (d *Teambition) Get(ctx context.Context, path string) (model.Obj, error) {
// // TODO this is optional
// return nil, errs.NotImplement
//}

func (d *Teambition) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
if u, ok := file.(model.URL); ok {
url := u.URL()
res, _ := base.NoRedirectClient.R().Get(url)
if res.StatusCode() == 302 {
url = res.Header().Get("location")
}
return &model.Link{URL: url}, nil
}
return nil, errors.New("can't convert obj to URL")
}

func (d *Teambition) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
data := base.Json{
"objectType": "collection",
"_projectId": d.ProjectID,
"_creatorId": "",
"created": "",
"updated": "",
"title": dirName,
"color": "blue",
"description": "",
"workCount": 0,
"collectionType": "",
"recentWorks": []interface{}{},
"_parentId": parentDir.GetID(),
"subCount": nil,
}
_, err := d.request("/api/collections", http.MethodPost, func(req *resty.Request) {
req.SetBody(data)
}, nil)
return err
}

func (d *Teambition) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
pre := "/api/works/"
if srcObj.IsDir() {
pre = "/api/collections/"
}
_, err := d.request(pre+srcObj.GetID()+"/move", http.MethodPut, func(req *resty.Request) {
req.SetBody(base.Json{
"_parentId": dstDir.GetID(),
})
}, nil)
return err
}

func (d *Teambition) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
pre := "/api/works/"
data := base.Json{
"fileName": newName,
}
if srcObj.IsDir() {
pre = "/api/collections/"
data = base.Json{
"title": newName,
}
}
_, err := d.request(pre+srcObj.GetID(), http.MethodPut, func(req *resty.Request) {
req.SetBody(data)
}, nil)
return err
}

func (d *Teambition) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
pre := "/api/works/"
if srcObj.IsDir() {
pre = "/api/collections/"
}
_, err := d.request(pre+srcObj.GetID()+"/fork", http.MethodPut, func(req *resty.Request) {
req.SetBody(base.Json{
"_parentId": dstDir.GetID(),
})
}, nil)
return err
}

func (d *Teambition) Remove(ctx context.Context, obj model.Obj) error {
pre := "/api/works/"
if obj.IsDir() {
pre = "/api/collections/"
}
_, err := d.request(pre+obj.GetID()+"/archive", http.MethodPost, nil, nil)
return err
}

func (d *Teambition) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
res, err := d.request("/projects", http.MethodGet, nil, nil)
if err != nil {
return err
}
token := GetBetweenStr(string(res), "strikerAuth":"", "","phoneForLogin")
var newFile *FileUpload
if stream.GetSize() <= 20971520 {
// post upload
newFile, err = d.upload(stream, token)
} else {
// chunk upload
//err = base.ErrNotImplement
newFile, err = d.chunkUpload(stream, token)
}
if err != nil {
return err
}
return d.finishUpload(newFile, dstDir.GetID())
}

func (d *Teambition) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
return nil, errs.NotSupport
}

var _ driver.Driver = (*Teambition)(nil)
27 changes: 27 additions & 0 deletions drivers/teambition/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package teambition

import (
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/op"
)

type Addition struct {
Region string `json:"region" type:"select" options:"china,international" required:"true"`
Cookie string `json:"cookie" required:"true"`
ProjectID string `json:"project_id" required:"true"`
driver.RootFolderID
OrderBy string `json:"order_by" type:"select" options:"fileName,fileSize,updated,created" default:"fileName"`
OrderDirection string `json:"order_direction" type:"select" options:"Asc,Desc" default:"Asc"`
}

var config = driver.Config{
Name: "Teambition",
}

func New() driver.Driver {
return &Teambition{}
}

func init() {
op.RegisterDriver(config, New)
}
68 changes: 68 additions & 0 deletions drivers/teambition/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package teambition

import "time"

type ErrResp struct {
Name string `json:"name"`
Message string `json:"message"`
}

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"`
}
Loading

0 comments on commit 0f2425c

Please sign in to comment.