From 0869060479af60e1e43976e15275be9db0112d8f Mon Sep 17 00:00:00 2001 From: LinkLeong Date: Wed, 7 Jun 2023 08:20:42 +0100 Subject: [PATCH 1/3] Update entry json output --- go.mod | 2 +- main.go | 5 --- route/v1/system.go | 8 ++-- route/v2.go | 96 +--------------------------------------------- 4 files changed, 8 insertions(+), 103 deletions(-) diff --git a/go.mod b/go.mod index 0b32cb5ad..6b1a91a67 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,6 @@ require ( go.uber.org/goleak v1.2.1 go.uber.org/zap v1.24.0 golang.org/x/crypto v0.8.0 - golang.org/x/net v0.9.0 golang.org/x/oauth2 v0.6.0 golang.org/x/sync v0.1.0 golang.org/x/sys v0.7.0 @@ -124,6 +123,7 @@ require ( golang.org/x/arch v0.3.0 // indirect golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect golang.org/x/image v0.6.0 // indirect + golang.org/x/net v0.9.0 // indirect golang.org/x/text v0.9.0 // indirect golang.org/x/time v0.3.0 // indirect google.golang.org/appengine v1.6.7 // indirect diff --git a/main.go b/main.go index 2462a1034..82c8733e2 100644 --- a/main.go +++ b/main.go @@ -106,15 +106,11 @@ func main() { v2Router := route.InitV2Router() v2DocRouter := route.InitV2DocRouter(_docHTML, _docYAML) - v3file := route.InitFile() - v4dir := route.InitDir() mux := &util_http.HandlerMultiplexer{ HandlerMap: map[string]http.Handler{ "v1": v1Router, "v2": v2Router, "doc": v2DocRouter, - "v3": v3file, - "v4": v4dir, }, } @@ -147,7 +143,6 @@ func main() { "/v1/test", route.V2APIPath, route.V2DocPath, - route.V3FilePath, } for _, apiPath := range routers { err = service.MyService.Gateway().CreateRoute(&model.Route{ diff --git a/route/v1/system.go b/route/v1/system.go index b1de6ccf7..9c6c81e0e 100644 --- a/route/v1/system.go +++ b/route/v1/system.go @@ -2,6 +2,7 @@ package v1 import ( "bytes" + "encoding/json" "fmt" "io" "io/ioutil" @@ -375,9 +376,10 @@ func PortCheck(c *gin.Context) { func GetSystemEntry(c *gin.Context) { entry := service.MyService.System().GetSystemEntry() - if !gjson.Valid(entry) { - c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: ""}) + str := json.RawMessage(entry) + if !gjson.ValidBytes(str) { + c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: entry, Data: "[]"}) return } - c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: entry}) + c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: str}) } diff --git a/route/v2.go b/route/v2.go index d2985f60f..8d346c625 100644 --- a/route/v2.go +++ b/route/v2.go @@ -2,17 +2,13 @@ package route import ( "crypto/ecdsa" - "log" "net/http" "net/url" - "path" - "path/filepath" "strconv" "strings" "github.com/IceWhaleTech/CasaOS/codegen" "github.com/IceWhaleTech/CasaOS/pkg/config" - "github.com/IceWhaleTech/CasaOS/pkg/utils/file" "github.com/IceWhaleTech/CasaOS-Common/external" "github.com/IceWhaleTech/CasaOS-Common/utils/jwt" @@ -27,10 +23,8 @@ import ( var ( _swagger *openapi3.T - V2APIPath string - V2DocPath string - V3FilePath string - V4DirPath string + V2APIPath string + V2DocPath string ) func init() { @@ -48,8 +42,6 @@ func init() { V2APIPath = strings.TrimRight(u.Path, "/") V2DocPath = "/doc" + V2APIPath - V3FilePath = "/v3/file" - V4DirPath = "/v4/dir" } func InitV2Router() http.Handler { @@ -139,87 +131,3 @@ func InitV2DocRouter(docHTML string, docYAML string) http.Handler { } }) } - -func InitFile() http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - filePath := r.URL.Query().Get("path") - fileName := path.Base(filePath) - w.Header().Add("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(fileName)) - http.ServeFile(w, r, filePath) - }) -} - -func InitDir() http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - t := r.URL.Query().Get("format") - files := r.URL.Query().Get("files") - - if len(files) == 0 { - // w.JSON(common_err.CLIENT_ERROR, model.Result{ - // Success: common_err.INVALID_PARAMS, - // Message: common_err.GetMsg(common_err.INVALID_PARAMS), - // }) - return - } - list := strings.Split(files, ",") - for _, v := range list { - if !file.Exists(v) { - // c.JSON(common_err.SERVICE_ERROR, model.Result{ - // Success: common_err.FILE_DOES_NOT_EXIST, - // Message: common_err.GetMsg(common_err.FILE_DOES_NOT_EXIST), - // }) - return - } - } - w.Header().Add("Content-Type", "application/octet-stream") - w.Header().Add("Content-Transfer-Encoding", "binary") - w.Header().Add("Cache-Control", "no-cache") - // handles only single files not folders and multiple files - // if len(list) == 1 { - - // filePath := list[0] - // info, err := os.Stat(filePath) - // if err != nil { - - // w.JSON(http.StatusOK, model.Result{ - // Success: common_err.FILE_DOES_NOT_EXIST, - // Message: common_err.GetMsg(common_err.FILE_DOES_NOT_EXIST), - // }) - //return - // } - //} - - extension, ar, err := file.GetCompressionAlgorithm(t) - if err != nil { - // w.JSON(common_err.CLIENT_ERROR, model.Result{ - // Success: common_err.INVALID_PARAMS, - // Message: common_err.GetMsg(common_err.INVALID_PARAMS), - // }) - return - } - - err = ar.Create(w) - if err != nil { - // c.JSON(common_err.SERVICE_ERROR, model.Result{ - // Success: common_err.SERVICE_ERROR, - // Message: common_err.GetMsg(common_err.SERVICE_ERROR), - // Data: err.Error(), - // }) - return - } - defer ar.Close() - commonDir := file.CommonPrefix(filepath.Separator, list...) - - currentPath := filepath.Base(commonDir) - - name := "_" + currentPath - name += extension - w.Header().Add("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(name)) - for _, fname := range list { - err = file.AddFile(ar, fname, commonDir) - if err != nil { - log.Printf("Failed to archive %s: %v", fname, err) - } - } - }) -} From 4a46d1bdf2b3122085a86ac98d2636a33270b81a Mon Sep 17 00:00:00 2001 From: LinkLeong Date: Wed, 7 Jun 2023 08:22:22 +0100 Subject: [PATCH 2/3] Update json output --- route/v1/system.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/route/v1/system.go b/route/v1/system.go index 9c6c81e0e..fa43062a2 100644 --- a/route/v1/system.go +++ b/route/v1/system.go @@ -378,7 +378,7 @@ func GetSystemEntry(c *gin.Context) { entry := service.MyService.System().GetSystemEntry() str := json.RawMessage(entry) if !gjson.ValidBytes(str) { - c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: entry, Data: "[]"}) + c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: entry, Data: json.RawMessage("[]")}) return } c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: str}) From e8ad00217549883d2ded3a0703d4cd1eca7c7a00 Mon Sep 17 00:00:00 2001 From: LinkLeong Date: Wed, 14 Jun 2023 04:31:13 +0100 Subject: [PATCH 3/3] Update baseinfo --- model/sys_common.go | 7 ++++--- route/init.go | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/model/sys_common.go b/model/sys_common.go index 859c2b41e..90ff8bae9 100644 --- a/model/sys_common.go +++ b/model/sys_common.go @@ -70,7 +70,8 @@ type FileSetting struct { DownloadDir string `json:"download_dir"` } type BaseInfo struct { - Hash string `json:"i"` - Version string `json:"v"` - Channel string `json:"c,omitempty"` + Hash string `json:"i"` + Version string `json:"v"` + Channel string `json:"c,omitempty"` + DriveModel string `json:"m,omitempty"` } diff --git a/route/init.go b/route/init.go index 697d9ad14..70d262854 100644 --- a/route/init.go +++ b/route/init.go @@ -17,6 +17,7 @@ import ( "strings" "time" + file1 "github.com/IceWhaleTech/CasaOS-Common/utils/file" "github.com/IceWhaleTech/CasaOS-Common/utils/logger" "github.com/IceWhaleTech/CasaOS/common" "github.com/IceWhaleTech/CasaOS/model" @@ -53,6 +54,12 @@ func InitInfo() { } mb.Hash = encryption.GetMD5ByStr(mac) mb.Version = common.VERSION + osRelease, _ := file1.ReadOSRelease() + + mb.DriveModel = osRelease["MODEL"] + if len(mb.DriveModel) == 0 { + mb.DriveModel = "Casa" + } os.Remove(config.AppInfo.DBPath + "/baseinfo.conf") by, err := json.Marshal(mb) if err != nil {