From c09e6f186b076a51382f2f9bb7e45972a0af6914 Mon Sep 17 00:00:00 2001 From: ZGGSONG Date: Sun, 10 Apr 2022 16:03:58 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81-=E6=8B=86?= =?UTF-8?q?=E5=88=86=E8=87=B3=E5=90=84=E4=B8=AA=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 + main.go | 69 +----------------- server/controller/address_controller.go | 22 ++++++ server/controller/files_controller.go | 39 ++++++++++ server/controller/qrcodes_controller.go | 21 ++++++ server/controller/texts_controller.go | 43 +++++++++++ server/controller/uploads_controller.go | 33 +++++++++ {frontend => server/frontend}/.eslintrc.js | 0 {frontend => server/frontend}/.gitignore | 0 {frontend => server/frontend}/index.html | 0 .../frontend}/package-lock.json | 0 {frontend => server/frontend}/package.json | 0 .../frontend}/src/components/center.jsx | 0 .../frontend}/src/components/dialog.jsx | 0 .../frontend}/src/components/loading.jsx | 0 .../frontend}/src/components/qrcode.jsx | 0 .../frontend}/src/components/space.jsx | 0 .../frontend}/src/hooks/use_query.js | 0 .../frontend}/src/images/synk.ico | Bin .../frontend}/src/images/synk.png | Bin .../frontend}/src/images/synk.svg | 0 .../frontend}/src/initializers/client_id.js | 0 .../frontend}/src/initializers/index.js | 0 {frontend => server/frontend}/src/main.jsx | 0 .../frontend}/src/pages/downloads.jsx | 0 .../frontend}/src/pages/home.jsx | 0 .../frontend}/src/pages/home/components.jsx | 0 .../frontend}/src/pages/home/nav.jsx | 0 .../src/pages/home/upload_file_form.jsx | 0 .../src/pages/home/upload_screenshot_form.jsx | 0 .../src/pages/home/upload_text_form.jsx | 0 .../frontend}/src/shared/app_context.js | 0 .../frontend}/src/shared/global_style.jsx | 0 .../frontend}/src/shared/history.js | 0 .../frontend}/src/shared/http.js | 0 .../frontend}/src/shared/prefetch.js | 0 .../frontend}/src/shared/ws_client.js | 0 .../frontend}/src/vite-env.d.ts | 0 {frontend => server/frontend}/tsconfig.json | 0 {frontend => server/frontend}/vite.config.ts | 0 {frontend => server/frontend}/yarn.lock | 0 server/server.go | 45 ++++++++++++ 43 files changed, 208 insertions(+), 67 deletions(-) create mode 100644 server/controller/address_controller.go create mode 100644 server/controller/files_controller.go create mode 100644 server/controller/qrcodes_controller.go create mode 100644 server/controller/texts_controller.go create mode 100644 server/controller/uploads_controller.go rename {frontend => server/frontend}/.eslintrc.js (100%) rename {frontend => server/frontend}/.gitignore (100%) rename {frontend => server/frontend}/index.html (100%) rename {frontend => server/frontend}/package-lock.json (100%) rename {frontend => server/frontend}/package.json (100%) rename {frontend => server/frontend}/src/components/center.jsx (100%) rename {frontend => server/frontend}/src/components/dialog.jsx (100%) rename {frontend => server/frontend}/src/components/loading.jsx (100%) rename {frontend => server/frontend}/src/components/qrcode.jsx (100%) rename {frontend => server/frontend}/src/components/space.jsx (100%) rename {frontend => server/frontend}/src/hooks/use_query.js (100%) rename {frontend => server/frontend}/src/images/synk.ico (100%) rename {frontend => server/frontend}/src/images/synk.png (100%) rename {frontend => server/frontend}/src/images/synk.svg (100%) rename {frontend => server/frontend}/src/initializers/client_id.js (100%) rename {frontend => server/frontend}/src/initializers/index.js (100%) rename {frontend => server/frontend}/src/main.jsx (100%) rename {frontend => server/frontend}/src/pages/downloads.jsx (100%) rename {frontend => server/frontend}/src/pages/home.jsx (100%) rename {frontend => server/frontend}/src/pages/home/components.jsx (100%) rename {frontend => server/frontend}/src/pages/home/nav.jsx (100%) rename {frontend => server/frontend}/src/pages/home/upload_file_form.jsx (100%) rename {frontend => server/frontend}/src/pages/home/upload_screenshot_form.jsx (100%) rename {frontend => server/frontend}/src/pages/home/upload_text_form.jsx (100%) rename {frontend => server/frontend}/src/shared/app_context.js (100%) rename {frontend => server/frontend}/src/shared/global_style.jsx (100%) rename {frontend => server/frontend}/src/shared/history.js (100%) rename {frontend => server/frontend}/src/shared/http.js (100%) rename {frontend => server/frontend}/src/shared/prefetch.js (100%) rename {frontend => server/frontend}/src/shared/ws_client.js (100%) rename {frontend => server/frontend}/src/vite-env.d.ts (100%) rename {frontend => server/frontend}/tsconfig.json (100%) rename {frontend => server/frontend}/vite.config.ts (100%) rename {frontend => server/frontend}/yarn.lock (100%) create mode 100644 server/server.go diff --git a/go.mod b/go.mod index eae3714..332a599 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require ( github.com/gin-gonic/gin v1.7.7 github.com/google/uuid v1.3.0 + github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e ) require ( diff --git a/go.sum b/go.sum index 0f8d844..b91cec3 100644 --- a/go.sum +++ b/go.sum @@ -30,6 +30,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLD github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= diff --git a/main.go b/main.go index 2413661..0e920dd 100644 --- a/main.go +++ b/main.go @@ -1,50 +1,16 @@ package main import ( - "embed" - "io/fs" - "io/ioutil" - "log" - "net/http" "os" "os/exec" "os/signal" - "path" - "path/filepath" - "strings" - "github.com/gin-gonic/gin" - "github.com/google/uuid" + "github.com/zggsong/FileSync/server" ) -//go:embed frontend/dist/* -var FS embed.FS - func main() { go func() { - gin.SetMode(gin.ReleaseMode) - router := gin.Default() - router.POST("/api/v1/texts", TextsController) - staticFiles, _ := fs.Sub(FS, "frontend/dist") - router.StaticFS("/static", http.FS(staticFiles)) - router.NoRoute(func(c *gin.Context) { - path := c.Request.URL.Path - if strings.HasPrefix(path, "/static/") { - reader, err := staticFiles.Open("index.html") - if err != nil { - log.Fatal(err) - } - defer reader.Close() - stat, err := reader.Stat() - if err != nil { - log.Fatal(err) - } - c.DataFromReader(http.StatusOK, stat.Size(), "text/html", reader, nil) - } else { - c.Status(http.StatusNotFound) - } - }) - router.Run(":27149") + server.Run() }() chromePath := "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" @@ -59,34 +25,3 @@ func main() { cmd.Process.Kill() } } - -func TextsController(c *gin.Context) { - var json struct { - Raw string `json:"raw"` - } - if err := c.ShouldBindJSON(&json); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - } else { - exe, err := os.Executable() - if err != nil { - log.Fatal(err) - } - dir := filepath.Dir(exe) - if err != nil { - log.Fatal(err) - } - filename := uuid.New().String() - uploads := filepath.Join(dir, "uploads") - err = os.MkdirAll(uploads, os.ModePerm) - if err != nil { - log.Fatal(err) - } - fullpath := path.Join("uploads", filename+".txt") - err = ioutil.WriteFile(filepath.Join(dir, fullpath), []byte(json.Raw), 0644) - if err != nil { - log.Fatal(err) - } - c.JSON(http.StatusOK, gin.H{"url": "/" + fullpath}) - } - -} diff --git a/server/controller/address_controller.go b/server/controller/address_controller.go new file mode 100644 index 0000000..1a055bc --- /dev/null +++ b/server/controller/address_controller.go @@ -0,0 +1,22 @@ +package controller + +import ( + "net" + "net/http" + + "github.com/gin-gonic/gin" +) + +func AddressesController(c *gin.Context) { + addrs, _ := net.InterfaceAddrs() + var result []string + for _, address := range addrs { + // check the address type and if it is not a loopback the display it + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + result = append(result, ipnet.IP.String()) + } + } + } + c.JSON(http.StatusOK, gin.H{"addresses": result}) +} diff --git a/server/controller/files_controller.go b/server/controller/files_controller.go new file mode 100644 index 0000000..11e9f46 --- /dev/null +++ b/server/controller/files_controller.go @@ -0,0 +1,39 @@ +package controller + +import ( + "log" + "net/http" + "os" + "path" + "path/filepath" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +func FilesController(c *gin.Context) { + file, err := c.FormFile("raw") + if err != nil { + log.Fatal(err) + } + exe, err := os.Executable() + if err != nil { + log.Fatal(err) + } + dir := filepath.Dir(exe) + if err != nil { + log.Fatal(err) + } + filename := uuid.New().String() + uploads := filepath.Join(dir, "uploads") + err = os.MkdirAll(uploads, os.ModePerm) + if err != nil { + log.Fatal(err) + } + fullpath := path.Join("uploads", filename+filepath.Ext(file.Filename)) + fileErr := c.SaveUploadedFile(file, filepath.Join(dir, fullpath)) + if fileErr != nil { + log.Fatal(fileErr) + } + c.JSON(http.StatusOK, gin.H{"url": "/" + fullpath}) +} diff --git a/server/controller/qrcodes_controller.go b/server/controller/qrcodes_controller.go new file mode 100644 index 0000000..9a1f4c1 --- /dev/null +++ b/server/controller/qrcodes_controller.go @@ -0,0 +1,21 @@ +package controller + +import ( + "log" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/skip2/go-qrcode" +) + +func QrcodesController(c *gin.Context) { + if content := c.Query("content"); content != "" { + png, err := qrcode.Encode(content, qrcode.Medium, 256) + if err != nil { + log.Fatal(err) + } + c.Data(http.StatusOK, "image/png", png) + } else { + c.Status(http.StatusPreconditionRequired) + } +} diff --git a/server/controller/texts_controller.go b/server/controller/texts_controller.go new file mode 100644 index 0000000..4255c08 --- /dev/null +++ b/server/controller/texts_controller.go @@ -0,0 +1,43 @@ +package controller + +import ( + "io/ioutil" + "log" + "net/http" + "os" + "path" + "path/filepath" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +func TextsController(c *gin.Context) { + var json struct { + Raw string `json:"raw"` + } + if err := c.ShouldBindJSON(&json); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + } else { + exe, err := os.Executable() + if err != nil { + log.Fatal(err) + } + dir := filepath.Dir(exe) + if err != nil { + log.Fatal(err) + } + filename := uuid.New().String() + uploads := filepath.Join(dir, "uploads") + err = os.MkdirAll(uploads, os.ModePerm) + if err != nil { + log.Fatal(err) + } + fullpath := path.Join("uploads", filename+".txt") + err = ioutil.WriteFile(filepath.Join(dir, fullpath), []byte(json.Raw), 0644) + if err != nil { + log.Fatal(err) + } + c.JSON(http.StatusOK, gin.H{"url": "/" + fullpath}) + } +} diff --git a/server/controller/uploads_controller.go b/server/controller/uploads_controller.go new file mode 100644 index 0000000..89f1c41 --- /dev/null +++ b/server/controller/uploads_controller.go @@ -0,0 +1,33 @@ +package controller + +import ( + "log" + "net/http" + "os" + "path/filepath" + + "github.com/gin-gonic/gin" +) + +func UploadsController(c *gin.Context) { + if path := c.Param("path"); path != "" { + target := filepath.Join(getUploadsDir(), path) + c.Header("Content-Description", "File Transfer") + c.Header("Content-Transfer-Encoding", "binary") + c.Header("Content-Disposition", "attachment; filename="+path) + c.Header("Content-Type", "application/octet-stream") + c.File(target) + } else { + c.Status(http.StatusNotFound) + } +} + +func getUploadsDir() (uploads string) { + exe, err := os.Executable() + if err != nil { + log.Fatal(err) + } + dir := filepath.Dir(exe) + uploads = filepath.Join(dir, "uploads") + return +} diff --git a/frontend/.eslintrc.js b/server/frontend/.eslintrc.js similarity index 100% rename from frontend/.eslintrc.js rename to server/frontend/.eslintrc.js diff --git a/frontend/.gitignore b/server/frontend/.gitignore similarity index 100% rename from frontend/.gitignore rename to server/frontend/.gitignore diff --git a/frontend/index.html b/server/frontend/index.html similarity index 100% rename from frontend/index.html rename to server/frontend/index.html diff --git a/frontend/package-lock.json b/server/frontend/package-lock.json similarity index 100% rename from frontend/package-lock.json rename to server/frontend/package-lock.json diff --git a/frontend/package.json b/server/frontend/package.json similarity index 100% rename from frontend/package.json rename to server/frontend/package.json diff --git a/frontend/src/components/center.jsx b/server/frontend/src/components/center.jsx similarity index 100% rename from frontend/src/components/center.jsx rename to server/frontend/src/components/center.jsx diff --git a/frontend/src/components/dialog.jsx b/server/frontend/src/components/dialog.jsx similarity index 100% rename from frontend/src/components/dialog.jsx rename to server/frontend/src/components/dialog.jsx diff --git a/frontend/src/components/loading.jsx b/server/frontend/src/components/loading.jsx similarity index 100% rename from frontend/src/components/loading.jsx rename to server/frontend/src/components/loading.jsx diff --git a/frontend/src/components/qrcode.jsx b/server/frontend/src/components/qrcode.jsx similarity index 100% rename from frontend/src/components/qrcode.jsx rename to server/frontend/src/components/qrcode.jsx diff --git a/frontend/src/components/space.jsx b/server/frontend/src/components/space.jsx similarity index 100% rename from frontend/src/components/space.jsx rename to server/frontend/src/components/space.jsx diff --git a/frontend/src/hooks/use_query.js b/server/frontend/src/hooks/use_query.js similarity index 100% rename from frontend/src/hooks/use_query.js rename to server/frontend/src/hooks/use_query.js diff --git a/frontend/src/images/synk.ico b/server/frontend/src/images/synk.ico similarity index 100% rename from frontend/src/images/synk.ico rename to server/frontend/src/images/synk.ico diff --git a/frontend/src/images/synk.png b/server/frontend/src/images/synk.png similarity index 100% rename from frontend/src/images/synk.png rename to server/frontend/src/images/synk.png diff --git a/frontend/src/images/synk.svg b/server/frontend/src/images/synk.svg similarity index 100% rename from frontend/src/images/synk.svg rename to server/frontend/src/images/synk.svg diff --git a/frontend/src/initializers/client_id.js b/server/frontend/src/initializers/client_id.js similarity index 100% rename from frontend/src/initializers/client_id.js rename to server/frontend/src/initializers/client_id.js diff --git a/frontend/src/initializers/index.js b/server/frontend/src/initializers/index.js similarity index 100% rename from frontend/src/initializers/index.js rename to server/frontend/src/initializers/index.js diff --git a/frontend/src/main.jsx b/server/frontend/src/main.jsx similarity index 100% rename from frontend/src/main.jsx rename to server/frontend/src/main.jsx diff --git a/frontend/src/pages/downloads.jsx b/server/frontend/src/pages/downloads.jsx similarity index 100% rename from frontend/src/pages/downloads.jsx rename to server/frontend/src/pages/downloads.jsx diff --git a/frontend/src/pages/home.jsx b/server/frontend/src/pages/home.jsx similarity index 100% rename from frontend/src/pages/home.jsx rename to server/frontend/src/pages/home.jsx diff --git a/frontend/src/pages/home/components.jsx b/server/frontend/src/pages/home/components.jsx similarity index 100% rename from frontend/src/pages/home/components.jsx rename to server/frontend/src/pages/home/components.jsx diff --git a/frontend/src/pages/home/nav.jsx b/server/frontend/src/pages/home/nav.jsx similarity index 100% rename from frontend/src/pages/home/nav.jsx rename to server/frontend/src/pages/home/nav.jsx diff --git a/frontend/src/pages/home/upload_file_form.jsx b/server/frontend/src/pages/home/upload_file_form.jsx similarity index 100% rename from frontend/src/pages/home/upload_file_form.jsx rename to server/frontend/src/pages/home/upload_file_form.jsx diff --git a/frontend/src/pages/home/upload_screenshot_form.jsx b/server/frontend/src/pages/home/upload_screenshot_form.jsx similarity index 100% rename from frontend/src/pages/home/upload_screenshot_form.jsx rename to server/frontend/src/pages/home/upload_screenshot_form.jsx diff --git a/frontend/src/pages/home/upload_text_form.jsx b/server/frontend/src/pages/home/upload_text_form.jsx similarity index 100% rename from frontend/src/pages/home/upload_text_form.jsx rename to server/frontend/src/pages/home/upload_text_form.jsx diff --git a/frontend/src/shared/app_context.js b/server/frontend/src/shared/app_context.js similarity index 100% rename from frontend/src/shared/app_context.js rename to server/frontend/src/shared/app_context.js diff --git a/frontend/src/shared/global_style.jsx b/server/frontend/src/shared/global_style.jsx similarity index 100% rename from frontend/src/shared/global_style.jsx rename to server/frontend/src/shared/global_style.jsx diff --git a/frontend/src/shared/history.js b/server/frontend/src/shared/history.js similarity index 100% rename from frontend/src/shared/history.js rename to server/frontend/src/shared/history.js diff --git a/frontend/src/shared/http.js b/server/frontend/src/shared/http.js similarity index 100% rename from frontend/src/shared/http.js rename to server/frontend/src/shared/http.js diff --git a/frontend/src/shared/prefetch.js b/server/frontend/src/shared/prefetch.js similarity index 100% rename from frontend/src/shared/prefetch.js rename to server/frontend/src/shared/prefetch.js diff --git a/frontend/src/shared/ws_client.js b/server/frontend/src/shared/ws_client.js similarity index 100% rename from frontend/src/shared/ws_client.js rename to server/frontend/src/shared/ws_client.js diff --git a/frontend/src/vite-env.d.ts b/server/frontend/src/vite-env.d.ts similarity index 100% rename from frontend/src/vite-env.d.ts rename to server/frontend/src/vite-env.d.ts diff --git a/frontend/tsconfig.json b/server/frontend/tsconfig.json similarity index 100% rename from frontend/tsconfig.json rename to server/frontend/tsconfig.json diff --git a/frontend/vite.config.ts b/server/frontend/vite.config.ts similarity index 100% rename from frontend/vite.config.ts rename to server/frontend/vite.config.ts diff --git a/frontend/yarn.lock b/server/frontend/yarn.lock similarity index 100% rename from frontend/yarn.lock rename to server/frontend/yarn.lock diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..4de6e3c --- /dev/null +++ b/server/server.go @@ -0,0 +1,45 @@ +package server + +import ( + "embed" + "io/fs" + "log" + "net/http" + "strings" + + "github.com/gin-gonic/gin" + controller "github.com/zggsong/FileSync/server/controller" +) + +//go:embed frontend/dist/* +var FS embed.FS + +func Run() { + gin.SetMode(gin.ReleaseMode) + router := gin.Default() + router.POST("/api/v1/texts", controller.TextsController) + router.GET("/api/v1/addresses", controller.AddressesController) + router.GET("/api/v1/qrcodes", controller.QrcodesController) + router.GET("/uploads/:path", controller.UploadsController) + router.POST("/api/v1/files", controller.FilesController) + staticFiles, _ := fs.Sub(FS, "frontend/dist") + router.StaticFS("/static", http.FS(staticFiles)) + router.NoRoute(func(c *gin.Context) { + path := c.Request.URL.Path + if strings.HasPrefix(path, "/static/") { + reader, err := staticFiles.Open("index.html") + if err != nil { + log.Fatal(err) + } + defer reader.Close() + stat, err := reader.Stat() + if err != nil { + log.Fatal(err) + } + c.DataFromReader(http.StatusOK, stat.Size(), "text/html", reader, nil) + } else { + c.Status(http.StatusNotFound) + } + }) + router.Run(":27149") +}