Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

58 rindas
1.3 KiB

  1. package main
  2. import (
  3. "egg-im/pkg/logger"
  4. "egg-im/pkg/util"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "go.uber.org/zap"
  10. "github.com/gin-gonic/gin"
  11. )
  12. const baseUrl = "http://111.229.238.28:8085/file/"
  13. type Response struct {
  14. Code int `json:"code"`
  15. Message string `json:"message"`
  16. Data interface{} `json:"data"`
  17. }
  18. func main() {
  19. router := gin.Default()
  20. router.Static("/file", "/root/file")
  21. // Set a lower memory limit for multipart forms (default is 32 MiB)
  22. router.MaxMultipartMemory = 8 << 20 // 8 MiB
  23. router.POST("/upload", func(c *gin.Context) {
  24. // single file
  25. file, err := c.FormFile("file")
  26. if err != nil {
  27. c.JSON(http.StatusOK, Response{Code: 1001, Message: err.Error()})
  28. return
  29. }
  30. filenames := strings.Split(file.Filename, ".")
  31. name := strconv.FormatInt(time.Now().UnixNano(), 10) + "-" + util.RandString(30) + "." + filenames[len(filenames)-1]
  32. filePath := "/root/file/" + name
  33. err = c.SaveUploadedFile(file, filePath)
  34. if err != nil {
  35. c.JSON(http.StatusOK, Response{Code: 1001, Message: err.Error()})
  36. return
  37. }
  38. c.JSON(http.StatusOK, Response{
  39. Code: 0,
  40. Message: "success",
  41. Data: map[string]string{"url": baseUrl + name},
  42. })
  43. })
  44. err := router.Run(":8085")
  45. if err != nil {
  46. logger.Logger.Error("Run error", zap.Error(err))
  47. }
  48. }