parent
1f1d2d992d
commit
c09e6f186b
@ -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})
|
||||
}
|
@ -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})
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
@ -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})
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
Before Width: | Height: | Size: 448 KiB After Width: | Height: | Size: 448 KiB |
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
@ -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")
|
||||
}
|
Loading…
Reference in new issue