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.

package.go 7.1 kB

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package http
  2. import (
  3. "fmt"
  4. "mime/multipart"
  5. "net/http"
  6. "net/url"
  7. "path/filepath"
  8. "github.com/gin-gonic/gin"
  9. "gitlink.org.cn/cloudream/common/pkgs/logger"
  10. "gitlink.org.cn/cloudream/jcs-pub/client/internal/http/types"
  11. cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api/v1"
  12. clitypes "gitlink.org.cn/cloudream/jcs-pub/client/types"
  13. "gitlink.org.cn/cloudream/jcs-pub/common/ecode"
  14. )
  15. // PackageService 包服务,负责处理包相关的HTTP请求。
  16. type PackageService struct {
  17. *Server
  18. }
  19. // Package 返回PackageService的实例。
  20. func (s *Server) Package() *PackageService {
  21. return &PackageService{
  22. Server: s,
  23. }
  24. }
  25. func (s *PackageService) Get(ctx *gin.Context) {
  26. log := logger.WithField("HTTP", "Package.Get")
  27. var req cliapi.PackageGetReq
  28. if err := ctx.ShouldBindQuery(&req); err != nil {
  29. log.Warnf("binding body: %s", err.Error())
  30. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "missing argument or invalid argument"))
  31. return
  32. }
  33. pkg, err := s.svc.PackageSvc().Get(req.PackageID)
  34. if err != nil {
  35. log.Warnf("getting package: %s", err.Error())
  36. ctx.JSON(http.StatusOK, types.FailedError(err))
  37. return
  38. }
  39. ctx.JSON(http.StatusOK, types.OK(cliapi.PackageGetResp{Package: pkg}))
  40. }
  41. func (s *PackageService) GetByFullName(ctx *gin.Context) {
  42. log := logger.WithField("HTTP", "Package.GetByFullName")
  43. var req cliapi.PackageGetByFullName
  44. if err := ctx.ShouldBindQuery(&req); err != nil {
  45. log.Warnf("binding query: %s", err.Error())
  46. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "missing argument or invalid argument"))
  47. return
  48. }
  49. pkg, err := s.svc.PackageSvc().GetByFullName(req.BucketName, req.PackageName)
  50. if err != nil {
  51. log.Warnf("getting package by name: %s", err.Error())
  52. ctx.JSON(http.StatusOK, types.FailedError(err))
  53. return
  54. }
  55. ctx.JSON(http.StatusOK, types.OK(cliapi.PackageGetByFullNameResp{Package: pkg}))
  56. }
  57. // Create 处理创建新包的HTTP请求。
  58. func (s *PackageService) Create(ctx *gin.Context) {
  59. log := logger.WithField("HTTP", "Package.Create")
  60. var req cliapi.PackageCreate
  61. if err := ctx.ShouldBindJSON(&req); err != nil {
  62. log.Warnf("binding body: %s", err.Error())
  63. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "missing argument or invalid argument"))
  64. return
  65. }
  66. pkg, err := s.svc.PackageSvc().Create(req.BucketID, req.Name)
  67. if err != nil {
  68. log.Warnf("creating package: %s", err.Error())
  69. ctx.JSON(http.StatusOK, types.FailedError(err))
  70. return
  71. }
  72. ctx.JSON(http.StatusOK, types.OK(cliapi.PackageCreateResp{
  73. Package: pkg,
  74. }))
  75. }
  76. type PackageCreateUpload struct {
  77. Info cliapi.PackageCreateUploadInfo `form:"info" binding:"required"`
  78. Files []*multipart.FileHeader `form:"files"`
  79. }
  80. func (s *PackageService) CreateLoad(ctx *gin.Context) {
  81. log := logger.WithField("HTTP", "Package.CreateUpload")
  82. var req PackageCreateUpload
  83. if err := ctx.ShouldBind(&req); err != nil {
  84. log.Warnf("binding body: %s", err.Error())
  85. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "missing argument or invalid argument"))
  86. return
  87. }
  88. if len(req.Info.CopyTo) != len(req.Info.CopyToPath) {
  89. log.Warnf("CopyTo and CopyToPath count not match")
  90. ctx.JSON(http.StatusOK, types.Failed(ecode.BadArgument, "CopyTo and CopyToPath count not match"))
  91. return
  92. }
  93. copyToPath := make([]clitypes.JPath, 0, len(req.Info.CopyToPath))
  94. for _, p := range req.Info.CopyToPath {
  95. copyToPath = append(copyToPath, clitypes.PathFromJcsPathString(p))
  96. }
  97. up, err := s.svc.Uploader.BeginCreateUpload(req.Info.BucketID, req.Info.Name, req.Info.CopyTo, copyToPath)
  98. if err != nil {
  99. log.Warnf("begin package create upload: %s", err.Error())
  100. ctx.JSON(http.StatusOK, types.Failed(ecode.OperationFailed, "%v", err))
  101. return
  102. }
  103. defer up.Abort()
  104. var pathes []string
  105. for _, file := range req.Files {
  106. f, err := file.Open()
  107. if err != nil {
  108. log.Warnf("open file: %s", err.Error())
  109. ctx.JSON(http.StatusOK, types.Failed(ecode.OperationFailed, fmt.Sprintf("open file %v: %v", file.Filename, err)))
  110. return
  111. }
  112. path, err := url.PathUnescape(file.Filename)
  113. if err != nil {
  114. log.Warnf("unescape filename: %s", err.Error())
  115. ctx.JSON(http.StatusOK, types.Failed(ecode.OperationFailed, fmt.Sprintf("unescape filename %v: %v", file.Filename, err)))
  116. return
  117. }
  118. path = filepath.ToSlash(path)
  119. err = up.Upload(clitypes.PathFromJcsPathString(path), f)
  120. if err != nil {
  121. log.Warnf("uploading file: %s", err.Error())
  122. ctx.JSON(http.StatusOK, types.Failed(ecode.OperationFailed, fmt.Sprintf("uploading file %v: %v", file.Filename, err)))
  123. return
  124. }
  125. pathes = append(pathes, path)
  126. }
  127. ret, err := up.Commit()
  128. if err != nil {
  129. log.Warnf("commit create upload: %s", err.Error())
  130. ctx.JSON(http.StatusOK, types.Failed(ecode.OperationFailed, fmt.Sprintf("commit create upload: %v", err)))
  131. return
  132. }
  133. objs := make([]clitypes.Object, len(pathes))
  134. for i := range pathes {
  135. objs[i] = ret.Objects[pathes[i]]
  136. }
  137. ctx.JSON(http.StatusOK, types.OK(cliapi.PackageCreateUploadResp{Package: ret.Package, Objects: objs}))
  138. }
  139. func (s *PackageService) Delete(ctx *gin.Context) {
  140. log := logger.WithField("HTTP", "Package.Delete")
  141. var req cliapi.PackageDelete
  142. if err := ctx.ShouldBindJSON(&req); err != nil {
  143. log.Warnf("binding body: %s", err.Error())
  144. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "missing argument or invalid argument"))
  145. return
  146. }
  147. err := s.svc.PackageSvc().DeletePackage(req.PackageID)
  148. if err != nil {
  149. log.Warnf("deleting package: %s", err.Error())
  150. ctx.JSON(http.StatusOK, types.Failed(ecode.OperationFailed, "delete package failed"))
  151. return
  152. }
  153. ctx.JSON(http.StatusOK, types.OK(nil))
  154. }
  155. func (s *PackageService) Clone(ctx *gin.Context) {
  156. log := logger.WithField("HTTP", "Package.Clone")
  157. var req cliapi.PackageClone
  158. if err := ctx.ShouldBindJSON(&req); err != nil {
  159. log.Warnf("binding body: %s", err.Error())
  160. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "missing argument or invalid argument"))
  161. return
  162. }
  163. pkg, err := s.svc.PackageSvc().Clone(req.PackageID, req.BucketID, req.Name)
  164. if err != nil {
  165. log.Warnf("cloning package: %s", err.Error())
  166. ctx.JSON(http.StatusOK, types.Failed(ecode.OperationFailed, "clone package failed"))
  167. return
  168. }
  169. ctx.JSON(http.StatusOK, types.OK(cliapi.PackageCloneResp{
  170. Package: pkg,
  171. }))
  172. }
  173. func (s *PackageService) ListBucketPackages(ctx *gin.Context) {
  174. log := logger.WithField("HTTP", "Package.ListBucketPackages")
  175. var req cliapi.PackageListBucketPackages
  176. if err := ctx.ShouldBindQuery(&req); err != nil {
  177. log.Warnf("binding query: %s", err.Error())
  178. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "missing argument or invalid argument"))
  179. return
  180. }
  181. pkgs, err := s.svc.PackageSvc().GetBucketPackages(req.BucketID)
  182. if err != nil {
  183. log.Warnf("getting bucket packages: %s", err.Error())
  184. ctx.JSON(http.StatusOK, types.Failed(ecode.OperationFailed, "get bucket packages failed"))
  185. return
  186. }
  187. ctx.JSON(http.StatusOK, types.OK(cliapi.PackageListBucketPackagesResp{
  188. Packages: pkgs,
  189. }))
  190. }

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