drive.go 4.2 KB

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