drive.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package httper
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "time"
  8. "github.com/IceWhaleTech/CasaOS-Common/utils/logger"
  9. "github.com/go-resty/resty/v2"
  10. "go.uber.org/zap"
  11. )
  12. type MountList struct {
  13. MountPoints []struct {
  14. MountPoint string `json:"MountPoint"`
  15. Fs string `json:"Fs"`
  16. Icon string `json:"Icon"`
  17. } `json:"mountPoints"`
  18. }
  19. type MountPoint struct {
  20. MountPoint string `json:"mount_point"`
  21. Fs string `json:"fs"`
  22. Icon string `json:"icon"`
  23. }
  24. type MountResult struct {
  25. Error string `json:"error"`
  26. Input struct {
  27. Fs string `json:"fs"`
  28. MountPoint string `json:"mountPoint"`
  29. } `json:"input"`
  30. Path string `json:"path"`
  31. Status int `json:"status"`
  32. }
  33. type RemotesResult struct {
  34. Remotes []string `json:"remotes"`
  35. }
  36. var UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
  37. var DefaultTimeout = time.Second * 30
  38. func NewRestyClient() *resty.Client {
  39. unixSocket := "/tmp/rclone.sock"
  40. transport := http.Transport{
  41. Dial: func(_, _ string) (net.Conn, error) {
  42. return net.Dial("unix", unixSocket)
  43. },
  44. }
  45. client := resty.New()
  46. client.SetTransport(&transport).SetBaseURL("http://localhost")
  47. client.SetRetryCount(3).SetRetryWaitTime(5*time.Second).SetTimeout(DefaultTimeout).SetHeader("User-Agent", UserAgent)
  48. return client
  49. }
  50. func GetMountList() (MountList, error) {
  51. var result MountList
  52. res, err := NewRestyClient().R().Post("/mount/listmounts")
  53. if err != nil {
  54. return result, err
  55. }
  56. if res.StatusCode() != 200 {
  57. return result, fmt.Errorf("get mount list failed")
  58. }
  59. json.Unmarshal(res.Body(), &result)
  60. for i := 0; i < len(result.MountPoints); i++ {
  61. result.MountPoints[i].Fs = result.MountPoints[i].Fs[:len(result.MountPoints[i].Fs)-1]
  62. }
  63. return result, err
  64. }
  65. func Mount(mountPoint string, fs string) error {
  66. res, err := NewRestyClient().R().SetFormData(map[string]string{
  67. "mountPoint": mountPoint,
  68. "fs": fs,
  69. }).Post("/mount/mount")
  70. if err != nil {
  71. return err
  72. }
  73. if res.StatusCode() != 200 {
  74. return fmt.Errorf("mount failed")
  75. }
  76. logger.Info("mount then", zap.Any("res", res.Body()))
  77. return nil
  78. }
  79. func Unmount(mountPoint string) error {
  80. res, err := NewRestyClient().R().SetFormData(map[string]string{
  81. "mountPoint": mountPoint,
  82. }).Post("/mount/unmount")
  83. if err != nil {
  84. logger.Error("when unmount", zap.Error(err))
  85. return err
  86. }
  87. if res.StatusCode() != 200 {
  88. logger.Error("then unmount failed", zap.Any("res", res.Body()))
  89. return fmt.Errorf("unmount failed")
  90. }
  91. logger.Info("unmount then", zap.Any("res", res.Body()))
  92. return nil
  93. }
  94. func CreateConfig(data map[string]string, name, t string) error {
  95. data["config_is_local"] = "false"
  96. dataStr, _ := json.Marshal(data)
  97. res, err := NewRestyClient().R().SetFormData(map[string]string{
  98. "name": name,
  99. "parameters": string(dataStr),
  100. "type": t,
  101. }).Post("/config/create")
  102. logger.Info("when create config then", zap.Any("res", res.Body()))
  103. if err != nil {
  104. return err
  105. }
  106. if res.StatusCode() != 200 {
  107. return fmt.Errorf("create config failed")
  108. }
  109. return nil
  110. }
  111. func GetConfigByName(name string) (map[string]string, error) {
  112. res, err := NewRestyClient().R().SetFormData(map[string]string{
  113. "name": name,
  114. }).Post("/config/get")
  115. if err != nil {
  116. return nil, err
  117. }
  118. if res.StatusCode() != 200 {
  119. return nil, fmt.Errorf("create config failed")
  120. }
  121. var result map[string]string
  122. json.Unmarshal(res.Body(), &result)
  123. return result, nil
  124. }
  125. func GetAllConfigName() (RemotesResult, error) {
  126. var result RemotesResult
  127. res, err := NewRestyClient().R().SetFormData(map[string]string{}).Post("/config/listremotes")
  128. if err != nil {
  129. return result, err
  130. }
  131. if res.StatusCode() != 200 {
  132. return result, fmt.Errorf("get config failed")
  133. }
  134. json.Unmarshal(res.Body(), &result)
  135. return result, nil
  136. }
  137. func DeleteConfigByName(name string) error {
  138. res, err := NewRestyClient().R().SetFormData(map[string]string{
  139. "name": name,
  140. }).Post("/config/delete")
  141. if err != nil {
  142. return err
  143. }
  144. if res.StatusCode() != 200 {
  145. return fmt.Errorf("delete config failed")
  146. }
  147. return nil
  148. }