feat: 合并服务端程序代码至主分支
This commit is contained in:
1 parent
47590ec0db
commit
d6816925ed
19 files changed
+672
-4
No files matched your search
@@ -0,0 +1,3 @@
|
||||
package api
|
||||
|
||||
var staticPath = "./"
|
||||
@@ -0,0 +1,57 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"miao-directory/fsOperateEventCenter"
|
||||
)
|
||||
|
||||
func CopyHandler(fsOperateEventCenter *fsOperateEventCenter.FsOperateEventCenter) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var req struct {
|
||||
NewPath string `json:"newPath"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
decodedPath, err := filepath.Abs(filepath.Join(staticPath, c.Param("path")))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to access path"})
|
||||
return
|
||||
}
|
||||
|
||||
newPath := filepath.Join(staticPath, req.NewPath, filepath.Base(decodedPath))
|
||||
eventId := fsOperateEventCenter.Push(func() error {
|
||||
return copyFile(decodedPath, newPath)
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"eventId": eventId})
|
||||
}
|
||||
}
|
||||
|
||||
func copyFile(src, dst string) error {
|
||||
sourceFile, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sourceFile.Close()
|
||||
|
||||
destFile, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer destFile.Close()
|
||||
|
||||
_, err = io.Copy(destFile, sourceFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return destFile.Sync()
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"miao-directory/fsOperateEventCenter"
|
||||
)
|
||||
|
||||
func CutHandler(fsOperateEventCenter *fsOperateEventCenter.FsOperateEventCenter) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var req struct {
|
||||
NewPath string `json:"newPath"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
decodedPath, err := filepath.Abs(filepath.Join(staticPath, c.Param("path")))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to access path"})
|
||||
return
|
||||
}
|
||||
|
||||
newPath := filepath.Join(staticPath, req.NewPath, filepath.Base(decodedPath))
|
||||
eventId := fsOperateEventCenter.Push(func() error {
|
||||
return os.Rename(decodedPath, newPath)
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"eventId": eventId})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"miao-directory/fsOperateEventCenter"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func DeleteHandler(fsOperateEventCenter *fsOperateEventCenter.FsOperateEventCenter) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
decodedPath, err := filepath.Abs(filepath.Join(staticPath, c.Param("path")))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to access path"})
|
||||
return
|
||||
}
|
||||
|
||||
eventId := fsOperateEventCenter.Push(func() error {
|
||||
return os.RemoveAll(decodedPath)
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"eventId": eventId})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetHandler(c *gin.Context) {
|
||||
decodedPath, err := filepath.Abs(filepath.Join(staticPath, c.Param("path")))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to access path"})
|
||||
return
|
||||
}
|
||||
|
||||
fileInfo, err := os.Stat(decodedPath)
|
||||
if os.IsNotExist(err) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Path not found"})
|
||||
return
|
||||
}
|
||||
|
||||
if fileInfo.IsDir() {
|
||||
files, err := ioutil.ReadDir(decodedPath)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to scan directory"})
|
||||
return
|
||||
}
|
||||
|
||||
var fileDetails []gin.H
|
||||
for _, file := range files {
|
||||
fileDetails = append(fileDetails, gin.H{
|
||||
"name": file.Name(),
|
||||
"size": file.Size(),
|
||||
"type": func() string {
|
||||
if file.IsDir() {
|
||||
return "directory"
|
||||
}
|
||||
return "file"
|
||||
}(),
|
||||
"stats": gin.H{
|
||||
"atimeMs": file.ModTime().UnixNano() / 1e6,
|
||||
"birthtimeMs": file.ModTime().UnixNano() / 1e6,
|
||||
"ctimeMs": file.ModTime().UnixNano() / 1e6,
|
||||
"mtimeMs": file.ModTime().UnixNano() / 1e6,
|
||||
},
|
||||
})
|
||||
}
|
||||
if len(fileDetails) == 0 {
|
||||
c.JSON(http.StatusOK, []gin.H{})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, fileDetails)
|
||||
}
|
||||
} else {
|
||||
if strings.HasSuffix(decodedPath, "index.html") {
|
||||
f, err := os.Open(decodedPath)
|
||||
if err != nil {
|
||||
c.Writer.WriteHeader(http.StatusInternalServerError)
|
||||
c.Writer.Write([]byte("Internal Server Error"))
|
||||
return
|
||||
}
|
||||
http.ServeContent(c.Writer, c.Request, "index.html", time.Now(), f)
|
||||
} else {
|
||||
// if c.Query("import") != "" {
|
||||
// c.Writer.Header().Set("Content-Type", "application/javascript")
|
||||
// }
|
||||
ext := filepath.Ext(decodedPath)
|
||||
switch ext {
|
||||
case ".js":
|
||||
case ".cjs":
|
||||
c.Writer.Header().Set("Content-Type", "application/javascript")
|
||||
case ".css":
|
||||
c.Writer.Header().Set("Content-Type", "text/css")
|
||||
case ".html":
|
||||
c.Writer.Header().Set("Content-Type", "text/html")
|
||||
case ".json":
|
||||
c.Writer.Header().Set("Content-Type", "application/json")
|
||||
default:
|
||||
c.Writer.Header().Set("Content-Type", "application/octet-stream")
|
||||
}
|
||||
c.File(decodedPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func MkdirHandler(c *gin.Context) {
|
||||
var request struct {
|
||||
FolderName string `json:"folderName"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": "failed", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
decodedPath, err := filepath.Abs(filepath.Join(staticPath, c.Param("path")))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "failed", "error": "Unable to access path"})
|
||||
return
|
||||
}
|
||||
|
||||
mkdirPath := filepath.Join(decodedPath, request.FolderName)
|
||||
|
||||
if err := os.MkdirAll(mkdirPath, os.ModePerm); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "failed", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "success"})
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"miao-directory/fsOperateEventCenter"
|
||||
)
|
||||
|
||||
func QueryHandler(fsOperateEventCenter *fsOperateEventCenter.FsOperateEventCenter) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
idStr := c.Query("id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 32)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid id"})
|
||||
return
|
||||
}
|
||||
|
||||
event := fsOperateEventCenter.Query(int32(id))
|
||||
c.JSON(http.StatusOK, event)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"miao-directory/fsOperateEventCenter"
|
||||
)
|
||||
|
||||
func RenameHandler(fsOperateEventCenter *fsOperateEventCenter.FsOperateEventCenter) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var req struct {
|
||||
NewName string `json:"newName"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
decodedPath, err := filepath.Abs(filepath.Join(staticPath, c.Param("path")))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to access path"})
|
||||
return
|
||||
}
|
||||
|
||||
newTargetPath := filepath.Join(filepath.Dir(decodedPath), req.NewName)
|
||||
eventId := fsOperateEventCenter.Push(func() error {
|
||||
return os.Rename(decodedPath, newTargetPath)
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"eventId": eventId})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
func UploadHandler(c *gin.Context) {
|
||||
fileName := c.PostForm("fileName")
|
||||
operateType := c.PostForm("operateType")
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": "failed", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
decodedPath, err := filepath.Abs(filepath.Join(staticPath, c.Param("path")))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "failed", "error": "Unable to access path"})
|
||||
return
|
||||
}
|
||||
|
||||
savePath := filepath.Join(decodedPath, fileName)
|
||||
fileData, err := file.Open()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "failed", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
defer fileData.Close()
|
||||
|
||||
data, err := ioutil.ReadAll(fileData)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "failed", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if operateType == "write" {
|
||||
err = ioutil.WriteFile(savePath, data, 0644)
|
||||
} else {
|
||||
f, err := os.OpenFile(savePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "failed", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
_, err = f.Write(data)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "failed", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "success"})
|
||||
}
|
||||
Reference in new issue
Block a user