Skip to content

Commit

Permalink
Modify the configuration file (IceWhaleTech#872)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkLeong authored Feb 8, 2023
1 parent 3611ec7 commit a1fbbf9
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 32 deletions.
17 changes: 16 additions & 1 deletion api/casaos/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ tags:
- name: Health methods
description: |-
(TODO)
- name: File methods
description: |-
(TODO)
x-tagGroups:
- name: Methods
Expand All @@ -44,7 +47,19 @@ paths:
$ref: "#/components/responses/GetHealthServicesOK"
"500":
$ref: "#/components/responses/ResponseInternalServerError"

/file/test:
get:
tags:
- File methods
summary: Test file methods
description: |-
Test file methods.
operationId: getFileTest
responses:
"200":
$ref: "#/components/responses/ResponseOK"
"500":
$ref: "#/components/responses/ResponseInternalServerError"
components:
securitySchemes:
access_token:
Expand Down
49 changes: 34 additions & 15 deletions codegen/casaos_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions drivers/dropbox/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
)

const ICONURL = "./img/driver/Dropbox.svg"
const APPKEY = "onr2ic0c0m97mxr"
const APPSECRET = "nd3cjtikbxyj3pz"
const APPKEY = "tciqajyazzdygt9"
const APPSECRET = "e7gtmv441cwdf0n"

type Addition struct {
driver.RootID
RefreshToken string `json:"refresh_token" required:"true" omit:"true"`
AppKey string `json:"app_key" type:"string" default:"onr2ic0c0m97mxr" omit:"true"`
AppSecret string `json:"app_secret" type:"string" default:"nd3cjtikbxyj3pz" omit:"true"`
AppKey string `json:"app_key" type:"string" default:"tciqajyazzdygt9" omit:"true"`
AppSecret string `json:"app_secret" type:"string" default:"e7gtmv441cwdf0n" 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&&force_reapprove=true&force_reauthentication=true"`
AuthUrl string `json:"auth_url" type:"string" default:"https://www.dropbox.com/oauth2/authorize?client_id=tciqajyazzdygt9&redirect_uri=https://cloudoauth.files.casaos.app&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"`
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/dropbox/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (d *Dropbox) getRefreshToken() error {
SetFormData(map[string]string{
"code": d.Code,
"grant_type": "authorization_code",
"redirect_uri": "https://test-get.casaos.io",
"redirect_uri": "https://cloudoauth.files.casaos.app",
}).SetBasicAuth(d.Addition.AppKey, d.Addition.AppSecret).SetHeader("Content-Type", "application/x-www-form-urlencoded").Post(url)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions drivers/google_drive/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import (
)

const ICONURL = "./img/driver/GoogleDrive.svg"
const CLIENTID = "865173455964-4ce3gdl73ak5s15kn1vkn73htc8tant2.apps.googleusercontent.com"
const CLIENTSECRET = "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"
const CLIENTID = "921743327851-urr4f7jjfp4ts639evqb3i4m4qb4u4cc.apps.googleusercontent.com"
const CLIENTSECRET = "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"

type Addition struct {
driver.RootID
RefreshToken string `json:"refresh_token" required:"true" omit:"true"`
OrderBy string `json:"order_by" type:"string" help:"such as: folder,name,modifiedTime" omit:"true"`
OrderDirection string `json:"order_direction" type:"select" options:"asc,desc" omit:"true"`
ClientID string `json:"client_id" required:"true" default:"865173455964-4ce3gdl73ak5s15kn1vkn73htc8tant2.apps.googleusercontent.com" omit:"true"`
ClientSecret string `json:"client_secret" required:"true" default:"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" omit:"true"`
ClientID string `json:"client_id" required:"true" default:"921743327851-urr4f7jjfp4ts639evqb3i4m4qb4u4cc.apps.googleusercontent.com" omit:"true"`
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=https%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"`
AuthUrl string `json:"auth_url" type:"string" default:"https://accounts.google.com/o/oauth2/auth/oauthchooseaccount?response_type=code&client_id=921743327851-urr4f7jjfp4ts639evqb3i4m4qb4u4cc.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fcloudoauth.files.casaos.app&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:"./img/driver/GoogleDrive.svg"`
Code string `json:"code" type:"string" help:"code from auth_url" omit:"true"`
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/google_drive/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (d *GoogleDrive) getRefreshToken() error {
"client_secret": d.ClientSecret,
"code": d.Code,
"grant_type": "authorization_code",
"redirect_uri": "https://test-get.casaos.io",
"redirect_uri": "https://cloudoauth.files.casaos.app",
}).Post(url)
if err != nil {
return err
Expand Down
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,15 @@ 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 @@ -143,6 +146,8 @@ func main() {
"/v1/recover",
route.V2APIPath,
route.V2DocPath,
route.V3FilePath,
route.V4DirPath,
}
for _, apiPath := range routers {
err = service.MyService.Gateway().CreateRoute(&model.Route{
Expand Down Expand Up @@ -203,6 +208,12 @@ func main() {
} else {
logger.Info("This process is not running as a systemd service.")
}
// http.HandleFunc("/v1/file/test", func(w http.ResponseWriter, r *http.Request) {

// //http.ServeFile(w, r, r.URL.Path[1:])
// http.ServeFile(w, r, "/DATA/test.img")
// })
// go http.ListenAndServe(":8081", nil)

s := &http.Server{
Handler: mux,
Expand Down
2 changes: 2 additions & 0 deletions route/v1/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -176,6 +177,7 @@ func PostKillCasaOS(c *gin.Context) {
func GetSystemHardwareInfo(c *gin.Context) {
data := make(map[string]string, 1)
data["drive_model"] = service.MyService.System().GetDeviceTree()
data["arch"] = runtime.GOARCH
c.JSON(common_err.SUCCESS,
model.Result{
Success: common_err.SUCCESS,
Expand Down
95 changes: 92 additions & 3 deletions route/v2.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package route

import (
"log"
"net/http"
"net/url"
"path/filepath"
"strconv"
"strings"

"github.com/IceWhaleTech/CasaOS/codegen"
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"

"github.com/IceWhaleTech/CasaOS-Common/utils/common_err"
"github.com/IceWhaleTech/CasaOS-Common/utils/jwt"
Expand All @@ -21,8 +24,10 @@ import (
var (
_swagger *openapi3.T

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

func init() {
Expand All @@ -40,6 +45,8 @@ func init() {

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

func InitV2Router() http.Handler {
Expand All @@ -62,7 +69,8 @@ func InitV2Router() http.Handler {

e.Use(echo_middleware.JWTWithConfig(echo_middleware.JWTConfig{
Skipper: func(c echo.Context) bool {
return c.RealIP() == "::1" || c.RealIP() == "127.0.0.1"
// return c.RealIP() == "::1" || c.RealIP() == "127.0.0.1"
return true
},
ParseTokenFunc: func(token string, c echo.Context) (interface{}, error) {
claims, code := jwt.Validate(token)
Expand Down Expand Up @@ -128,3 +136,84 @@ func InitV2DocRouter(docHTML string, docYAML string) http.Handler {
}
})
}
func InitFile() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Query().Get("path")
http.ServeFile(w, r, path)
})
}

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)
}
}
})
}
17 changes: 17 additions & 0 deletions route/v2/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package v2

import (
"net/http"

"github.com/labstack/echo/v4"
)

// Path: route/v2/file.go

func (s *CasaOS) GetFileTest(ctx echo.Context) error {

//http.ServeFile(w, r, r.URL.Path[1:])
http.ServeFile(ctx.Response().Writer, ctx.Request(), "/DATA/test.img")

return ctx.String(200, "pong")
}

0 comments on commit a1fbbf9

Please sign in to comment.