Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Up json #1186

Merged
merged 3 commits into from
Jun 14, 2023
Merged

Up json #1186

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down Expand Up @@ -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{
Expand Down
7 changes: 4 additions & 3 deletions model/sys_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
7 changes: 7 additions & 0 deletions route/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions route/v1/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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: json.RawMessage("[]")})
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})
}
96 changes: 2 additions & 94 deletions route/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,10 +23,8 @@ import (
var (
_swagger *openapi3.T

V2APIPath string
V2DocPath string
V3FilePath string
V4DirPath string
V2APIPath string
V2DocPath string
)

func init() {
Expand All @@ -48,8 +42,6 @@ func init() {

V2APIPath = strings.TrimRight(u.Path, "/")
V2DocPath = "/doc" + V2APIPath
V3FilePath = "/v3/file"
V4DirPath = "/v4/dir"
}

func InitV2Router() http.Handler {
Expand Down Expand Up @@ -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)
}
}
})
}