🐝 reformat code
This commit is contained in:
@@ -11,68 +11,68 @@ import (
|
||||
// use token login
|
||||
func TokenLogin() (*TokenLoginResp, error) {
|
||||
log.Infof("尝试使用token登录...")
|
||||
url:="https://auth.aliyundrive.com/v2/oauth/token_login"
|
||||
req:=TokenLoginReq{Token:conf.Conf.AliDrive.LoginToken}
|
||||
log.Debugf("token_login_req:%+v",req)
|
||||
url := "https://auth.aliyundrive.com/v2/oauth/token_login"
|
||||
req := TokenLoginReq{Token: conf.Conf.AliDrive.LoginToken}
|
||||
log.Debugf("token_login_req:%+v", req)
|
||||
var tokenLogin TokenLoginResp
|
||||
if body, err := DoPost(url, req,false); err != nil {
|
||||
log.Errorf("tokenLogin-doPost出错:%s",err.Error())
|
||||
return nil,err
|
||||
}else {
|
||||
if err = json.Unmarshal(body,&tokenLogin);err!=nil {
|
||||
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
||||
return nil,err
|
||||
if body, err := DoPost(url, req, false); err != nil {
|
||||
log.Errorf("tokenLogin-doPost出错:%s", err.Error())
|
||||
return nil, err
|
||||
} else {
|
||||
if err = json.Unmarshal(body, &tokenLogin); err != nil {
|
||||
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if tokenLogin.IsAvailable() {
|
||||
return &tokenLogin,nil
|
||||
return &tokenLogin, nil
|
||||
}
|
||||
return nil,fmt.Errorf("登录token失效,请更换:%s",tokenLogin.Message)
|
||||
return nil, fmt.Errorf("登录token失效,请更换:%s", tokenLogin.Message)
|
||||
}
|
||||
|
||||
// get access token
|
||||
func GetToken(tokenLogin *TokenLoginResp) (*TokenResp,error) {
|
||||
func GetToken(tokenLogin *TokenLoginResp) (*TokenResp, error) {
|
||||
log.Infof("获取API token...")
|
||||
url:="https://websv.aliyundrive.com/token/get"
|
||||
code:=utils.GetCode(tokenLogin.Goto)
|
||||
url := "https://websv.aliyundrive.com/token/get"
|
||||
code := utils.GetCode(tokenLogin.Goto)
|
||||
if code == "" {
|
||||
return nil,fmt.Errorf("获取code出错")
|
||||
return nil, fmt.Errorf("获取code出错")
|
||||
}
|
||||
req:=GetTokenReq{Code:code}
|
||||
req := GetTokenReq{Code: code}
|
||||
var token TokenResp
|
||||
if body, err := DoPost(url, req,false); err != nil {
|
||||
log.Errorf("tokenLogin-doPost出错:%s",err.Error())
|
||||
return nil,err
|
||||
}else {
|
||||
if err = json.Unmarshal(body,&token);err!=nil {
|
||||
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
||||
if body, err := DoPost(url, req, false); err != nil {
|
||||
log.Errorf("tokenLogin-doPost出错:%s", err.Error())
|
||||
return nil, err
|
||||
} else {
|
||||
if err = json.Unmarshal(body, &token); err != nil {
|
||||
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
|
||||
log.Errorf("此处json解析失败应该是code失效")
|
||||
return nil,fmt.Errorf("code失效")
|
||||
return nil, fmt.Errorf("code失效")
|
||||
}
|
||||
}
|
||||
return &token,nil
|
||||
return &token, nil
|
||||
}
|
||||
|
||||
// refresh access_token token by refresh_token
|
||||
func RefreshToken() bool {
|
||||
log.Infof("刷新token...")
|
||||
url:="https://websv.aliyundrive.com/token/refresh"
|
||||
req:=RefreshTokenReq{RefreshToken:conf.Conf.AliDrive.RefreshToken}
|
||||
url := "https://websv.aliyundrive.com/token/refresh"
|
||||
req := RefreshTokenReq{RefreshToken: conf.Conf.AliDrive.RefreshToken}
|
||||
var token TokenResp
|
||||
if body, err := DoPost(url, req,false); err != nil {
|
||||
log.Errorf("tokenLogin-doPost出错:%s",err.Error())
|
||||
if body, err := DoPost(url, req, false); err != nil {
|
||||
log.Errorf("tokenLogin-doPost出错:%s", err.Error())
|
||||
return false
|
||||
}else {
|
||||
if err = json.Unmarshal(body,&token);err!=nil {
|
||||
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
||||
} else {
|
||||
if err = json.Unmarshal(body, &token); err != nil {
|
||||
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
|
||||
log.Errorf("此处json解析失败应该是refresh_token失效")
|
||||
return false
|
||||
}
|
||||
}
|
||||
//刷新成功 更新token并写入文件
|
||||
conf.Conf.AliDrive.AccessToken=token.AccessToken
|
||||
conf.Conf.AliDrive.RefreshToken=token.RefreshToken
|
||||
conf.Authorization=token.TokenType+"\t"+token.AccessToken
|
||||
utils.WriteToYml(conf.Con,conf.Conf)
|
||||
conf.Conf.AliDrive.AccessToken = token.AccessToken
|
||||
conf.Conf.AliDrive.RefreshToken = token.RefreshToken
|
||||
conf.Authorization = token.TokenType + "\t" + token.AccessToken
|
||||
utils.WriteToYml(conf.Con, conf.Conf)
|
||||
return true
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
package alidrive
|
||||
|
||||
var (
|
||||
User *UserInfo
|
||||
User *UserInfo
|
||||
)
|
||||
|
@@ -24,10 +24,10 @@ type GetReq struct {
|
||||
|
||||
// download request bean
|
||||
type DownloadReq struct {
|
||||
DriveId string `json:"drive_id"`
|
||||
FileId string `json:"file_id"`
|
||||
ExpireSec int `json:"expire_sec"`
|
||||
FileName string `json:"file_name"`
|
||||
DriveId string `json:"drive_id"`
|
||||
FileId string `json:"file_id"`
|
||||
ExpireSec int `json:"expire_sec"`
|
||||
FileName string `json:"file_name"`
|
||||
}
|
||||
|
||||
// search request bean
|
||||
|
@@ -14,64 +14,64 @@ import (
|
||||
|
||||
// get file
|
||||
func GetFile(fileId string) (*File, error) {
|
||||
url:=conf.Conf.AliDrive.ApiUrl+"/file/get"
|
||||
req:=GetReq{
|
||||
url := conf.Conf.AliDrive.ApiUrl + "/file/get"
|
||||
req := GetReq{
|
||||
DriveId: User.DefaultDriveId,
|
||||
FileId: fileId,
|
||||
ImageThumbnailProcess: conf.ImageThumbnailProcess,
|
||||
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
||||
}
|
||||
var resp File
|
||||
if err := BodyToJson(url, req, &resp, true); err!=nil {
|
||||
return nil,err
|
||||
if err := BodyToJson(url, req, &resp, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp,nil
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
// get download_url
|
||||
func GetDownLoadUrl(fileId string) (*DownloadResp, error) {
|
||||
url:=conf.Conf.AliDrive.ApiUrl+"/file/get_download_url"
|
||||
req:=DownloadReq{
|
||||
DriveId: User.DefaultDriveId,
|
||||
FileId: fileId,
|
||||
ExpireSec: 14400,
|
||||
url := conf.Conf.AliDrive.ApiUrl + "/file/get_download_url"
|
||||
req := DownloadReq{
|
||||
DriveId: User.DefaultDriveId,
|
||||
FileId: fileId,
|
||||
ExpireSec: 14400,
|
||||
}
|
||||
var resp DownloadResp
|
||||
if err := BodyToJson(url, req, &resp, true); err!=nil {
|
||||
return nil,err
|
||||
if err := BodyToJson(url, req, &resp, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp,nil
|
||||
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{
|
||||
func Search(key string, limit int, marker string) (*Files, error) {
|
||||
url := conf.Conf.AliDrive.ApiUrl + "/file/search"
|
||||
req := SearchReq{
|
||||
DriveId: User.DefaultDriveId,
|
||||
ImageThumbnailProcess: conf.ImageThumbnailProcess,
|
||||
ImageUrlProcess: conf.ImageUrlProcess,
|
||||
Limit: limit,
|
||||
Marker: marker,
|
||||
OrderBy: conf.OrderSearch,
|
||||
Query: fmt.Sprintf("name match '%s'",key),
|
||||
Query: fmt.Sprintf("name match '%s'", key),
|
||||
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
||||
}
|
||||
var resp Files
|
||||
if err := BodyToJson(url, req, &resp, true); err!=nil {
|
||||
return nil,err
|
||||
if err := BodyToJson(url, req, &resp, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp,nil
|
||||
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)
|
||||
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{
|
||||
func GetList(parent string, limit int, marker string, orderBy string, orderDirection string) (*Files, error) {
|
||||
url := conf.Conf.AliDrive.ApiUrl + "/file/list"
|
||||
req := ListReq{
|
||||
DriveId: User.DefaultDriveId,
|
||||
Fields: "*",
|
||||
ImageThumbnailProcess: conf.ImageThumbnailProcess,
|
||||
@@ -84,45 +84,45 @@ func GetList(parent string,limit int,marker string,orderBy string,orderDirection
|
||||
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
||||
}
|
||||
var resp Files
|
||||
if err := BodyToJson(url, req, &resp, true); err!=nil {
|
||||
return nil,err
|
||||
if err := BodyToJson(url, req, &resp, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp,nil
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
// get user info
|
||||
func GetUserInfo() (*UserInfo,error) {
|
||||
url:=conf.Conf.AliDrive.ApiUrl+"/user/get"
|
||||
func GetUserInfo() (*UserInfo, error) {
|
||||
url := conf.Conf.AliDrive.ApiUrl + "/user/get"
|
||||
var resp UserInfo
|
||||
if err := BodyToJson(url, map[string]interface{}{}, &resp, true); err!=nil {
|
||||
return nil,err
|
||||
if err := BodyToJson(url, map[string]interface{}{}, &resp, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp,nil
|
||||
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{
|
||||
func GetOfficePreviewUrl(fileId string) (*OfficePreviewUrlResp, error) {
|
||||
url := conf.Conf.AliDrive.ApiUrl + "/file/get_office_preview_url"
|
||||
req := OfficePreviewUrlReq{
|
||||
AccessToken: conf.Conf.AliDrive.AccessToken,
|
||||
DriveId: User.DefaultDriveId,
|
||||
FileId: fileId,
|
||||
}
|
||||
var resp OfficePreviewUrlResp
|
||||
if err := BodyToJson(url, req, &resp, true); err!=nil {
|
||||
return nil,err
|
||||
if err := BodyToJson(url, req, &resp, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp,nil
|
||||
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())
|
||||
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())
|
||||
return err
|
||||
}else {
|
||||
if err = json.Unmarshal(body,&resp);err!=nil {
|
||||
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
||||
} else {
|
||||
if err = json.Unmarshal(body, &resp); err != nil {
|
||||
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -132,73 +132,73 @@ func BodyToJson(url string, req interface{}, resp RespHandle,auth bool) error {
|
||||
if resp.GetCode() == conf.AccessTokenInvalid {
|
||||
resp.SetCode("")
|
||||
if RefreshToken() {
|
||||
return BodyToJson(url,req,resp,auth)
|
||||
return BodyToJson(url, req, resp, auth)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf(resp.GetMessage())
|
||||
}
|
||||
|
||||
// do post request
|
||||
func DoPost(url string,request interface{},auth bool) (body []byte, err error) {
|
||||
var(
|
||||
func DoPost(url string, request interface{}, auth bool) (body []byte, err error) {
|
||||
var (
|
||||
resp *http.Response
|
||||
)
|
||||
requestBody := new(bytes.Buffer)
|
||||
err = json.NewEncoder(requestBody).Encode(request)
|
||||
if err !=nil {
|
||||
log.Errorf("创建requestBody出错:%s",err.Error())
|
||||
}
|
||||
req,err:=http.NewRequest("POST",url,requestBody)
|
||||
log.Debugf("do_post_req:%+v",req)
|
||||
if err != nil {
|
||||
log.Errorf("创建request出错:%s",err.Error())
|
||||
log.Errorf("创建requestBody出错:%s", err.Error())
|
||||
}
|
||||
req, err := http.NewRequest("POST", url, requestBody)
|
||||
log.Debugf("do_post_req:%+v", req)
|
||||
if err != nil {
|
||||
log.Errorf("创建request出错:%s", err.Error())
|
||||
return
|
||||
}
|
||||
if auth {
|
||||
req.Header.Set("authorization",conf.Authorization)
|
||||
req.Header.Set("authorization", conf.Authorization)
|
||||
}
|
||||
req.Header.Add("content-type","application/json")
|
||||
req.Header.Add("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
|
||||
req.Header.Add("origin","https://aliyundrive.com")
|
||||
req.Header.Add("accept","*/*")
|
||||
req.Header.Add("content-type", "application/json")
|
||||
req.Header.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
|
||||
req.Header.Add("origin", "https://aliyundrive.com")
|
||||
req.Header.Add("accept", "*/*")
|
||||
req.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
|
||||
req.Header.Add("Connection", "keep-alive")
|
||||
|
||||
for retryCount := 3; retryCount >= 0; retryCount-- {
|
||||
if resp,err=conf.Client.Do(req);err!=nil&&strings.Contains(err.Error(),"timeout") {
|
||||
<- time.After(time.Second)
|
||||
}else {
|
||||
if resp, err = conf.Client.Do(req); err != nil && strings.Contains(err.Error(), "timeout") {
|
||||
<-time.After(time.Second)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err!=nil {
|
||||
log.Errorf("请求阿里云盘api时出错:%s",err.Error())
|
||||
if err != nil {
|
||||
log.Errorf("请求阿里云盘api时出错:%s", err.Error())
|
||||
return
|
||||
}
|
||||
if body, err = ioutil.ReadAll(resp.Body); err != nil {
|
||||
log.Errorf("读取api返回内容失败")
|
||||
}
|
||||
log.Debugf("请求返回信息:%s",string(body))
|
||||
log.Debugf("请求返回信息:%s", string(body))
|
||||
return
|
||||
}
|
||||
|
||||
func GetPaths(fileId string) (*[]Path,error) {
|
||||
paths:=make([]Path,0)
|
||||
func GetPaths(fileId string) (*[]Path, error) {
|
||||
paths := make([]Path, 0)
|
||||
for fileId != conf.Conf.AliDrive.RootFolder && fileId != "root" {
|
||||
file,err:=GetFile(fileId)
|
||||
if err !=nil {
|
||||
log.Errorf("获取path出错:%s",err.Error())
|
||||
return nil,err
|
||||
file, err := GetFile(fileId)
|
||||
if err != nil {
|
||||
log.Errorf("获取path出错:%s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
paths=append(paths,Path{
|
||||
paths = append(paths, Path{
|
||||
Name: file.Name,
|
||||
FileId: file.FileId,
|
||||
})
|
||||
fileId=file.ParentFileId
|
||||
fileId = file.ParentFileId
|
||||
}
|
||||
paths=append(paths, Path{
|
||||
paths = append(paths, Path{
|
||||
Name: "Root",
|
||||
FileId: "root",
|
||||
})
|
||||
return &paths,nil
|
||||
return &paths, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user