FileRouter.go 1002 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package router
  2. import (
  3. "crypto/md5"
  4. "file-manger-server/db"
  5. "file-manger-server/service/dao"
  6. "file-manger-server/util"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. func UploadInfo(c *gin.Context) {
  14. //获取用户数据信息
  15. claims, err := util.ValidateTokenToMyClaims(c.GetHeader("auth-sign"))
  16. if err != nil {
  17. c.JSON(200, CreateResultError(400, "token错误"))
  18. return
  19. }
  20. user, err := dao.UserDao{}.GetById(claims.Id)
  21. if err != nil || user.Id == 0 {
  22. c.JSON(200, CreateResultError(400, "用户信息错误"))
  23. return
  24. }
  25. tid := ToHexBytes(random16Bytes())
  26. db.FileUserDao{}.Insert(tid, user.Id)
  27. c.JSON(200, CreateResultData(gin.H{
  28. "transactionId": tid,
  29. }))
  30. }
  31. func ToHexBytes(bytes [16]byte) string {
  32. var hexBytes string
  33. for b := range bytes {
  34. hexBytes = fmt.Sprint(hexBytes, fmt.Sprintf("%02X", bytes[b]))
  35. }
  36. return strings.TrimSpace(hexBytes)
  37. }
  38. func random16Bytes() [16]byte {
  39. return md5.Sum([]byte(strconv.FormatInt(time.Now().UnixNano(), 10)))
  40. }