From b9676182c9511cd0970ecb252a08de6fd1f7a0ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=87=89?= <36558727+Xhofe@users.noreply.github.com> Date: Tue, 19 Jan 2021 17:19:48 +0800 Subject: [PATCH] =?UTF-8?q?:herb:=20=E4=BC=98=E5=8C=96=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- alidrive/req_bean.go | 58 +++++++++++++----------- alidrive/request.go | 102 +++++++++++++++++------------------------- alidrive/resp_bean.go | 39 +++++++++++++--- bootstrap/cmd.go | 6 +++ conf/const.go | 3 +- 5 files changed, 113 insertions(+), 95 deletions(-) diff --git a/alidrive/req_bean.go b/alidrive/req_bean.go index a899dbe1..2028d3bd 100644 --- a/alidrive/req_bean.go +++ b/alidrive/req_bean.go @@ -1,46 +1,52 @@ package alidrive type ListReq struct { - DriveId string `json:"drive_id"` - Fields string `json:"fields"` - ImageThumbnailProcess string `json:"image_thumbnail_process"` - ImageUrlProcess string `json:"image_url_process"` - Limit int `json:"limit"` - Marker string `json:"marker"` - OrderBy string `json:"order_by"` - OrderDirection string `json:"order_direction"` - ParentFileId string `json:"parent_file_id"` - VideoThumbnailProcess string `json:"video_thumbnail_process"` + DriveId string `json:"drive_id"` + Fields string `json:"fields"` + ImageThumbnailProcess string `json:"image_thumbnail_process"` + ImageUrlProcess string `json:"image_url_process"` + Limit int `json:"limit"` + Marker string `json:"marker"` + OrderBy string `json:"order_by"` + OrderDirection string `json:"order_direction"` + ParentFileId string `json:"parent_file_id"` + VideoThumbnailProcess string `json:"video_thumbnail_process"` } type GetReq struct { - DriveId string `json:"drive_id"` - FileId string `json:"file_id"` - ImageThumbnailProcess string `json:"image_thumbnail_process"` - VideoThumbnailProcess string `json:"video_thumbnail_process"` + DriveId string `json:"drive_id"` + FileId string `json:"file_id"` + ImageThumbnailProcess string `json:"image_thumbnail_process"` + VideoThumbnailProcess string `json:"video_thumbnail_process"` } type SearchReq struct { - DriveId string `json:"drive_id"` - ImageThumbnailProcess string `json:"image_thumbnail_process"` - ImageUrlProcess string `json:"image_url_process"` - Limit int `json:"limit"` - Marker string `json:"marker"` - OrderBy string `json:"order_by"`//"type ASC,updated_at DESC" + DriveId string `json:"drive_id"` + ImageThumbnailProcess string `json:"image_thumbnail_process"` + ImageUrlProcess string `json:"image_url_process"` + Limit int `json:"limit"` + Marker string `json:"marker"` + OrderBy string `json:"order_by"` //"type ASC,updated_at DESC" - Query string `json:"query"`// "name match '测试文件'" + Query string `json:"query"` // "name match '测试文件'" - VideoThumbnailProcess string `json:"video_thumbnail_process"` + VideoThumbnailProcess string `json:"video_thumbnail_process"` } type TokenLoginReq struct { - Token string `json:"token"` + Token string `json:"token"` } type GetTokenReq struct { - Code string `json:"code"` + Code string `json:"code"` } type RefreshTokenReq struct { - RefreshToken string `json:"refresh_token"` -} \ No newline at end of file + RefreshToken string `json:"refresh_token"` +} + +type OfficePreviewUrlReq struct { + AccessToken string `json:"access_token"` + DriveId string `json:"drive_id"` + FileId string `json:"file_id"` +} diff --git a/alidrive/request.go b/alidrive/request.go index c680d03e..bcc55e91 100644 --- a/alidrive/request.go +++ b/alidrive/request.go @@ -20,25 +20,11 @@ func GetFile(fileId string) (*File, error) { ImageThumbnailProcess: conf.ImageThumbnailProcess, VideoThumbnailProcess: conf.VideoThumbnailProcess, } - var file File - if body, err := DoPost(url, req,true); err != nil { - log.Errorf("doPost出错:%s",err.Error()) + var resp File + if err := BodyToJson(url, req, &resp, true); err!=nil { return nil,err - }else { - if err = json.Unmarshal(body,&file);err !=nil { - log.Errorf("解析json[%s]出错:%s",string(body),err.Error()) - return nil,err - } } - if file.IsAvailable() { - return &file,nil - } - if file.Code==conf.AccessTokenInvalid { - if RefreshToken() { - return GetFile(fileId) - } - } - return nil,fmt.Errorf(file.Message) + return &resp,nil } func Search(key string,limit int, marker string) (*Files, error) { @@ -53,25 +39,11 @@ func Search(key string,limit int, marker string) (*Files, error) { Query: fmt.Sprintf("name match '%s'",key), VideoThumbnailProcess: conf.VideoThumbnailProcess, } - var files Files - if body, err := DoPost(url, req,true); err != nil { - log.Errorf("doPost出错:%s",err.Error()) + var resp Files + if err := BodyToJson(url, req, &resp, true); err!=nil { return nil,err - }else { - if err = json.Unmarshal(body,&files);err !=nil { - log.Errorf("解析json[%s]出错:%s",string(body),err.Error()) - return nil,err - } } - if files.IsAvailable() { - return &files,nil - } - if files.Code==conf.AccessTokenInvalid { - if RefreshToken() { - return Search(key,limit,marker) - } - } - return nil,fmt.Errorf(files.Message) + return &resp,nil } func GetRoot(limit int,marker string,orderBy string,orderDirection string) (*Files,error) { @@ -92,48 +64,56 @@ func GetList(parent string,limit int,marker string,orderBy string,orderDirection ParentFileId: parent, VideoThumbnailProcess: conf.VideoThumbnailProcess, } - var files Files - if body, err := DoPost(url, req,true); err != nil { - log.Errorf("doPost出错:%s",err.Error()) + var resp Files + if err := BodyToJson(url, req, &resp, true); err!=nil { return nil,err - }else { - if err = json.Unmarshal(body,&files);err !=nil { - log.Errorf("解析json[%s]出错:%s",string(body),err.Error()) - return nil,err - } } - if files.IsAvailable() { - return &files,nil - } - if files.Code==conf.AccessTokenInvalid { - if RefreshToken() { - return GetRoot(limit,marker,orderBy,orderDirection) - } - } - return nil,fmt.Errorf(files.Message) + return &resp,nil } func GetUserInfo() (*UserInfo,error) { url:=conf.Conf.AliDrive.ApiUrl+"/user/get" - var user UserInfo - if body, err := DoPost(url, map[string]interface{}{},true); err != nil { - log.Errorf("doPost出错:%s",err.Error()) + var resp UserInfo + if err := BodyToJson(url, map[string]interface{}{}, &resp, true); err!=nil { return nil,err + } + return &resp,nil +} + +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 + } + return &resp,nil +} + +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,&user);err !=nil { + if err = json.Unmarshal(body,&resp);err!=nil { log.Errorf("解析json[%s]出错:%s",string(body),err.Error()) - return nil,err + return err } } - if user.IsAvailable() { - return &user,nil + if resp.IsAvailable() { + return nil } - if user.Code==conf.AccessTokenInvalid { + if resp.GetCode() == conf.AccessTokenInvalid { + resp.SetCode("") if RefreshToken() { - return GetUserInfo() + return BodyToJson(url,req,resp,auth) } } - return nil,fmt.Errorf(user.Message) + return fmt.Errorf(resp.GetMessage()) } func DoPost(url string,request interface{},auth bool) (body []byte, err error) { diff --git a/alidrive/resp_bean.go b/alidrive/resp_bean.go index ea4fb3ac..00b6ab5c 100644 --- a/alidrive/resp_bean.go +++ b/alidrive/resp_bean.go @@ -8,11 +8,34 @@ import ( "time" ) +type RespHandle interface { + IsAvailable() bool + GetCode() string + GetMessage() string + SetCode(code string) +} + type RespError struct { Code string `json:"code"` Message string `json:"message"` } +func (resp *RespError) IsAvailable() bool { + return resp.Code == "" +} + +func (resp *RespError)GetCode() string { + return resp.Code +} + +func (resp *RespError)GetMessage() string { + return resp.Message +} + +func (resp *RespError)SetCode(code string) { + resp.Code=code +} + type UserInfo struct { RespError DomainId string `json:"domain_id"` @@ -36,7 +59,7 @@ type Files struct { Items []File `json:"items"` NextMarker string `json:"next_marker"` Readme string `json:"readme"` - Paths []Path `json:"paths"` + Paths []Path `json:"paths"` } type Path struct { @@ -72,11 +95,7 @@ type File struct { Url string `json:"url"` ImageMediaMetadata map[string]interface{} `json:"image_media_metadata"` - Paths []Path `json:"paths"` -} - -func (resp *RespError) IsAvailable() bool { - return resp.Code == "" + Paths []Path `json:"paths"` } type TokenLoginResp struct { @@ -104,11 +123,17 @@ type TokenResp struct { DeviceId string `json:"device_id"` } +type OfficePreviewUrlResp struct { + RespError + PreviewUrl string `json:"preview_url"` + AccessToken string `json:"access_token"` +} + func HasPassword(files *Files) string { fileList := files.Items for i, file := range fileList { if strings.HasPrefix(file.Name, ".password-") { - files.Items=fileList[:i+copy(fileList[i:],fileList[i+1:])] + files.Items = fileList[:i+copy(fileList[i:], fileList[i+1:])] return file.Name[10:] } } diff --git a/bootstrap/cmd.go b/bootstrap/cmd.go index 35472a36..a0d4360a 100644 --- a/bootstrap/cmd.go +++ b/bootstrap/cmd.go @@ -2,6 +2,7 @@ package bootstrap import ( "flag" + "fmt" "github.com/Xhofe/alist/conf" serv "github.com/Xhofe/alist/server" "github.com/gin-gonic/gin" @@ -11,6 +12,7 @@ import ( func init() { flag.BoolVar(&conf.Debug,"debug",false,"use debug mode") flag.BoolVar(&conf.Help,"help",false,"show usage help") + flag.BoolVar(&conf.Version,"version",false,"show version info") flag.StringVar(&conf.Con,"conf","conf.yml","config file") } @@ -20,6 +22,10 @@ func Run() { flag.Usage() return } + if conf.Version { + fmt.Println("Current version:"+conf.VERSION) + return + } start() } diff --git a/conf/const.go b/conf/const.go index 13396bba..837239d1 100644 --- a/conf/const.go +++ b/conf/const.go @@ -8,6 +8,7 @@ import ( var( Debug bool Help bool + Version bool Con string Client *http.Client Authorization string @@ -20,7 +21,7 @@ var( var Conf = new(Config) const ( - VERSION="v0.1.5" + VERSION="v0.1.6" ImageThumbnailProcess="image/resize,w_50" VideoThumbnailProcess="video/snapshot,t_0,f_jpg,w_50"