From 76bc6e68ae0f384b512531d0e0219ab9e95e78af Mon Sep 17 00:00:00 2001 From: link Date: Thu, 2 Feb 2023 15:59:33 +0000 Subject: [PATCH] Modify file list return data --- drivers/dropbox/meta.go | 8 +-- drivers/google_drive/meta.go | 6 ++- main.go | 6 ++- model/req.go | 15 +++--- pkg/utils/httper/drive.go | 5 ++ route/v1.go | 13 +++-- route/v1/file.go | 52 +++++++------------ route/v1/file_read.go | 33 ++++++------ route/v1/recover.go | 99 ++++++++++++++++-------------------- route/v1/storage.go | 12 ++++- 10 files changed, 125 insertions(+), 124 deletions(-) diff --git a/drivers/dropbox/meta.go b/drivers/dropbox/meta.go index ac494b3..4ffc4bf 100644 --- a/drivers/dropbox/meta.go +++ b/drivers/dropbox/meta.go @@ -5,7 +5,9 @@ import ( "github.com/IceWhaleTech/CasaOS/internal/op" ) -const ICONURL = "https://i.pcmag.com/imagery/reviews/02PHW91bUvLOs36qNbBzOiR-12.fit_scale.size_760x427.v1569471162.png" +const ICONURL = "./img/driver/Dropbox.svg" +const APPKEY = "onr2ic0c0m97mxr" +const APPSECRET = "nd3cjtikbxyj3pz" type Addition struct { driver.RootID @@ -13,8 +15,8 @@ type Addition struct { AppKey string `json:"app_key" type:"string" default:"onr2ic0c0m97mxr" omit:"true"` AppSecret string `json:"app_secret" type:"string" default:"nd3cjtikbxyj3pz" omit:"true"` OrderDirection string `json:"order_direction" type:"select" options:"asc,desc" omit:"true"` - AuthUrl string `json:"auth_url" type:"string" default:"https://www.dropbox.com/oauth2/authorize?client_id=onr2ic0c0m97mxr&redirect_uri=https://test-get.casaos.io&response_type=code&token_access_type=offline&state=${HOST}%2Fv1%2Frecover%2FDropbox"` - Icon string `json:"icon" type:"string" default:"https://i.pcmag.com/imagery/reviews/02PHW91bUvLOs36qNbBzOiR-12.fit_scale.size_760x427.v1569471162.png"` + AuthUrl string `json:"auth_url" type:"string" default:"https://www.dropbox.com/oauth2/authorize?client_id=onr2ic0c0m97mxr&redirect_uri=https://test-get.casaos.io&response_type=code&token_access_type=offline&state=${HOST}%2Fv1%2Frecover%2FDropbox&&force_reapprove=true&force_reauthentication=true"` + Icon string `json:"icon" type:"string" default:"./img/driver/Dropbox.svg"` Code string `json:"code" type:"string" help:"code from auth_url" omit:"true"` } diff --git a/drivers/google_drive/meta.go b/drivers/google_drive/meta.go index 16e63fb..0cf88b5 100644 --- a/drivers/google_drive/meta.go +++ b/drivers/google_drive/meta.go @@ -5,7 +5,9 @@ import ( "github.com/IceWhaleTech/CasaOS/internal/op" ) -const ICONURL = "https://i.pcmag.com/imagery/reviews/02PHW91bUvLOs36qNbBzOiR-12.fit_scale.size_760x427.v1569471162.png" +const ICONURL = "./img/driver/GoogleDrive.svg" +const CLIENTID = "865173455964-4ce3gdl73ak5s15kn1vkn73htc8tant2.apps.googleusercontent.com" +const CLIENTSECRET = "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" type Addition struct { driver.RootID @@ -16,7 +18,7 @@ type Addition struct { ClientSecret string `json:"client_secret" required:"true" default:"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" omit:"true"` ChunkSize int64 `json:"chunk_size" type:"number" help:"chunk size while uploading (unit: MB)" omit:"true"` AuthUrl string `json:"auth_url" type:"string" default:"https://accounts.google.com/o/oauth2/auth/oauthchooseaccount?response_type=code&client_id=865173455964-4ce3gdl73ak5s15kn1vkn73htc8tant2.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Ftest-get.casaos.io&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&access_type=offline&approval_prompt=force&state=${HOST}%2Fv1%2Frecover%2FGoogleDrive&service=lso&o2v=1&flowName=GeneralOAuthFlow"` - Icon string `json:"icon" type:"string" default:"https://i.pcmag.com/imagery/reviews/02PHW91bUvLOs36qNbBzOiR-12.fit_scale.size_760x427.v1569471162.png"` + Icon string `json:"icon" type:"string" default:"./img/driver/GoogleDrive.svg"` Code string `json:"code" type:"string" help:"code from auth_url" omit:"true"` } diff --git a/main.go b/main.go index 0133cfb..8ef231b 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "github.com/IceWhaleTech/CasaOS-Common/model" "github.com/IceWhaleTech/CasaOS-Common/utils/constants" "github.com/IceWhaleTech/CasaOS-Common/utils/logger" + "github.com/gin-gonic/gin" util_http "github.com/IceWhaleTech/CasaOS-Common/utils/http" @@ -100,6 +101,9 @@ func main() { v1Router := route.InitV1Router() defer service.SocketServer.Close() + v1Router.GET("/v1/socketio/*any", gin.WrapH(service.SocketServer)) + v1Router.POST("/v1/socketio/*any", gin.WrapH(service.SocketServer)) + v2Router := route.InitV2Router() v2DocRouter := route.InitV2DocRouter(_docHTML, _docYAML) @@ -146,7 +150,7 @@ func main() { "/v1/notify", "/v1/socketio", "/v1/driver", - "/v1/storage", + "/v1/cloud", "/v1/recover", route.V2APIPath, route.V2DocPath, diff --git a/model/req.go b/model/req.go index fe3a08b..cee1107 100644 --- a/model/req.go +++ b/model/req.go @@ -1,8 +1,8 @@ package model type PageReq struct { - Page int `json:"page" form:"page"` - PerPage int `json:"per_page" form:"per_page"` + Index int `json:"page" form:"index"` + Size int `json:"size" form:"size"` } const MaxUint = ^uint(0) @@ -11,10 +11,13 @@ const MaxInt = int(MaxUint >> 1) const MinInt = -MaxInt - 1 func (p *PageReq) Validate() { - if p.Page < 1 { - p.Page = 1 + if p.Index < 1 { + p.Index = 1 } - if p.PerPage < 1 { - p.PerPage = MaxInt + if p.Size < 1 { + p.Size = 100000 } + // if p.PerPage < 1 { + // p.PerPage = MaxInt + // } } diff --git a/pkg/utils/httper/drive.go b/pkg/utils/httper/drive.go index b64fd1b..5693d28 100644 --- a/pkg/utils/httper/drive.go +++ b/pkg/utils/httper/drive.go @@ -17,6 +17,11 @@ type MountList struct { Icon string `json:"Icon"` } `json:"mountPoints"` } +type MountPoint struct { + MountPoint string `json:"mount_point"` + Fs string `json:"fs"` + Icon string `json:"icon"` +} type MountResult struct { Error string `json:"error"` Input struct { diff --git a/route/v1.go b/route/v1.go index ea09d4b..a818d35 100644 --- a/route/v1.go +++ b/route/v1.go @@ -7,7 +7,6 @@ import ( "github.com/IceWhaleTech/CasaOS-Common/utils/jwt" "github.com/IceWhaleTech/CasaOS/pkg/config" v1 "github.com/IceWhaleTech/CasaOS/route/v1" - "github.com/IceWhaleTech/CasaOS/service" "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin" @@ -98,11 +97,11 @@ func InitV1Router() *gin.Engine { // v1FileGroup.GET("/download", v1.UserFileDownloadCommonService) } - v1StorageGroup := v1Group.Group("/storage") - v1StorageGroup.Use() + v1CloudGroup := v1Group.Group("/cloud") + v1CloudGroup.Use() { - v1StorageGroup.GET("", v1.ListStorages) - v1StorageGroup.DELETE("", v1.DeleteStorage) + v1CloudGroup.GET("", v1.ListStorages) + v1CloudGroup.DELETE("", v1.DeleteStorage) } v1DriverGroup := v1Group.Group("/driver") v1DriverGroup.Use() @@ -162,8 +161,8 @@ func InitV1Router() *gin.Engine { } // socketio - v1Group.GET("/socketio/*any", gin.WrapH(service.SocketServer)) - v1Group.POST("/socketio/*any", gin.WrapH(service.SocketServer)) + // v1Group.GET("/socketio/*any", gin.WrapH(service.SocketServer)) + // v1Group.POST("/socketio/*any", gin.WrapH(service.SocketServer)) return r } diff --git a/route/v1/file.go b/route/v1/file.go index 2646147..e5cd49c 100644 --- a/route/v1/file.go +++ b/route/v1/file.go @@ -256,40 +256,23 @@ func DirPath(c *gin.Context) { return } req.Validate() - storage, _, _ := service.MyService.StoragePath().GetStorageAndActualPath(req.Path) - if storage != nil { - req.Validate() - objs, err := service.MyService.FsService().FList(c, req.Path, req.Refresh) - if err != nil { - c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()}) - return - } - total, objs := pagination(objs, &req.PageReq) - provider := "unknown" - storage, err := service.MyService.FsService().GetStorage(req.Path) - if err == nil { - provider = storage.GetStorage().Driver - } - c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: FsListResp{ - Content: toObjsResp(objs, req.Path, false), - Total: int64(total), - Readme: "", - Write: false, - Provider: provider, - }}) - return - } info := service.MyService.System().GetDirPath(req.Path) shares := service.MyService.Shares().GetSharesList() sharesMap := make(map[string]string) for _, v := range shares { sharesMap[v.Path] = fmt.Sprint(v.ID) } - - for i := 0; i < len(info); i++ { + // if len(info) <= (req.Page-1)*req.Size { + // c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.INVALID_PARAMS), Data: "page out of range"}) + // return + // } + forEnd := req.Index * req.Size + if forEnd > len(info) { + forEnd = len(info) + } + for i := (req.Index - 1) * req.Size; i < forEnd; i++ { if v, ok := sharesMap[info[i].Path]; ok { ex := make(map[string]interface{}) - shareEx := make(map[string]string) shareEx["shared"] = "true" shareEx["id"] = v @@ -314,11 +297,10 @@ func DirPath(c *gin.Context) { } pathList := []ObjResp{} - for i := 0; i < len(info); i++ { + for i := (req.Index - 1) * req.Size; i < forEnd; i++ { if info[i].Name == ".temp" && info[i].IsDir { continue } - if _, ok := fileQueue[info[i].Path]; !ok { t := ObjResp{} t.IsDir = info[i].IsDir @@ -326,15 +308,19 @@ func DirPath(c *gin.Context) { t.Modified = info[i].Date t.Size = info[i].Size t.Path = info[i].Path + t.Extensions = info[i].Extensions pathList = append(pathList, t) + } } flist := FsListResp{ - Content: pathList, - Total: int64(len(pathList)), - Readme: "", - Write: true, - Provider: "local", + Content: pathList, + Total: int64(len(info)), + // Readme: "", + // Write: true, + // Provider: "local", + Index: req.Index, + Size: req.Size, } c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: flist}) } diff --git a/route/v1/file_read.go b/route/v1/file_read.go index 377116c..95a01e8 100644 --- a/route/v1/file_read.go +++ b/route/v1/file_read.go @@ -13,25 +13,28 @@ import ( type ListReq struct { model.PageReq - Path string `json:"path" form:"path"` - Refresh bool `json:"refresh"` + Path string `json:"path" form:"path"` + //Refresh bool `json:"refresh"` } type ObjResp struct { - Name string `json:"name"` - Size int64 `json:"size"` - IsDir bool `json:"is_dir"` - Modified time.Time `json:"modified"` - Sign string `json:"sign"` - Thumb string `json:"thumb"` - Type int `json:"type"` - Path string `json:"path"` + Name string `json:"name"` + Size int64 `json:"size"` + IsDir bool `json:"is_dir"` + Modified time.Time `json:"modified"` + Sign string `json:"sign"` + Thumb string `json:"thumb"` + Type int `json:"type"` + Path string `json:"path"` + Extensions map[string]interface{} `json:"extensions"` } type FsListResp struct { Content []ObjResp `json:"content"` Total int64 `json:"total"` - Readme string `json:"readme"` - Write bool `json:"write"` - Provider string `json:"provider"` + Readme string `json:"readme,omitempty"` + Write bool `json:"write,omitempty"` + Provider string `json:"provider,omitempty"` + Index int `json:"index"` + Size int `json:"size"` } func FsList(c *gin.Context) { @@ -41,7 +44,7 @@ func FsList(c *gin.Context) { return } req.Validate() - objs, err := service.MyService.FsService().FList(c, req.Path, req.Refresh) + objs, err := service.MyService.FsService().FList(c, req.Path, false) if err != nil { c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()}) return @@ -61,7 +64,7 @@ func FsList(c *gin.Context) { }}) } func pagination(objs []model.Obj, req *model.PageReq) (int, []model.Obj) { - pageIndex, pageSize := req.Page, req.PerPage + pageIndex, pageSize := req.Index, req.Size total := len(objs) start := (pageIndex - 1) * pageSize if start > total { diff --git a/route/v1/recover.go b/route/v1/recover.go index 76affcf..2cec3b4 100644 --- a/route/v1/recover.go +++ b/route/v1/recover.go @@ -1,13 +1,11 @@ package v1 import ( - "strconv" "strings" "time" "github.com/IceWhaleTech/CasaOS/drivers/dropbox" "github.com/IceWhaleTech/CasaOS/drivers/google_drive" - "github.com/IceWhaleTech/CasaOS/internal/op" "github.com/IceWhaleTech/CasaOS/service" "github.com/gin-gonic/gin" ) @@ -17,45 +15,39 @@ func GetRecoverStorage(c *gin.Context) { t := c.Param("type") currentTime := time.Now().UTC() currentDate := time.Now().UTC().Format("2006-01-02") - // timeStr := time.Now().Format("20060102150405") + notify := make(map[string]interface{}) if t == "GoogleDrive" { - - gd := op.GetDriverInfoMap()[t] - add := google_drive.Addition{} add.Code = c.Query("code") if len(add.Code) == 0 { - c.String(200, `

code cannot be empty

`) + c.String(200, `

Code cannot be empty

`) + notify["status"] = "fail" + notify["message"] = "Code cannot be empty" + service.MyService.Notify().SendNotify("recover_status", notify) return } + add.RootFolderID = "root" - for _, v := range gd { - if v.Name == "client_id" { - add.ClientID = v.Default - } - if v.Name == "client_secret" { - add.ClientSecret = v.Default - } - if v.Name == "chunk_size" { - cs, err := strconv.ParseInt(v.Default, 10, 64) - if err != nil { - cs = 5 - } - add.ChunkSize = cs - } - } + add.ClientID = google_drive.CLIENTID + add.ClientSecret = google_drive.CLIENTSECRET var google_drive google_drive.GoogleDrive google_drive.Addition = add err := google_drive.Init(c) if err != nil { - c.String(200, `

Initialization failure:`+err.Error()+`

`) + c.String(200, `

Initialization failure:`+err.Error()+`

`) + notify["status"] = "fail" + notify["message"] = "Initialization failure" + service.MyService.Notify().SendNotify("recover_status", notify) return } username, err := google_drive.GetUserInfo(c) if err != nil { - c.String(200, `

Failed to get user information:`+err.Error()+`

`) + c.String(200, `

Failed to get user information:`+err.Error()+`

`) + notify["status"] = "fail" + notify["message"] = "Failed to get user information" + service.MyService.Notify().SendNotify("recover_status", notify) return } if len(username) > 0 { @@ -65,8 +57,11 @@ func GetRecoverStorage(c *gin.Context) { username += "_drive" dataMap, _ := service.MyService.Storage().GetConfigByName(username) if len(dataMap) > 0 { - c.String(200, `

The same configuration has been added

`) + c.String(200, `

The same configuration has been added

`) service.MyService.Storage().CheckAndMountByName(username) + notify["status"] = "warn" + notify["message"] = "The same configuration has been added" + service.MyService.Notify().SendNotify("recover_status", notify) return } dmap := make(map[string]string) @@ -74,54 +69,42 @@ func GetRecoverStorage(c *gin.Context) { dmap["client_secret"] = add.ClientSecret dmap["scope"] = "drive" dmap["mount_point"] = "/mnt/" + username - dmap["token"] = `{"access_token":"` + google_drive.AccessToken + `","token_type":"Bearer","refresh_token":"` + google_drive.RefreshToken + `","expiry":"` + currentDate + `T` + currentTime.Add(time.Hour*1).Format("15:04:05") + `Z"}` - // data.SetValue(username, "type", "drive") - // data.SetValue(username, "client_id", "865173455964-4ce3gdl73ak5s15kn1vkn73htc8tant2.apps.googleusercontent.com") - // data.SetValue(username, "client_secret", "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") - // data.SetValue(username, "scope", "drive") - // data.SetValue(username, "mount_point", "/mnt/"+username) - // data.SetValue(username, "token", `{"access_token":"`+google_drive.AccessToken+`","token_type":"Bearer","refresh_token":"`+google_drive.RefreshToken+`","expiry":"`+currentDate+`T`+currentTime.Add(time.Hour*1).Format("15:04:05")+`Z"}`) - // e = data.Save() - // if e != nil { - // c.String(200, `

保存配置失败:`+e.Error()+`

`) - // return - // } + dmap["token"] = `{"access_token":"` + google_drive.AccessToken + `","token_type":"Bearer","refresh_token":"` + google_drive.RefreshToken + `","expiry":"` + currentDate + `T` + currentTime.Add(time.Hour*1).Add(time.Minute*50).Format("15:04:05") + `Z"}` service.MyService.Storage().CreateConfig(dmap, username, "drive") service.MyService.Storage().MountStorage("/mnt/"+username, username+":") notify := make(map[string]interface{}) notify["status"] = "success" + notify["message"] = "Success" service.MyService.Notify().SendNotify("recover_status", notify) } else if t == "Dropbox" { - - //mountPath += timeStr - - db := op.GetDriverInfoMap()[t] - add := dropbox.Addition{} add.Code = c.Query("code") if len(add.Code) == 0 { - c.String(200, `

code cannot be empty

`) + c.String(200, `

Code cannot be empty

`) + notify["status"] = "fail" + notify["message"] = "Code cannot be empty" + service.MyService.Notify().SendNotify("recover_status", notify) return } add.RootFolderID = "" - for _, v := range db { - if v.Name == "app_key" { - add.AppKey = v.Default - } - if v.Name == "app_secret" { - add.AppSecret = v.Default - } - } + add.AppKey = dropbox.APPKEY + add.AppSecret = dropbox.APPSECRET var dropbox dropbox.Dropbox dropbox.Addition = add err := dropbox.Init(c) if err != nil { - c.String(200, `

Initialization failure:`+err.Error()+`

`) + c.String(200, `

Initialization failure:`+err.Error()+`

`) + notify["status"] = "fail" + notify["message"] = "Initialization failure" + service.MyService.Notify().SendNotify("recover_status", notify) return } username, err := dropbox.GetUserInfo(c) if err != nil { - c.String(200, `

Failed to get user information:`+err.Error()+`

`) + c.String(200, `

Failed to get user information:`+err.Error()+`

`) + notify["status"] = "fail" + notify["message"] = "Failed to get user information" + service.MyService.Notify().SendNotify("recover_status", notify) return } if len(username) > 0 { @@ -131,14 +114,17 @@ func GetRecoverStorage(c *gin.Context) { username += "_dropbox" dataMap, _ := service.MyService.Storage().GetConfigByName(username) if len(dataMap) > 0 { - c.String(200, `

The same configuration has been added

`) + c.String(200, `

The same configuration has been added

`) service.MyService.Storage().CheckAndMountByName(username) + notify["status"] = "warn" + notify["message"] = "The same configuration has been added" + service.MyService.Notify().SendNotify("recover_status", notify) return } dmap := make(map[string]string) dmap["client_id"] = add.AppKey dmap["client_secret"] = add.AppSecret - dmap["token"] = `{"access_token":"` + dropbox.AccessToken + `","token_type":"bearer","refresh_token":"` + dropbox.Addition.RefreshToken + `","expiry":"` + currentDate + `T` + currentTime.Add(time.Hour*3).Format("15:04:05") + `.780385354Z"}` + dmap["token"] = `{"access_token":"` + dropbox.AccessToken + `","token_type":"bearer","refresh_token":"` + dropbox.Addition.RefreshToken + `","expiry":"` + currentDate + `T` + currentTime.Add(time.Hour*3).Add(time.Minute*50).Format("15:04:05") + `.780385354Z"}` dmap["mount_point"] = "/mnt/" + username // data.SetValue(username, "type", "dropbox") // data.SetValue(username, "client_id", add.AppKey) @@ -154,8 +140,9 @@ func GetRecoverStorage(c *gin.Context) { // } service.MyService.Storage().CreateConfig(dmap, username, "dropbox") service.MyService.Storage().MountStorage("/mnt/"+username, username+":") - notify := make(map[string]interface{}) + notify["status"] = "success" + notify["message"] = "Success" service.MyService.Notify().SendNotify("recover_status", notify) } diff --git a/route/v1/storage.go b/route/v1/storage.go index 698a1f0..0bfea8a 100644 --- a/route/v1/storage.go +++ b/route/v1/storage.go @@ -9,6 +9,7 @@ import ( "github.com/IceWhaleTech/CasaOS/drivers/google_drive" "github.com/IceWhaleTech/CasaOS/model" "github.com/IceWhaleTech/CasaOS/pkg/utils/common_err" + "github.com/IceWhaleTech/CasaOS/pkg/utils/httper" "github.com/IceWhaleTech/CasaOS/service" "github.com/gin-gonic/gin" "go.uber.org/zap" @@ -52,8 +53,17 @@ func ListStorages(c *gin.Context) { r.MountPoints[i].Icon = dropbox.ICONURL } } + list := []httper.MountPoint{} - c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: r}) + for _, v := range r.MountPoints { + list = append(list, httper.MountPoint{ + Fs: v.Fs, + Icon: v.Icon, + MountPoint: v.MountPoint, + }) + } + + c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: list}) } func UpdateStorage(c *gin.Context) {