diff --git a/controller/file.go b/controller/file.go index 6923dee..556880d 100644 --- a/controller/file.go +++ b/controller/file.go @@ -1,14 +1,20 @@ package controller import ( + "background/db" + "background/model" "background/utils" + "fmt" "image" "image/color" "image/jpeg" "io/ioutil" "log" + "math" "net/http" "os" + "strconv" + "strings" "github.com/disintegration/imaging" "github.com/gin-gonic/gin" @@ -210,6 +216,88 @@ func (this *FileController) DownloadFile(c *gin.Context) { c.Writer.Write(bytes) } +func myAtoi(str string) int { + num := 0 + s := "" + flag := false + str = strings.TrimSpace(str) + if len(str) == 0 { + return 0 + } + for i := 0; i < len(str); i++ { + if !(str[i] >= '0' && str[i] <= '9') && !(str[i] == '+' || str[i] == '-') { + break + } + if str[i] == '+' { + if flag || s != "" { + break + } + flag = true + continue + } + if str[i] == '-' { + if flag || s != "" { + break + } + flag = true + s += "-" + continue + } + s += string(str[i]) + } + num, _ = strconv.Atoi(s) + if num > math.MaxInt32 { + return math.MaxInt32 + } else if num < math.MinInt32 { + return math.MinInt32 + } + return num +} + +func (this *FileController) OnFileUploadFileMarkdown(c *gin.Context) { + parent := c.Query("parent") + filename := c.Query("filename") + file, _, err := c.Request.FormFile("file") + if nil != err || nil == file { + log.Print(err.Error()) + return + } + bytes, err := ioutil.ReadAll(file) + if nil != err { + log.Print(err.Error()) + return + } + log.Print(parent, filename, string(bytes)) + + query := fmt.Sprintf("select * from doc_copy1 where doc_copy1.id = '%d'", int32(myAtoi(parent))) + docs := []model.DocTree{} + e := db.GetMysqlClient().Query2(query, &docs) + if nil != e { + log.Print(e.Error()) + return + } + if len(docs) == 0 { + return + } + + fork := strings.ReplaceAll(string(bytes), "\\\\", "\\\\\\\\") + fork = strings.ReplaceAll(string(fork), "\\", "\\\\") + + e = model.CreateDocTree( + model.DocTree{ + Title: filename, + Content: fork, + Author: "admin", + IsPublic: 1, + Father: int32(myAtoi(parent)), + Level: docs[0].Level + 1, + }, + ) + if nil != e { + log.Printf(e.Error()) + } + c.JSON(200, map[string]interface{}{"msg": "ok"}) +} func (this *FileController) OnFileUploadFile(c *gin.Context) { path := c.Query("path") diff --git a/main.go b/main.go index be69166..17998a5 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,6 @@ func CORSMiddleware(c *gin.Context) { } else { c.Writer.Header().Set("Access-Control-Allow-Origin", "https://testingcloud.club") - c.Writer.Header().Set("Access-Control-Allow-Origin", "http://127.0.0.1:8080") } c.Writer.Header().Set("Access-Control-Max-Age", "86400") c.Writer.Header().Set("Access-Control-Allow-Headers", @@ -194,7 +193,7 @@ func main() { api.GET("/hardware", controller.ReadHardWare) // 读取硬件 api.DELETE("/hardware", controller.DeleteHardWare) // 读取硬件 - api.POST("/file", fileController.OnFileUploadFile) // 上传文件 + api.POST("/upload_doc", fileController.OnFileUploadFileMarkdown) // 上传文件 api.PUT("/file", fileController.OnFileUploadFile) // 上传文件 api.GET("/file", fileController.DownloadFile) // 下载 文件 diff --git a/model/blog.go b/model/blog.go index 447f0d9..a04fdc4 100644 --- a/model/blog.go +++ b/model/blog.go @@ -145,6 +145,7 @@ func CreateDocTree(doc DocTree) error { NOT EXISTS ( SELECT * FROM doc_copy1 WHERE doc_copy1.title = '%s' );`, doc.Title, strings.Replace(doc.Content, "'", "\\'", -1), doc.Author, doc.Type, doc.IsPublic, time.Now().Format("2006-01-02 15:04:05"), doc.Father, doc.Level, doc.Title) _, e := db.GetMysqlClient().Query(sql) + log.Print(sql) if nil != e { log.Print(sql) logs.Error(e.Error())