|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package http
-
- import (
- "fmt"
- "net/http"
-
- "github.com/gin-gonic/gin"
- "gitlink.org.cn/cloudream/common/consts/errorcode"
- "gitlink.org.cn/cloudream/common/pkgs/logger"
- cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api"
- )
-
- type UserSpaceService struct {
- *Server
- }
-
- func (s *Server) UserSpace() *UserSpaceService {
- return &UserSpaceService{
- Server: s,
- }
- }
-
- func (s *UserSpaceService) DownloadPackage(ctx *gin.Context) {
- log := logger.WithField("HTTP", "UserSpace.DownloadPackage")
-
- var req cliapi.UserSpaceDownloadPackageReq
- if err := ctx.ShouldBindJSON(&req); err != nil {
- log.Warnf("binding body: %s", err.Error())
- ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
- return
- }
-
- err := s.svc.UserSpaceSvc().DownloadPackage(req.PackageID, req.UserSpaceID, req.RootPath)
- if err != nil {
- log.Warnf("downloading package: %s", err.Error())
- ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "%v", err))
- return
- }
-
- ctx.JSON(http.StatusOK, OK(cliapi.UserSpaceDownloadPackageResp{}))
- }
-
- func (s *UserSpaceService) CreatePackage(ctx *gin.Context) {
- log := logger.WithField("HTTP", "UserSpace.CreatePackage")
-
- var req cliapi.UserSpaceCreatePackageReq
- if err := ctx.ShouldBindJSON(&req); err != nil {
- log.Warnf("binding body: %s", err.Error())
- ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
- return
- }
-
- pkg, err := s.svc.Uploader.UserSpaceUpload(req.UserSpaceID, req.Path, req.BucketID, req.Name, req.SpaceAffinity)
- if err != nil {
- log.Warnf("userspace create package: %s", err.Error())
- ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("userspace create package: %v", err)))
- return
- }
-
- ctx.JSON(http.StatusOK, OK(cliapi.UserSpaceCreatePackageResp{
- Package: *pkg,
- }))
- }
-
- func (s *UserSpaceService) Get(ctx *gin.Context) {
- log := logger.WithField("HTTP", "UserSpace.Get")
-
- var req cliapi.UserSpaceGet
- if err := ctx.ShouldBindQuery(&req); err != nil {
- log.Warnf("binding query: %s", err.Error())
- ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
- return
- }
-
- info, err := s.svc.UserSpaceSvc().Get(req.UserSpaceID)
- if err != nil {
- log.Warnf("getting info: %s", err.Error())
- ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get userspace inf failed"))
- return
- }
-
- ctx.JSON(http.StatusOK, OK(cliapi.UserSpaceGetResp{
- UserSpace: info,
- }))
- }
-
- func (s *UserSpaceService) SpaceToSpace(ctx *gin.Context) {
- log := logger.WithField("HTTP", "UserSpace.SpaceToSpace")
-
- var req cliapi.UserSpaceSpaceToSpace
- if err := ctx.ShouldBindJSON(&req); err != nil {
- log.Warnf("binding body: %s", err.Error())
- ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
- return
- }
-
- ret, err := s.svc.UserSpaceSvc().SpaceToSpace(req.SrcUserSpaceID, req.SrcPath, req.DstUserSpaceID, req.DstPath)
- if err != nil {
- log.Warnf("space2space: %s", err.Error())
- ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "space2space failed"))
- return
- }
-
- ctx.JSON(http.StatusOK, OK(cliapi.UserSpaceSpaceToSpaceResp{
- SpaceToSpaceResult: ret,
- }))
- }
|