You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

presigned.go 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package http
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "net/url"
  7. "path"
  8. "path/filepath"
  9. "github.com/gin-gonic/gin"
  10. "gitlink.org.cn/cloudream/common/consts/errorcode"
  11. "gitlink.org.cn/cloudream/common/pkgs/logger"
  12. "gitlink.org.cn/cloudream/common/sdks/storage/cdsapi"
  13. "gitlink.org.cn/cloudream/common/utils/math2"
  14. "gitlink.org.cn/cloudream/storage/client/internal/config"
  15. "gitlink.org.cn/cloudream/storage/common/pkgs/downloader"
  16. )
  17. type PresignedService struct {
  18. *Server
  19. }
  20. func (s *Server) Presigned() *PresignedService {
  21. return &PresignedService{
  22. Server: s,
  23. }
  24. }
  25. func (s *PresignedService) ObjectDownloadByPath(ctx *gin.Context) {
  26. log := logger.WithField("HTTP", "Presigned.ObjectDownloadByPath")
  27. var req cdsapi.PresignedObjectDownloadByPath
  28. if err := ctx.ShouldBindQuery(&req); err != nil {
  29. log.Warnf("binding query: %s", err.Error())
  30. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  31. return
  32. }
  33. resp, err := s.svc.ObjectSvc().GetByPath(cdsapi.ObjectListByPath{
  34. UserID: req.UserID, PackageID: req.PackageID, Path: req.Path,
  35. })
  36. if err != nil {
  37. log.Warnf("getting object by path: %s", err.Error())
  38. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get object by path failed"))
  39. return
  40. }
  41. if len(resp.Objects) == 0 {
  42. log.Warnf("object not found: %s", req.Path)
  43. ctx.JSON(http.StatusOK, Failed(errorcode.DataNotFound, "object not found"))
  44. return
  45. }
  46. off := req.Offset
  47. len := int64(-1)
  48. if req.Length != nil {
  49. len = *req.Length
  50. }
  51. file, err := s.svc.ObjectSvc().Download(req.UserID, downloader.DownloadReqeust{
  52. ObjectID: resp.Objects[0].ObjectID,
  53. Offset: off,
  54. Length: len,
  55. })
  56. if err != nil {
  57. log.Warnf("downloading object: %s", err.Error())
  58. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "download object failed"))
  59. return
  60. }
  61. defer file.File.Close()
  62. ctx.Header("Content-Disposition", "attachment; filename="+url.PathEscape(path.Base(file.Object.Path)))
  63. ctx.Header("Content-Type", "application/octet-stream")
  64. ctx.Header("Content-Transfer-Encoding", "binary")
  65. n, err := io.Copy(ctx.Writer, file.File)
  66. if err != nil {
  67. log.Warnf("copying file: %s", err.Error())
  68. }
  69. if config.Cfg().StorageID > 0 {
  70. s.svc.AccessStat.AddAccessCounter(file.Object.ObjectID, file.Object.PackageID, config.Cfg().StorageID, math2.DivOrDefault(float64(n), float64(file.Object.Size), 1))
  71. }
  72. }
  73. func (s *PresignedService) ObjectDownload(ctx *gin.Context) {
  74. log := logger.WithField("HTTP", "Presigned.ObjectDownloadByPath")
  75. var req cdsapi.PresignedObjectDownload
  76. if err := ctx.ShouldBindQuery(&req); err != nil {
  77. log.Warnf("binding query: %s", err.Error())
  78. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  79. return
  80. }
  81. off := req.Offset
  82. len := int64(-1)
  83. if req.Length != nil {
  84. len = *req.Length
  85. }
  86. file, err := s.svc.ObjectSvc().Download(req.UserID, downloader.DownloadReqeust{
  87. ObjectID: req.ObjectID,
  88. Offset: off,
  89. Length: len,
  90. })
  91. if err != nil {
  92. log.Warnf("downloading object: %s", err.Error())
  93. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "download object failed"))
  94. return
  95. }
  96. defer file.File.Close()
  97. ctx.Header("Content-Disposition", "attachment; filename="+url.PathEscape(path.Base(file.Object.Path)))
  98. ctx.Header("Content-Type", "application/octet-stream")
  99. ctx.Header("Content-Transfer-Encoding", "binary")
  100. n, err := io.Copy(ctx.Writer, file.File)
  101. if err != nil {
  102. log.Warnf("copying file: %s", err.Error())
  103. }
  104. if config.Cfg().StorageID > 0 {
  105. s.svc.AccessStat.AddAccessCounter(file.Object.ObjectID, file.Object.PackageID, config.Cfg().StorageID, math2.DivOrDefault(float64(n), float64(file.Object.Size), 1))
  106. }
  107. }
  108. func (s *PresignedService) ObjectUpload(ctx *gin.Context) {
  109. log := logger.WithField("HTTP", "Presigned.ObjectUpload")
  110. var req cdsapi.PresignedObjectUpload
  111. if err := ctx.ShouldBindQuery(&req); err != nil {
  112. log.Warnf("binding query: %s", err.Error())
  113. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  114. return
  115. }
  116. up, err := s.svc.Uploader.BeginUpdate(req.UserID, req.PackageID, req.Affinity, req.LoadTo, req.LoadToPath)
  117. if err != nil {
  118. log.Warnf("begin update: %s", err.Error())
  119. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("begin update: %v", err)))
  120. return
  121. }
  122. defer up.Abort()
  123. path := filepath.ToSlash(req.Path)
  124. err = up.Upload(path, ctx.Request.Body)
  125. if err != nil {
  126. log.Warnf("uploading file: %s", err.Error())
  127. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("uploading file %v: %v", req.Path, err)))
  128. return
  129. }
  130. ret, err := up.Commit()
  131. if err != nil {
  132. log.Warnf("commit update: %s", err.Error())
  133. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("commit update: %v", err)))
  134. return
  135. }
  136. ctx.JSON(http.StatusOK, OK(cdsapi.PresignedObjectUploadResp{Object: ret.Objects[path]}))
  137. }
  138. func (s *PresignedService) ObjectNewMultipartUpload(ctx *gin.Context) {
  139. log := logger.WithField("HTTP", "Presigned.ObjectNewMultipartUpload")
  140. var req cdsapi.PresignedObjectNewMultipartUpload
  141. if err := ctx.ShouldBindQuery(&req); err != nil {
  142. log.Warnf("binding query: %s", err.Error())
  143. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  144. return
  145. }
  146. obj, err := s.svc.ObjectSvc().NewMultipartUploadObject(req.UserID, req.PackageID, req.Path)
  147. if err != nil {
  148. log.Warnf("new multipart upload: %s", err.Error())
  149. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("new multipart upload: %v", err)))
  150. return
  151. }
  152. ctx.JSON(http.StatusOK, OK(cdsapi.PresignedObjectUploadResp{Object: obj}))
  153. }
  154. func (s *PresignedService) ObjectUploadPart(ctx *gin.Context) {
  155. log := logger.WithField("HTTP", "Presigned.ObjectUploadPart")
  156. var req cdsapi.PresignedObjectUploadPart
  157. if err := ctx.ShouldBindQuery(&req); err != nil {
  158. log.Warnf("binding query: %s", err.Error())
  159. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  160. return
  161. }
  162. err := s.svc.Uploader.UploadPart(req.UserID, req.ObjectID, req.Index, ctx.Request.Body)
  163. if err != nil {
  164. log.Warnf("uploading part: %s", err.Error())
  165. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("upload part: %v", err)))
  166. return
  167. }
  168. ctx.JSON(http.StatusOK, OK(cdsapi.ObjectUploadPartResp{}))
  169. }
  170. func (s *PresignedService) ObjectCompleteMultipartUpload(ctx *gin.Context) {
  171. log := logger.WithField("HTTP", "Presigned.ObjectCompleteMultipartUpload")
  172. var req cdsapi.PresignedObjectCompleteMultipartUpload
  173. if err := ctx.ShouldBindQuery(&req); err != nil {
  174. log.Warnf("binding query: %s", err.Error())
  175. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  176. return
  177. }
  178. obj, err := s.svc.ObjectSvc().CompleteMultipartUpload(req.UserID, req.ObjectID, req.Indexes)
  179. if err != nil {
  180. log.Warnf("completing multipart upload: %s", err.Error())
  181. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("complete multipart upload: %v", err)))
  182. return
  183. }
  184. ctx.JSON(http.StatusOK, OK(cdsapi.ObjectCompleteMultipartUploadResp{Object: obj}))
  185. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。