cloud.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package v1
  2. import (
  3. "strings"
  4. "github.com/IceWhaleTech/CasaOS-Common/utils/logger"
  5. "github.com/IceWhaleTech/CasaOS/drivers/dropbox"
  6. "github.com/IceWhaleTech/CasaOS/drivers/google_drive"
  7. "github.com/IceWhaleTech/CasaOS/drivers/onedrive"
  8. "github.com/IceWhaleTech/CasaOS/model"
  9. "github.com/IceWhaleTech/CasaOS/pkg/utils/common_err"
  10. "github.com/IceWhaleTech/CasaOS/pkg/utils/httper"
  11. "github.com/IceWhaleTech/CasaOS/service"
  12. "github.com/gin-gonic/gin"
  13. "go.uber.org/zap"
  14. )
  15. func ListStorages(c *gin.Context) {
  16. // var req model.PageReq
  17. // if err := c.ShouldBind(&req); err != nil {
  18. // c.JSON(common_err.SUCCESS, model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.CLIENT_ERROR), Data: err.Error()})
  19. // return
  20. // }
  21. // req.Validate()
  22. //logger.Info("ListStorages", zap.Any("req", req))
  23. //storages, total, err := service.MyService.Storage().GetStorages(req.Page, req.PerPage)
  24. // if err != nil {
  25. // c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
  26. // return
  27. // }
  28. // c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: model.PageResp{
  29. // Content: storages,
  30. // Total: total,
  31. // }})
  32. r, err := service.MyService.Storage().GetStorages()
  33. if err != nil {
  34. c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
  35. return
  36. }
  37. for i := 0; i < len(r.MountPoints); i++ {
  38. dataMap, err := service.MyService.Storage().GetConfigByName(r.MountPoints[i].Fs)
  39. if err != nil {
  40. logger.Error("GetConfigByName", zap.Any("err", err))
  41. continue
  42. }
  43. if dataMap["type"] == "drive" {
  44. r.MountPoints[i].Icon = google_drive.ICONURL
  45. }
  46. if dataMap["type"] == "dropbox" {
  47. r.MountPoints[i].Icon = dropbox.ICONURL
  48. }
  49. if dataMap["type"] == "onedrive" {
  50. r.MountPoints[i].Icon = onedrive.ICONURL
  51. }
  52. r.MountPoints[i].Name = dataMap["username"]
  53. }
  54. list := []httper.MountPoint{}
  55. for _, v := range r.MountPoints {
  56. list = append(list, httper.MountPoint{
  57. Fs: v.Fs,
  58. Icon: v.Icon,
  59. MountPoint: v.MountPoint,
  60. Name: v.Name,
  61. })
  62. }
  63. c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: list})
  64. }
  65. func UmountStorage(c *gin.Context) {
  66. json := make(map[string]string)
  67. c.ShouldBind(&json)
  68. mountPoint := json["mount_point"]
  69. if mountPoint == "" {
  70. c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.CLIENT_ERROR), Data: "mount_point is empty"})
  71. return
  72. }
  73. err := service.MyService.Storage().UnmountStorage(mountPoint)
  74. if err != nil {
  75. c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
  76. return
  77. }
  78. service.MyService.Storage().DeleteConfigByName(strings.ReplaceAll(mountPoint, "/mnt/", ""))
  79. c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: "success"})
  80. }
  81. func GetStorage(c *gin.Context) {
  82. // idStr := c.Query("id")
  83. // id, err := strconv.Atoi(idStr)
  84. // if err != nil {
  85. // c.JSON(common_err.SUCCESS, model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.CLIENT_ERROR), Data: err.Error()})
  86. // return
  87. // }
  88. // storage, err := service.MyService.Storage().GetStorageById(uint(id))
  89. // if err != nil {
  90. // c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
  91. // return
  92. // }
  93. // c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: storage})
  94. }