📝 add notes

This commit is contained in:
微凉
2021-02-04 10:02:34 +08:00
parent 3b2a729dc6
commit e146054679
26 changed files with 91 additions and 28 deletions

View File

@@ -8,6 +8,7 @@ import (
log "github.com/sirupsen/logrus"
)
// use token login
func TokenLogin() (*TokenLoginResp, error) {
log.Infof("尝试使用token登录...")
url:="https://auth.aliyundrive.com/v2/oauth/token_login"
@@ -29,6 +30,7 @@ func TokenLogin() (*TokenLoginResp, error) {
return nil,fmt.Errorf("登录token失效,请更换:%s",tokenLogin.Message)
}
// get access token
func GetToken(tokenLogin *TokenLoginResp) (*TokenResp,error) {
log.Infof("获取API token...")
url:="https://websv.aliyundrive.com/token/get"
@@ -51,6 +53,7 @@ func GetToken(tokenLogin *TokenLoginResp) (*TokenResp,error) {
return &token,nil
}
// refresh access_token token by refresh_token
func RefreshToken() bool {
log.Infof("刷新token...")
url:="https://websv.aliyundrive.com/token/refresh"

View File

@@ -1,5 +1,6 @@
package alidrive
// list request bean
type ListReq struct {
DriveId string `json:"drive_id"`
Fields string `json:"fields"`
@@ -13,6 +14,7 @@ type ListReq struct {
VideoThumbnailProcess string `json:"video_thumbnail_process"`
}
// get request bean
type GetReq struct {
DriveId string `json:"drive_id"`
FileId string `json:"file_id"`
@@ -20,6 +22,7 @@ type GetReq struct {
VideoThumbnailProcess string `json:"video_thumbnail_process"`
}
// search request bean
type SearchReq struct {
DriveId string `json:"drive_id"`
ImageThumbnailProcess string `json:"image_thumbnail_process"`
@@ -33,18 +36,22 @@ type SearchReq struct {
VideoThumbnailProcess string `json:"video_thumbnail_process"`
}
// token_login request bean
type TokenLoginReq struct {
Token string `json:"token"`
}
// get_token request bean
type GetTokenReq struct {
Code string `json:"code"`
}
// refresh_token request bean
type RefreshTokenReq struct {
RefreshToken string `json:"refresh_token"`
}
// office_preview_url request bean
type OfficePreviewUrlReq struct {
AccessToken string `json:"access_token"`
DriveId string `json:"drive_id"`

View File

@@ -12,6 +12,7 @@ import (
"time"
)
// get file
func GetFile(fileId string) (*File, error) {
url:=conf.Conf.AliDrive.ApiUrl+"/file/get"
req:=GetReq{
@@ -27,6 +28,7 @@ func GetFile(fileId string) (*File, error) {
return &resp,nil
}
// search by keyword
func Search(key string,limit int, marker string) (*Files, error) {
url:=conf.Conf.AliDrive.ApiUrl+"/file/search"
req:=SearchReq{
@@ -46,10 +48,12 @@ func Search(key string,limit int, marker string) (*Files, error) {
return &resp,nil
}
// get root folder
func GetRoot(limit int,marker string,orderBy string,orderDirection string) (*Files,error) {
return GetList(conf.Conf.AliDrive.RootFolder,limit,marker,orderBy,orderDirection)
}
// get folder list by file_id
func GetList(parent string,limit int,marker string,orderBy string,orderDirection string) (*Files,error) {
url:=conf.Conf.AliDrive.ApiUrl+"/file/list"
req:=ListReq{
@@ -71,6 +75,7 @@ func GetList(parent string,limit int,marker string,orderBy string,orderDirection
return &resp,nil
}
// get user info
func GetUserInfo() (*UserInfo,error) {
url:=conf.Conf.AliDrive.ApiUrl+"/user/get"
var resp UserInfo
@@ -80,6 +85,7 @@ func GetUserInfo() (*UserInfo,error) {
return &resp,nil
}
// get office preview url and token
func GetOfficePreviewUrl(fileId string) (*OfficePreviewUrlResp,error) {
url:=conf.Conf.AliDrive.ApiUrl+"/file/get_office_preview_url"
req:=OfficePreviewUrlReq{
@@ -94,6 +100,7 @@ func GetOfficePreviewUrl(fileId string) (*OfficePreviewUrlResp,error) {
return &resp,nil
}
// convert body to json
func BodyToJson(url string, req interface{}, resp RespHandle,auth bool) error {
if body,err := DoPost(url,req,auth);err!=nil {
log.Errorf("doPost出错:%s",err.Error())
@@ -116,6 +123,7 @@ func BodyToJson(url string, req interface{}, resp RespHandle,auth bool) error {
return fmt.Errorf(resp.GetMessage())
}
// do post request
func DoPost(url string,request interface{},auth bool) (body []byte, err error) {
var(
resp *http.Response

View File

@@ -8,13 +8,15 @@ import (
"time"
)
// response bean methods
type RespHandle interface {
IsAvailable() bool
GetCode() string
GetMessage() string
SetCode(code string)
IsAvailable() bool // check available
GetCode() string // get err code
GetMessage() string // get err message
SetCode(code string) // set err code
}
// common response bean
type RespError struct {
Code string `json:"code"`
Message string `json:"message"`
@@ -36,6 +38,7 @@ func (resp *RespError)SetCode(code string) {
resp.Code=code
}
// user_info response bean
type UserInfo struct {
RespError
DomainId string `json:"domain_id"`
@@ -54,19 +57,22 @@ type UserInfo struct {
UserData map[string]interface{} `json:"user_data"`
}
// folder files response bean
type Files struct {
RespError
Items []File `json:"items"`
NextMarker string `json:"next_marker"`
Readme string `json:"readme"`
Readme string `json:"readme"` // Deprecated
Paths []Path `json:"paths"`
}
// path bean
type Path struct {
Name string `json:"name"`
FileId string `json:"file_id"`
}
// file response bean
type File struct {
RespError
DriveId string `json:"drive_id"`
@@ -98,11 +104,13 @@ type File struct {
Paths []Path `json:"paths"`
}
// token_login response bean
type TokenLoginResp struct {
RespError
Goto string `json:"goto"`
}
// token response bean
type TokenResp struct {
RespError
AccessToken string `json:"access_token"`
@@ -123,12 +131,14 @@ type TokenResp struct {
DeviceId string `json:"device_id"`
}
// office_preview_url response bean
type OfficePreviewUrlResp struct {
RespError
PreviewUrl string `json:"preview_url"`
AccessToken string `json:"access_token"`
}
// check password
func HasPassword(files *Files) string {
fileList := files.Items
for i, file := range fileList {
@@ -140,6 +150,7 @@ func HasPassword(files *Files) string {
return ""
}
// Deprecated: check readme, implemented by the front end now
func HasReadme(files *Files) string {
fileList := files.Items
for _, file := range fileList {