obs.go 995 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package controller
  2. import (
  3. "eta_gn/eta_obs/controller/resp"
  4. "eta_gn/eta_obs/services/oss"
  5. "github.com/gin-gonic/gin"
  6. "strings"
  7. )
  8. type ObsController struct{}
  9. // File
  10. // @Description 文件读取
  11. // @Success 200 {string} string "获取成功"
  12. // @Router /auth/auth_code [post]
  13. func (a *ObsController) File(c *gin.Context) {
  14. objPath := c.Request.URL.Path
  15. objPath = strings.TrimLeft(objPath, "/")
  16. // ico过滤
  17. if objPath == `favicon.ico` {
  18. return
  19. }
  20. ossClient := new(oss.MinioOss)
  21. fileBytes, contentType, err := ossClient.GetFile(objPath)
  22. if err != nil {
  23. resp.FailData("异常请求", objPath+err.Error(), c)
  24. return
  25. }
  26. //fileName := objPath[strings.LastIndex(objPath, "/")+1:]
  27. //contentDisposition := `attachment`
  28. //if strings.HasPrefix(contentType, "image/") {
  29. // contentDisposition = `inline`
  30. //}
  31. //c.Header("Content-Disposition", fmt.Sprintf("%s; filename=%s", contentDisposition, fileName))
  32. c.Header("Content-Type", contentType)
  33. c.Writer.Write(fileBytes)
  34. return
  35. }