drive.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. return err
  85. }
  86. if res.StatusCode() != 200 {
  87. return fmt.Errorf("unmount failed")
  88. }
  89. logger.Info("unmount then", zap.Any("res", res.Body()))
  90. return nil
  91. }
  92. func CreateConfig(data map[string]string, name, t string) error {
  93. data["config_is_local"] = "false"
  94. dataStr, _ := json.Marshal(data)
  95. res, err := NewRestyClient().R().SetFormData(map[string]string{
  96. "name": name,
  97. "parameters": string(dataStr),
  98. "type": t,
  99. }).Post("/config/create")
  100. logger.Info("when create config then", zap.Any("res", res.Body()))
  101. if err != nil {
  102. return err
  103. }
  104. if res.StatusCode() != 200 {
  105. return fmt.Errorf("create config failed")
  106. }
  107. return nil
  108. }
  109. func GetConfigByName(name string) (map[string]string, error) {
  110. res, err := NewRestyClient().R().SetFormData(map[string]string{
  111. "name": name,
  112. }).Post("/config/get")
  113. if err != nil {
  114. return nil, err
  115. }
  116. if res.StatusCode() != 200 {
  117. return nil, fmt.Errorf("create config failed")
  118. }
  119. var result map[string]string
  120. json.Unmarshal(res.Body(), &result)
  121. return result, nil
  122. }
  123. func GetAllConfigName() (RemotesResult, error) {
  124. var result RemotesResult
  125. res, err := NewRestyClient().R().SetFormData(map[string]string{}).Post("/config/listremotes")
  126. if err != nil {
  127. return result, err
  128. }
  129. if res.StatusCode() != 200 {
  130. return result, fmt.Errorf("get config failed")
  131. }
  132. json.Unmarshal(res.Body(), &result)
  133. return result, nil
  134. }
  135. func DeleteConfigByName(name string) error {
  136. res, err := NewRestyClient().R().SetFormData(map[string]string{
  137. "name": name,
  138. }).Post("/config/delete")
  139. if err != nil {
  140. return err
  141. }
  142. if res.StatusCode() != 200 {
  143. return fmt.Errorf("delete config failed")
  144. }
  145. return nil
  146. }