-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(drivers/alidoc): add DingTalk Docs driver #2543
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
Open
zjhcx
wants to merge
8
commits into
OpenListTeam:main
Choose a base branch
from
zjhcx:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d06f8b1
feat(drivers/alidoc): add DingTalk Docs driver
zjhcx 3dfa30c
chore(drivers/alidoc): update metadata copy
zjhcx 957d341
chore(drivers/alidoc): update read-only alert copy
zjhcx fc36dca
chore(gitignore): remove alidoc ignore rule
zjhcx 640912f
feat(alidoc): support upload move and recycle delete
zjhcx c1205eb
fix(alidoc): commit uploaded files after oss transfer
zjhcx 4330be2
feat(alidoc): support mkdir copy and rename
zjhcx 08b031e
chore(alidoc): remove unused readonly helper
zjhcx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,310 @@ | ||
| package alidoc | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "path" | ||
| "strings" | ||
|
|
||
| "github.com/OpenListTeam/OpenList/v4/drivers/base" | ||
| "github.com/OpenListTeam/OpenList/v4/internal/driver" | ||
| "github.com/OpenListTeam/OpenList/v4/internal/model" | ||
| "github.com/go-resty/resty/v2" | ||
| ) | ||
|
|
||
| type AliDoc struct { | ||
| model.Storage | ||
| Addition | ||
|
|
||
| client *resty.Client | ||
| } | ||
|
|
||
| func (d *AliDoc) Config() driver.Config { | ||
| return config | ||
| } | ||
|
|
||
| func (d *AliDoc) GetAddition() driver.Additional { | ||
| return &d.Addition | ||
| } | ||
|
|
||
| func (d *AliDoc) Init(ctx context.Context) error { | ||
| d.Cookie = strings.TrimSpace(d.Cookie) | ||
| d.RootFolderID = strings.TrimSpace(d.RootFolderID) | ||
| if d.Cookie == "" { | ||
| return fmt.Errorf("cookie is empty") | ||
| } | ||
| if d.RootFolderID == "" { | ||
| return fmt.Errorf("root folder id is empty") | ||
| } | ||
| d.client = newClient() | ||
| if _, err := d.list(ctx, d.RootFolderID); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (d *AliDoc) Drop(ctx context.Context) error { | ||
| d.client = nil | ||
| return nil | ||
| } | ||
|
|
||
| func (d *AliDoc) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) { | ||
| parentID := d.RootFolderID | ||
| parentPath := "/" | ||
| if dir != nil { | ||
| if id := strings.TrimSpace(dir.GetID()); id != "" { | ||
| parentID = id | ||
| } | ||
| if p := dir.GetPath(); p != "" { | ||
| parentPath = p | ||
| } | ||
| } | ||
|
|
||
| items, err := d.list(ctx, parentID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| objs := make([]model.Obj, 0, len(items)) | ||
| for _, item := range items { | ||
| if strings.TrimSpace(item.DentryUUID) == "" || strings.TrimSpace(item.Name) == "" { | ||
| continue | ||
| } | ||
| objs = append(objs, toObj(parentPath, item)) | ||
| } | ||
| return objs, nil | ||
| } | ||
|
|
||
| func (d *AliDoc) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) { | ||
| if file == nil || file.IsDir() { | ||
| return nil, fmt.Errorf("alidoc does not support directory links") | ||
| } | ||
| resp, err := d.download(ctx, file.GetID()) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| url, err := firstDownloadURL(resp) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &model.Link{ | ||
| URL: url, | ||
| Header: http.Header{ | ||
| "User-Agent": []string{base.UserAgent}, | ||
| "Referer": []string{apiBase + "/"}, | ||
| }, | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *AliDoc) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) { | ||
| dirName = strings.TrimSpace(dirName) | ||
| if dirName == "" { | ||
| return nil, fmt.Errorf("folder name is empty") | ||
| } | ||
|
|
||
| parentID := d.RootFolderID | ||
| parentPath := "/" | ||
| if parentDir != nil { | ||
| if id := strings.TrimSpace(parentDir.GetID()); id != "" { | ||
| parentID = id | ||
| } | ||
| if p := parentDir.GetPath(); p != "" { | ||
| parentPath = p | ||
| } | ||
| } | ||
|
|
||
| var result apiResp | ||
| resp, err := d.request(ctx). | ||
| SetBody(map[string]string{ | ||
| "dentryType": "folder", | ||
| "name": dirName, | ||
| "parentDentryUuid": parentID, | ||
| "conflictHandleStrategy": "auto_rename", | ||
| }). | ||
| SetResult(&result). | ||
| SetError(&result). | ||
| Post(apiBase + "/box/api/v2/dentry/createfolder") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := checkResp(resp, result); err != nil { | ||
| return nil, err | ||
| } | ||
| return &Object{ | ||
| Object: model.Object{ | ||
| Path: joinPath(parentPath, dirName), | ||
| Name: dirName, | ||
| IsFolder: true, | ||
| }, | ||
| DentryType: "folder", | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *AliDoc) Move(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) { | ||
| if srcObj == nil { | ||
| return nil, fmt.Errorf("source object is nil") | ||
| } | ||
| if dstDir == nil { | ||
| return nil, fmt.Errorf("destination directory is nil") | ||
| } | ||
| sourceID := strings.TrimSpace(srcObj.GetID()) | ||
| targetID := strings.TrimSpace(dstDir.GetID()) | ||
| if sourceID == "" { | ||
| return nil, fmt.Errorf("source dentry uuid is empty") | ||
| } | ||
| if targetID == "" { | ||
| return nil, fmt.Errorf("target parent dentry uuid is empty") | ||
| } | ||
|
|
||
| var result apiResp | ||
| resp, err := d.request(ctx). | ||
| SetBody(map[string]interface{}{ | ||
| "targetParentDentryUuid": targetID, | ||
| "sourceDentryUuid": sourceID, | ||
| "operateFrom": 1, | ||
| }). | ||
| SetResult(&result). | ||
| SetError(&result). | ||
| Post(apiBase + "/box/api/v2/dentry/move") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := checkResp(resp, result); err != nil { | ||
| return nil, err | ||
| } | ||
| return srcObj, nil | ||
| } | ||
|
|
||
| func (d *AliDoc) Rename(ctx context.Context, srcObj model.Obj, newName string) (model.Obj, error) { | ||
| if srcObj == nil { | ||
| return nil, fmt.Errorf("source object is nil") | ||
| } | ||
| newName = strings.TrimSpace(newName) | ||
| if newName == "" { | ||
| return nil, fmt.Errorf("new name is empty") | ||
| } | ||
| dentryUUID := strings.TrimSpace(srcObj.GetID()) | ||
| if dentryUUID == "" { | ||
| return nil, fmt.Errorf("dentry uuid is empty") | ||
| } | ||
|
|
||
| var result apiResp | ||
| resp, err := d.request(ctx). | ||
| SetBody(map[string]string{ | ||
| "dentryUuid": dentryUUID, | ||
| "name": newName, | ||
| }). | ||
| SetResult(&result). | ||
| SetError(&result). | ||
| Post(apiBase + "/box/api/v2/dentry/rename") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := checkResp(resp, result); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| newPath := srcObj.GetPath() | ||
| if newPath != "" { | ||
| newPath = path.Join(path.Dir(newPath), newName) | ||
| } | ||
| return &Object{ | ||
| Object: model.Object{ | ||
| ID: srcObj.GetID(), | ||
| Path: newPath, | ||
| Name: newName, | ||
| Size: srcObj.GetSize(), | ||
| Modified: srcObj.ModTime(), | ||
| Ctime: srcObj.CreateTime(), | ||
| IsFolder: srcObj.IsDir(), | ||
| HashInfo: srcObj.GetHash(), | ||
| }, | ||
| DentryType: pickAliDocDentryType(srcObj), | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *AliDoc) Copy(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) { | ||
| if srcObj == nil { | ||
| return nil, fmt.Errorf("source object is nil") | ||
| } | ||
| if dstDir == nil { | ||
| return nil, fmt.Errorf("destination directory is nil") | ||
| } | ||
| sourceID := strings.TrimSpace(srcObj.GetID()) | ||
| targetID := strings.TrimSpace(dstDir.GetID()) | ||
| if sourceID == "" { | ||
| return nil, fmt.Errorf("source dentry uuid is empty") | ||
| } | ||
| if targetID == "" { | ||
| return nil, fmt.Errorf("target parent dentry uuid is empty") | ||
| } | ||
|
|
||
| var result apiResp | ||
| resp, err := d.request(ctx). | ||
| SetBody(map[string]interface{}{ | ||
| "sourceDentryUuid": sourceID, | ||
| "targetParentDentryUuid": targetID, | ||
| "operateFrom": 1, | ||
| "onlyCopyMeta": false, | ||
| }). | ||
| SetResult(&result). | ||
| SetError(&result). | ||
| Post(apiBase + "/box/api/v2/dentry/copy") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := checkResp(resp, result); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &Object{ | ||
| Object: model.Object{ | ||
| Path: joinPath(dstDir.GetPath(), srcObj.GetName()), | ||
| Name: srcObj.GetName(), | ||
| Size: srcObj.GetSize(), | ||
| Modified: srcObj.ModTime(), | ||
| Ctime: srcObj.CreateTime(), | ||
| IsFolder: srcObj.IsDir(), | ||
| HashInfo: srcObj.GetHash(), | ||
| }, | ||
| DentryType: pickAliDocDentryType(srcObj), | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *AliDoc) Remove(ctx context.Context, obj model.Obj) error { | ||
| if obj == nil { | ||
| return fmt.Errorf("object is nil") | ||
| } | ||
| dentryUUID := strings.TrimSpace(obj.GetID()) | ||
| if dentryUUID == "" { | ||
| return fmt.Errorf("dentry uuid is empty") | ||
| } | ||
|
|
||
| var result apiResp | ||
| resp, err := d.request(ctx). | ||
| SetBody(map[string]string{ | ||
| "dentryUuid": dentryUUID, | ||
| }). | ||
| SetResult(&result). | ||
| SetError(&result). | ||
| Post(apiBase + "/box/api/v1/dentry/recycle") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return checkResp(resp, result) | ||
| } | ||
|
|
||
| func (d *AliDoc) Put(ctx context.Context, dstDir model.Obj, file model.FileStreamer, up driver.UpdateProgress) (model.Obj, error) { | ||
| return d.put(ctx, dstDir, file, up) | ||
| } | ||
|
|
||
| var ( | ||
| _ driver.Driver = (*AliDoc)(nil) | ||
| _ driver.MkdirResult = (*AliDoc)(nil) | ||
| _ driver.MoveResult = (*AliDoc)(nil) | ||
| _ driver.RenameResult = (*AliDoc)(nil) | ||
| _ driver.CopyResult = (*AliDoc)(nil) | ||
| _ driver.Remove = (*AliDoc)(nil) | ||
| _ driver.PutResult = (*AliDoc)(nil) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package alidoc | ||
|
|
||
| import ( | ||
| "github.com/OpenListTeam/OpenList/v4/internal/driver" | ||
| "github.com/OpenListTeam/OpenList/v4/internal/op" | ||
| ) | ||
|
|
||
| type Addition struct { | ||
| driver.RootID | ||
| Cookie string `json:"cookie" type:"text" required:"true" help:"钉钉文档网页 Cookie"` | ||
| } | ||
|
|
||
| var config = driver.Config{ | ||
| Name: "AliDoc", | ||
| LocalSort: true, | ||
| DefaultRoot: "", | ||
| Alert: "info|This driver supports accessing DingDrive through DingTalk Docs, including listing, download, upload, move, and recycle delete.", | ||
| } | ||
|
|
||
| func init() { | ||
| op.RegisterDriver(func() driver.Driver { | ||
| return &AliDoc{} | ||
| }) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以删了