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.

object.go 6.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package http
  2. import (
  3. "fmt"
  4. "io"
  5. "mime/multipart"
  6. "net/http"
  7. ul "net/url"
  8. "path"
  9. "time"
  10. "github.com/gin-gonic/gin"
  11. "gitlink.org.cn/cloudream/common/consts/errorcode"
  12. "gitlink.org.cn/cloudream/common/pkgs/logger"
  13. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  14. myhttp "gitlink.org.cn/cloudream/common/utils/http"
  15. "gitlink.org.cn/cloudream/storage/common/pkgs/downloader"
  16. )
  17. type ObjectService struct {
  18. *Server
  19. }
  20. func (s *Server) Object() *ObjectService {
  21. return &ObjectService{
  22. Server: s,
  23. }
  24. }
  25. type ObjectUploadReq struct {
  26. Info cdssdk.ObjectUploadInfo `form:"info" binding:"required"`
  27. Files []*multipart.FileHeader `form:"files"`
  28. }
  29. func (s *ObjectService) Upload(ctx *gin.Context) {
  30. log := logger.WithField("HTTP", "Object.Upload")
  31. var req ObjectUploadReq
  32. if err := ctx.ShouldBind(&req); err != nil {
  33. log.Warnf("binding body: %s", err.Error())
  34. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  35. return
  36. }
  37. var err error
  38. objIter := mapMultiPartFileToUploadingObject(req.Files)
  39. taskID, err := s.svc.ObjectSvc().StartUploading(req.Info.UserID, req.Info.PackageID, objIter, req.Info.NodeAffinity)
  40. if err != nil {
  41. log.Warnf("start uploading object task: %s", err.Error())
  42. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "start uploading task failed"))
  43. return
  44. }
  45. for {
  46. complete, objs, err := s.svc.ObjectSvc().WaitUploading(taskID, time.Second*5)
  47. if complete {
  48. if err != nil {
  49. log.Warnf("uploading object: %s", err.Error())
  50. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "uploading object failed"))
  51. return
  52. }
  53. uploadeds := make([]cdssdk.UploadedObject, len(objs.Objects))
  54. for i, obj := range objs.Objects {
  55. err := ""
  56. if obj.Error != nil {
  57. err = obj.Error.Error()
  58. }
  59. o := obj.Object
  60. uploadeds[i] = cdssdk.UploadedObject{
  61. Object: &o,
  62. Error: err,
  63. }
  64. }
  65. ctx.JSON(http.StatusOK, OK(cdssdk.ObjectUploadResp{Uploadeds: uploadeds}))
  66. return
  67. }
  68. if err != nil {
  69. log.Warnf("waiting task: %s", err.Error())
  70. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "wait uploading task failed"))
  71. return
  72. }
  73. }
  74. }
  75. func (s *ObjectService) Download(ctx *gin.Context) {
  76. log := logger.WithField("HTTP", "Object.Download")
  77. var req cdssdk.ObjectDownload
  78. if err := ctx.ShouldBindQuery(&req); err != nil {
  79. log.Warnf("binding body: %s", err.Error())
  80. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  81. return
  82. }
  83. off := req.Offset
  84. len := int64(-1)
  85. if req.Length != nil {
  86. len = *req.Length
  87. }
  88. file, err := s.svc.ObjectSvc().Download(req.UserID, downloader.DownloadReqeust{
  89. ObjectID: req.ObjectID,
  90. Offset: off,
  91. Length: len,
  92. })
  93. if err != nil {
  94. log.Warnf("downloading object: %s", err.Error())
  95. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "download object failed"))
  96. return
  97. }
  98. mw := multipart.NewWriter(ctx.Writer)
  99. defer mw.Close()
  100. ctx.Writer.Header().Set("Content-Type", fmt.Sprintf("%s;boundary=%s", myhttp.ContentTypeMultiPart, mw.Boundary()))
  101. ctx.Writer.WriteHeader(http.StatusOK)
  102. if req.PartSize == 0 {
  103. err = sendFileOnePart(mw, "file", path.Base(file.Object.Path), file.File)
  104. } else {
  105. err = sendFileMultiPart(mw, "file", path.Base(file.Object.Path), file.File, req.PartSize)
  106. }
  107. if err != nil {
  108. log.Warnf("copying file: %s", err.Error())
  109. }
  110. }
  111. func sendFileMultiPart(muWriter *multipart.Writer, fieldName, fileName string, file io.ReadCloser, partSize int64) error {
  112. for {
  113. w, err := muWriter.CreateFormFile(fieldName, ul.PathEscape(fileName))
  114. if err != nil {
  115. return fmt.Errorf("create form file failed, err: %w", err)
  116. }
  117. n, err := io.Copy(w, io.LimitReader(file, partSize))
  118. if err != nil {
  119. return err
  120. }
  121. if n == 0 {
  122. break
  123. }
  124. }
  125. return nil
  126. }
  127. func sendFileOnePart(muWriter *multipart.Writer, fieldName, fileName string, file io.ReadCloser) error {
  128. w, err := muWriter.CreateFormFile(fieldName, ul.PathEscape(fileName))
  129. if err != nil {
  130. return fmt.Errorf("create form file failed, err: %w", err)
  131. }
  132. _, err = io.Copy(w, file)
  133. return err
  134. }
  135. func (s *ObjectService) UpdateInfo(ctx *gin.Context) {
  136. log := logger.WithField("HTTP", "Object.UpdateInfo")
  137. var req cdssdk.ObjectUpdateInfo
  138. if err := ctx.ShouldBindJSON(&req); err != nil {
  139. log.Warnf("binding body: %s", err.Error())
  140. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  141. return
  142. }
  143. sucs, err := s.svc.ObjectSvc().UpdateInfo(req.UserID, req.Updatings)
  144. if err != nil {
  145. log.Warnf("updating objects: %s", err.Error())
  146. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "update objects failed"))
  147. return
  148. }
  149. ctx.JSON(http.StatusOK, OK(cdssdk.ObjectUpdateInfoResp{Successes: sucs}))
  150. }
  151. func (s *ObjectService) Move(ctx *gin.Context) {
  152. log := logger.WithField("HTTP", "Object.Move")
  153. var req cdssdk.ObjectMove
  154. if err := ctx.ShouldBindJSON(&req); err != nil {
  155. log.Warnf("binding body: %s", err.Error())
  156. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  157. return
  158. }
  159. sucs, err := s.svc.ObjectSvc().Move(req.UserID, req.Movings)
  160. if err != nil {
  161. log.Warnf("moving objects: %s", err.Error())
  162. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "move objects failed"))
  163. return
  164. }
  165. ctx.JSON(http.StatusOK, OK(cdssdk.ObjectMoveResp{Successes: sucs}))
  166. }
  167. func (s *ObjectService) Delete(ctx *gin.Context) {
  168. log := logger.WithField("HTTP", "Object.Delete")
  169. var req cdssdk.ObjectDelete
  170. if err := ctx.ShouldBindJSON(&req); err != nil {
  171. log.Warnf("binding body: %s", err.Error())
  172. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  173. return
  174. }
  175. err := s.svc.ObjectSvc().Delete(req.UserID, req.ObjectIDs)
  176. if err != nil {
  177. log.Warnf("deleting objects: %s", err.Error())
  178. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "delete objects failed"))
  179. return
  180. }
  181. ctx.JSON(http.StatusOK, OK(nil))
  182. }
  183. func (s *ObjectService) GetPackageObjects(ctx *gin.Context) {
  184. log := logger.WithField("HTTP", "Object.GetPackageObjects")
  185. var req cdssdk.ObjectGetPackageObjects
  186. if err := ctx.ShouldBindQuery(&req); err != nil {
  187. log.Warnf("binding body: %s", err.Error())
  188. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  189. return
  190. }
  191. objs, err := s.svc.ObjectSvc().GetPackageObjects(req.UserID, req.PackageID)
  192. if err != nil {
  193. log.Warnf("getting package objects: %s", err.Error())
  194. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get package object failed"))
  195. return
  196. }
  197. ctx.JSON(http.StatusOK, OK(cdssdk.ObjectGetPackageObjectsResp{Objects: objs}))
  198. }

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