Update entry json output (#1172)

This commit is contained in:
link 2023-06-07 15:21:22 +08:00 committed by GitHub
parent 78e7a8b411
commit f867453573
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 103 deletions

2
go.mod
View file

@ -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

View file

@ -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{

View file

@ -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})
}

View file

@ -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)
}
}
})
}