🌿 优化了一些代码
This commit is contained in:
@ -44,3 +44,9 @@ type GetTokenReq struct {
|
|||||||
type RefreshTokenReq struct {
|
type RefreshTokenReq struct {
|
||||||
RefreshToken string `json:"refresh_token"`
|
RefreshToken string `json:"refresh_token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OfficePreviewUrlReq struct {
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
DriveId string `json:"drive_id"`
|
||||||
|
FileId string `json:"file_id"`
|
||||||
|
}
|
||||||
|
@ -20,25 +20,11 @@ func GetFile(fileId string) (*File, error) {
|
|||||||
ImageThumbnailProcess: conf.ImageThumbnailProcess,
|
ImageThumbnailProcess: conf.ImageThumbnailProcess,
|
||||||
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
||||||
}
|
}
|
||||||
var file File
|
var resp File
|
||||||
if body, err := DoPost(url, req,true); err != nil {
|
if err := BodyToJson(url, req, &resp, true); err!=nil {
|
||||||
log.Errorf("doPost出错:%s",err.Error())
|
|
||||||
return nil,err
|
|
||||||
}else {
|
|
||||||
if err = json.Unmarshal(body,&file);err !=nil {
|
|
||||||
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
|
||||||
return nil,err
|
return nil,err
|
||||||
}
|
}
|
||||||
}
|
return &resp,nil
|
||||||
if file.IsAvailable() {
|
|
||||||
return &file,nil
|
|
||||||
}
|
|
||||||
if file.Code==conf.AccessTokenInvalid {
|
|
||||||
if RefreshToken() {
|
|
||||||
return GetFile(fileId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil,fmt.Errorf(file.Message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Search(key string,limit int, marker string) (*Files, error) {
|
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),
|
Query: fmt.Sprintf("name match '%s'",key),
|
||||||
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
||||||
}
|
}
|
||||||
var files Files
|
var resp Files
|
||||||
if body, err := DoPost(url, req,true); err != nil {
|
if err := BodyToJson(url, req, &resp, true); err!=nil {
|
||||||
log.Errorf("doPost出错:%s",err.Error())
|
|
||||||
return nil,err
|
|
||||||
}else {
|
|
||||||
if err = json.Unmarshal(body,&files);err !=nil {
|
|
||||||
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
|
||||||
return nil,err
|
return nil,err
|
||||||
}
|
}
|
||||||
}
|
return &resp,nil
|
||||||
if files.IsAvailable() {
|
|
||||||
return &files,nil
|
|
||||||
}
|
|
||||||
if files.Code==conf.AccessTokenInvalid {
|
|
||||||
if RefreshToken() {
|
|
||||||
return Search(key,limit,marker)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil,fmt.Errorf(files.Message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRoot(limit int,marker string,orderBy string,orderDirection string) (*Files,error) {
|
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,
|
ParentFileId: parent,
|
||||||
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
||||||
}
|
}
|
||||||
var files Files
|
var resp Files
|
||||||
if body, err := DoPost(url, req,true); err != nil {
|
if err := BodyToJson(url, req, &resp, true); err!=nil {
|
||||||
log.Errorf("doPost出错:%s",err.Error())
|
|
||||||
return nil,err
|
|
||||||
}else {
|
|
||||||
if err = json.Unmarshal(body,&files);err !=nil {
|
|
||||||
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
|
||||||
return nil,err
|
return nil,err
|
||||||
}
|
}
|
||||||
}
|
return &resp,nil
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUserInfo() (*UserInfo,error) {
|
func GetUserInfo() (*UserInfo,error) {
|
||||||
url:=conf.Conf.AliDrive.ApiUrl+"/user/get"
|
url:=conf.Conf.AliDrive.ApiUrl+"/user/get"
|
||||||
var user UserInfo
|
var resp UserInfo
|
||||||
if body, err := DoPost(url, map[string]interface{}{},true); err != nil {
|
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())
|
log.Errorf("doPost出错:%s",err.Error())
|
||||||
return nil,err
|
return err
|
||||||
}else {
|
}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())
|
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
||||||
return nil,err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if user.IsAvailable() {
|
if resp.IsAvailable() {
|
||||||
return &user,nil
|
return nil
|
||||||
}
|
}
|
||||||
if user.Code==conf.AccessTokenInvalid {
|
if resp.GetCode() == conf.AccessTokenInvalid {
|
||||||
|
resp.SetCode("")
|
||||||
if RefreshToken() {
|
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) {
|
func DoPost(url string,request interface{},auth bool) (body []byte, err error) {
|
||||||
|
@ -8,11 +8,34 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type RespHandle interface {
|
||||||
|
IsAvailable() bool
|
||||||
|
GetCode() string
|
||||||
|
GetMessage() string
|
||||||
|
SetCode(code string)
|
||||||
|
}
|
||||||
|
|
||||||
type RespError struct {
|
type RespError struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
Message string `json:"message"`
|
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 {
|
type UserInfo struct {
|
||||||
RespError
|
RespError
|
||||||
DomainId string `json:"domain_id"`
|
DomainId string `json:"domain_id"`
|
||||||
@ -75,10 +98,6 @@ type File struct {
|
|||||||
Paths []Path `json:"paths"`
|
Paths []Path `json:"paths"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (resp *RespError) IsAvailable() bool {
|
|
||||||
return resp.Code == ""
|
|
||||||
}
|
|
||||||
|
|
||||||
type TokenLoginResp struct {
|
type TokenLoginResp struct {
|
||||||
RespError
|
RespError
|
||||||
Goto string `json:"goto"`
|
Goto string `json:"goto"`
|
||||||
@ -104,6 +123,12 @@ type TokenResp struct {
|
|||||||
DeviceId string `json:"device_id"`
|
DeviceId string `json:"device_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OfficePreviewUrlResp struct {
|
||||||
|
RespError
|
||||||
|
PreviewUrl string `json:"preview_url"`
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
}
|
||||||
|
|
||||||
func HasPassword(files *Files) string {
|
func HasPassword(files *Files) string {
|
||||||
fileList := files.Items
|
fileList := files.Items
|
||||||
for i, file := range fileList {
|
for i, file := range fileList {
|
||||||
|
@ -2,6 +2,7 @@ package bootstrap
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
serv "github.com/Xhofe/alist/server"
|
serv "github.com/Xhofe/alist/server"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -11,6 +12,7 @@ import (
|
|||||||
func init() {
|
func init() {
|
||||||
flag.BoolVar(&conf.Debug,"debug",false,"use debug mode")
|
flag.BoolVar(&conf.Debug,"debug",false,"use debug mode")
|
||||||
flag.BoolVar(&conf.Help,"help",false,"show usage help")
|
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")
|
flag.StringVar(&conf.Con,"conf","conf.yml","config file")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,6 +22,10 @@ func Run() {
|
|||||||
flag.Usage()
|
flag.Usage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if conf.Version {
|
||||||
|
fmt.Println("Current version:"+conf.VERSION)
|
||||||
|
return
|
||||||
|
}
|
||||||
start()
|
start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
var(
|
var(
|
||||||
Debug bool
|
Debug bool
|
||||||
Help bool
|
Help bool
|
||||||
|
Version bool
|
||||||
Con string
|
Con string
|
||||||
Client *http.Client
|
Client *http.Client
|
||||||
Authorization string
|
Authorization string
|
||||||
@ -20,7 +21,7 @@ var(
|
|||||||
var Conf = new(Config)
|
var Conf = new(Config)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION="v0.1.5"
|
VERSION="v0.1.6"
|
||||||
|
|
||||||
ImageThumbnailProcess="image/resize,w_50"
|
ImageThumbnailProcess="image/resize,w_50"
|
||||||
VideoThumbnailProcess="video/snapshot,t_0,f_jpg,w_50"
|
VideoThumbnailProcess="video/snapshot,t_0,f_jpg,w_50"
|
||||||
|
Reference in New Issue
Block a user