http.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package oss
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/gogf/gf/v2/util/gconv"
  7. "io"
  8. "log"
  9. "net/http"
  10. "strings"
  11. "app.yhyue.com/moapp/jybase/api"
  12. . "app.yhyue.com/moapp/jybase/common"
  13. "jygit.jydev.jianyu360.cn/BaseService/ossClient/constant"
  14. "jygit.jydev.jianyu360.cn/BaseService/ossService/util"
  15. )
  16. /* restful方式上传
  17. * @param bucket_id 桶id
  18. * @param object_name 对象名称
  19. * @param text 文本内容
  20. * @param file 附件
  21. * @param gzip 是否压缩,true or false
  22. * @return {"error_code":0,"error_msg":"上传成功"}
  23. */
  24. func UploadHandler(w http.ResponseWriter, r *http.Request) {
  25. if !api.R.CheckReqParam(w, r, "bucket_id", "object_name") {
  26. return
  27. }
  28. bucketID := r.FormValue("bucket_id")
  29. objectName := r.FormValue("object_name")
  30. gzipFlag := r.FormValue("gzip")
  31. log.Println(GetIp(r), "restful方式上传文件", bucketID, objectName, gzipFlag)
  32. text := strings.TrimSpace(r.FormValue("text"))
  33. var reader io.Reader
  34. file, _, err := r.FormFile("file")
  35. status, message := func() (int, string) {
  36. if err != nil {
  37. if text == "" {
  38. return api.Error_code_1002, api.Error_msg_1002 + ",file和text必须传一个"
  39. } else {
  40. reader = strings.NewReader(text)
  41. }
  42. } else {
  43. reader = file
  44. defer file.Close()
  45. }
  46. err = Upload(bucketID, objectName, reader, gzipFlag == "true")
  47. if err != nil {
  48. return -1, fmt.Sprintf(constant.UploadFail, err)
  49. }
  50. return 0, constant.UploadSuccess
  51. }()
  52. api.R.ServeJson(w, r, &api.Result{Error_code: status, Error_msg: message})
  53. }
  54. /* restful方式下载
  55. * @param bucket_id 桶id
  56. * @param object_name 对象名称
  57. * @return 文件内容
  58. */
  59. func DownloadHandler(w http.ResponseWriter, r *http.Request) {
  60. bucketID := r.FormValue("bucket_id")
  61. if bucketID == "" {
  62. http.Error(w, api.Error_msg_1002+"bucket_id", http.StatusInternalServerError)
  63. return
  64. }
  65. objectName := r.FormValue("object_name")
  66. if objectName == "" {
  67. http.Error(w, api.Error_msg_1002+"object_name", http.StatusInternalServerError)
  68. return
  69. }
  70. autoExtract := 0
  71. if gconv.Bool(r.FormValue("extract")) {
  72. autoExtract = 1
  73. }
  74. log.Println(GetIp(r), "restful方式下载文件", bucketID, objectName, autoExtract)
  75. if file_name := r.FormValue("file_name"); file_name != "" {
  76. w.Header().Set("Content-Disposition", "attachment; filename="+file_name)
  77. }
  78. if err := Download(w, autoExtract, bucketID, objectName); err != nil {
  79. log.Println(bucketID, objectName, "restful方式", fmt.Sprintf(constant.DownloadFail, err))
  80. http.Error(w, fmt.Sprintf(constant.DownloadFail, err), http.StatusInternalServerError)
  81. return
  82. }
  83. }
  84. /* restful方式删除
  85. * @param bucket_id 桶id
  86. * @param object_name 对象名称
  87. * @return {"error_code":0,"error_msg":"删除成功"}
  88. */
  89. func DeleteHandler(w http.ResponseWriter, r *http.Request) {
  90. if !api.R.CheckReqParam(w, r, "bucket_id", "object_name") {
  91. return
  92. }
  93. bucketID := r.FormValue("bucket_id")
  94. objectName := r.FormValue("object_name")
  95. log.Println(GetIp(r), "restful方式删除文件", bucketID, objectName)
  96. status, message := func() (int, string) {
  97. err := Delete(bucketID, objectName)
  98. if err != nil {
  99. return -1, fmt.Sprintf(constant.DeleteFail, err)
  100. }
  101. return 1, constant.DeleteSuccess
  102. }()
  103. api.R.ServeJson(w, r, &api.Result{Error_code: status, Error_msg: message})
  104. }
  105. // NodesHandler 接口用于查看当前在线节点信息(依赖heartbeat中redis心跳)
  106. func NodesHandler(w http.ResponseWriter, r *http.Request) {
  107. nodes, err := util.GetOnlineNodes(gctx.New()) // 此函数在 heartbeat 模块中实现
  108. var b []byte
  109. if err == nil {
  110. b, err = json.Marshal(nodes)
  111. }
  112. if err != nil {
  113. log.Println("Fetch nodes failed: %v", err)
  114. http.Error(w, "Fetch nodes failed: "+err.Error(), http.StatusInternalServerError)
  115. return
  116. }
  117. w.Header().Set("Content-Type", "application/json")
  118. w.Write(b)
  119. }
  120. /* 根据标讯id获取正文
  121. * @param bucket_id 桶id
  122. * @param object_name 对象名称
  123. * @return 标讯正文内容
  124. */
  125. func BidDetailHandler(w http.ResponseWriter, r *http.Request) {
  126. bucketID := r.FormValue("bucket_id")
  127. if bucketID == "" {
  128. http.Error(w, api.Error_msg_1002+"bucket_id", http.StatusInternalServerError)
  129. return
  130. }
  131. objectName := r.FormValue("object_name")
  132. if objectName == "" {
  133. http.Error(w, api.Error_msg_1002+"object_name", http.StatusInternalServerError)
  134. return
  135. }
  136. log.Println(GetIp(r), "获取标讯正文", bucketID, objectName)
  137. GetBidDetail(w, bucketID, objectName)
  138. }