recover.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package v1
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. "github.com/IceWhaleTech/CasaOS-Common/utils/logger"
  7. "github.com/IceWhaleTech/CasaOS/drivers/dropbox"
  8. "github.com/IceWhaleTech/CasaOS/drivers/google_drive"
  9. "github.com/IceWhaleTech/CasaOS/drivers/onedrive"
  10. "github.com/IceWhaleTech/CasaOS/service"
  11. "github.com/gin-gonic/gin"
  12. "go.uber.org/zap"
  13. )
  14. func GetRecoverStorage(c *gin.Context) {
  15. c.Header("Content-Type", "text/html; charset=utf-8")
  16. t := c.Param("type")
  17. currentTime := time.Now().UTC()
  18. currentDate := time.Now().UTC().Format("2006-01-02")
  19. notify := make(map[string]interface{})
  20. if t == "GoogleDrive" {
  21. google_drive := google_drive.GetConfig()
  22. google_drive.Code = c.Query("code")
  23. if len(google_drive.Code) == 0 {
  24. c.String(200, `<p>Code cannot be empty</p><script>window.close()</script>`)
  25. notify["status"] = "fail"
  26. notify["message"] = "Code cannot be empty"
  27. logger.Error("Then code is empty: ", zap.String("code", google_drive.Code), zap.Any("name", "google_drive"))
  28. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  29. return
  30. }
  31. err := google_drive.Init(c)
  32. if err != nil {
  33. c.String(200, `<p>Initialization failure:`+err.Error()+`</p><script>window.close()</script>`)
  34. notify["status"] = "fail"
  35. notify["message"] = "Initialization failure"
  36. logger.Error("Then init error: ", zap.Error(err), zap.Any("name", "google_drive"))
  37. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  38. return
  39. }
  40. username, err := google_drive.GetUserInfo(c)
  41. if err != nil {
  42. c.String(200, `<p>Failed to get user information:`+err.Error()+`</p><script>window.close()</script>`)
  43. notify["status"] = "fail"
  44. notify["message"] = "Failed to get user information"
  45. logger.Error("Then get user info error: ", zap.Error(err), zap.Any("name", "google_drive"))
  46. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  47. return
  48. }
  49. dmap := make(map[string]string)
  50. dmap["username"] = username
  51. configs, err := service.MyService.Storage().GetConfig()
  52. if err != nil {
  53. c.String(200, `<p>Failed to get rclone config:`+err.Error()+`</p><script>window.close()</script>`)
  54. notify["status"] = "fail"
  55. notify["message"] = "Failed to get rclone config"
  56. logger.Error("Then get config error: ", zap.Error(err), zap.Any("name", "google_drive"))
  57. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  58. return
  59. }
  60. for _, v := range configs.Remotes {
  61. cf, err := service.MyService.Storage().GetConfigByName(v)
  62. if err != nil {
  63. logger.Error("then get config by name error: ", zap.Error(err), zap.Any("name", v))
  64. continue
  65. }
  66. if cf["type"] == "drive" && cf["username"] == dmap["username"] {
  67. c.String(200, `<p>The same configuration has been added</p><script>window.close()</script>`)
  68. err := service.MyService.Storage().CheckAndMountByName(v)
  69. if err != nil {
  70. logger.Error("check and mount by name error: ", zap.Error(err), zap.Any("name", cf["username"]))
  71. }
  72. notify["status"] = "warn"
  73. notify["message"] = "The same configuration has been added"
  74. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  75. return
  76. }
  77. }
  78. if len(username) > 0 {
  79. a := strings.Split(username, "@")
  80. username = a[0]
  81. }
  82. //username = fileutil.NameAccumulation(username, "/mnt")
  83. username += "_google_drive_" + strconv.FormatInt(time.Now().Unix(), 10)
  84. dmap["client_id"] = google_drive.ClientID
  85. dmap["client_secret"] = google_drive.ClientSecret
  86. dmap["scope"] = "drive"
  87. dmap["mount_point"] = "/mnt/" + username
  88. dmap["token"] = `{"access_token":"` + google_drive.AccessToken + `","token_type":"Bearer","refresh_token":"` + google_drive.RefreshToken + `","expiry":"` + currentDate + `T` + currentTime.Add(time.Hour*1).Add(time.Minute*50).Format("15:04:05") + `Z"}`
  89. service.MyService.Storage().CreateConfig(dmap, username, "drive")
  90. service.MyService.Storage().MountStorage("/mnt/"+username, username+":")
  91. notify := make(map[string]interface{})
  92. notify["status"] = "success"
  93. notify["message"] = "Success"
  94. notify["driver"] = "GoogleDrive"
  95. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  96. } else if t == "Dropbox" {
  97. dropbox := dropbox.GetConfig()
  98. dropbox.Code = c.Query("code")
  99. if len(dropbox.Code) == 0 {
  100. c.String(200, `<p>Code cannot be empty</p><script>window.close()</script>`)
  101. notify["status"] = "fail"
  102. notify["message"] = "Code cannot be empty"
  103. logger.Error("Then code is empty error: ", zap.String("code", dropbox.Code), zap.Any("name", "dropbox"))
  104. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  105. return
  106. }
  107. err := dropbox.Init(c)
  108. if err != nil {
  109. c.String(200, `<p>Initialization failure:`+err.Error()+`</p><script>window.close()</script>`)
  110. notify["status"] = "fail"
  111. notify["message"] = "Initialization failure"
  112. logger.Error("Then init error: ", zap.Error(err), zap.Any("name", "dropbox"))
  113. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  114. return
  115. }
  116. username, err := dropbox.GetUserInfo(c)
  117. if err != nil {
  118. c.String(200, `<p>Failed to get user information:`+err.Error()+`</p><script>window.close()</script>`)
  119. notify["status"] = "fail"
  120. notify["message"] = "Failed to get user information"
  121. logger.Error("Then get user information: ", zap.Error(err), zap.Any("name", "dropbox"))
  122. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  123. return
  124. }
  125. dmap := make(map[string]string)
  126. dmap["username"] = username
  127. configs, err := service.MyService.Storage().GetConfig()
  128. if err != nil {
  129. c.String(200, `<p>Failed to get rclone config:`+err.Error()+`</p><script>window.close()</script>`)
  130. notify["status"] = "fail"
  131. notify["message"] = "Failed to get rclone config"
  132. logger.Error("Then get config error: ", zap.Error(err), zap.Any("name", "dropbox"))
  133. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  134. return
  135. }
  136. for _, v := range configs.Remotes {
  137. cf, err := service.MyService.Storage().GetConfigByName(v)
  138. if err != nil {
  139. logger.Error("then get config by name error: ", zap.Error(err), zap.Any("name", v))
  140. continue
  141. }
  142. if cf["type"] == "dropbox" && cf["username"] == dmap["username"] {
  143. c.String(200, `<p>The same configuration has been added</p><script>window.close()</script>`)
  144. err := service.MyService.Storage().CheckAndMountByName(v)
  145. if err != nil {
  146. logger.Error("check and mount by name error: ", zap.Error(err), zap.Any("name", cf["username"]))
  147. }
  148. notify["status"] = "warn"
  149. notify["message"] = "The same configuration has been added"
  150. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  151. return
  152. }
  153. }
  154. if len(username) > 0 {
  155. a := strings.Split(username, "@")
  156. username = a[0]
  157. }
  158. username += "_dropbox_" + strconv.FormatInt(time.Now().Unix(), 10)
  159. dmap["client_id"] = dropbox.AppKey
  160. dmap["client_secret"] = dropbox.AppSecret
  161. dmap["token"] = `{"access_token":"` + dropbox.AccessToken + `","token_type":"bearer","refresh_token":"` + dropbox.Addition.RefreshToken + `","expiry":"` + currentDate + `T` + currentTime.Add(time.Hour*3).Add(time.Minute*50).Format("15:04:05") + `.780385354Z"}`
  162. dmap["mount_point"] = "/mnt/" + username
  163. // data.SetValue(username, "type", "dropbox")
  164. // data.SetValue(username, "client_id", add.AppKey)
  165. // data.SetValue(username, "client_secret", add.AppSecret)
  166. // data.SetValue(username, "mount_point", "/mnt/"+username)
  167. // data.SetValue(username, "token", `{"access_token":"`+dropbox.AccessToken+`","token_type":"bearer","refresh_token":"`+dropbox.Addition.RefreshToken+`","expiry":"`+currentDate+`T`+currentTime.Add(time.Hour*3).Format("15:04:05")+`.780385354Z"}`)
  168. // e = data.Save()
  169. // if e != nil {
  170. // c.String(200, `<p>保存配置失败:`+e.Error()+`</p>`)
  171. // return
  172. // }
  173. service.MyService.Storage().CreateConfig(dmap, username, "dropbox")
  174. service.MyService.Storage().MountStorage("/mnt/"+username, username+":")
  175. notify["status"] = "success"
  176. notify["message"] = "Success"
  177. notify["driver"] = "Dropbox"
  178. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  179. } else if t == "Onedrive" {
  180. onedrive := onedrive.GetConfig()
  181. onedrive.Code = c.Query("code")
  182. if len(onedrive.Code) == 0 {
  183. c.String(200, `<p>Code cannot be empty</p><script>window.close()</script>`)
  184. notify["status"] = "fail"
  185. notify["message"] = "Code cannot be empty"
  186. logger.Error("Then code is empty error: ", zap.String("code", onedrive.Code), zap.Any("name", "onedrive"))
  187. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  188. return
  189. }
  190. err := onedrive.Init(c)
  191. if err != nil {
  192. c.String(200, `<p>Initialization failure:`+err.Error()+`</p><script>window.close()</script>`)
  193. notify["status"] = "fail"
  194. notify["message"] = "Initialization failure"
  195. logger.Error("Then init error: ", zap.Error(err), zap.Any("name", "onedrive"))
  196. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  197. return
  198. }
  199. username, driveId, driveType, err := onedrive.GetInfo(c)
  200. if err != nil {
  201. c.String(200, `<p>Failed to get user information:`+err.Error()+`</p><script>window.close()</script>`)
  202. notify["status"] = "fail"
  203. notify["message"] = "Failed to get user information"
  204. logger.Error("Then get user information: ", zap.Error(err), zap.Any("name", "onedrive"))
  205. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  206. return
  207. }
  208. dmap := make(map[string]string)
  209. dmap["username"] = username
  210. configs, err := service.MyService.Storage().GetConfig()
  211. if err != nil {
  212. c.String(200, `<p>Failed to get rclone config:`+err.Error()+`</p><script>window.close()</script>`)
  213. notify["status"] = "fail"
  214. notify["message"] = "Failed to get rclone config"
  215. logger.Error("Then get config error: ", zap.Error(err), zap.Any("name", "onedrive"))
  216. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  217. return
  218. }
  219. for _, v := range configs.Remotes {
  220. cf, err := service.MyService.Storage().GetConfigByName(v)
  221. if err != nil {
  222. logger.Error("then get config by name error: ", zap.Error(err), zap.Any("name", v))
  223. continue
  224. }
  225. if cf["type"] == "onedrive" && cf["username"] == dmap["username"] {
  226. c.String(200, `<p>The same configuration has been added</p><script>window.close()</script>`)
  227. err := service.MyService.Storage().CheckAndMountByName(v)
  228. if err != nil {
  229. logger.Error("check and mount by name error: ", zap.Error(err), zap.Any("name", cf["username"]))
  230. }
  231. notify["status"] = "warn"
  232. notify["message"] = "The same configuration has been added"
  233. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  234. return
  235. }
  236. }
  237. if len(username) > 0 {
  238. a := strings.Split(username, "@")
  239. username = a[0]
  240. }
  241. username += "_onedrive_" + strconv.FormatInt(time.Now().Unix(), 10)
  242. dmap["client_id"] = onedrive.ClientID
  243. dmap["client_secret"] = onedrive.ClientSecret
  244. dmap["token"] = `{"access_token":"` + onedrive.AccessToken + `","token_type":"bearer","refresh_token":"` + onedrive.RefreshToken + `","expiry":"` + currentDate + `T` + currentTime.Add(time.Hour*3).Add(time.Minute*50).Format("15:04:05") + `.780385354Z"}`
  245. dmap["mount_point"] = "/mnt/" + username
  246. dmap["drive_id"] = driveId
  247. dmap["drive_type"] = driveType
  248. // data.SetValue(username, "type", "dropbox")
  249. // data.SetValue(username, "client_id", add.AppKey)
  250. // data.SetValue(username, "client_secret", add.AppSecret)
  251. // data.SetValue(username, "mount_point", "/mnt/"+username)
  252. // data.SetValue(username, "token", `{"access_token":"`+dropbox.AccessToken+`","token_type":"bearer","refresh_token":"`+dropbox.Addition.RefreshToken+`","expiry":"`+currentDate+`T`+currentTime.Add(time.Hour*3).Format("15:04:05")+`.780385354Z"}`)
  253. // e = data.Save()
  254. // if e != nil {
  255. // c.String(200, `<p>保存配置失败:`+e.Error()+`</p>`)
  256. // return
  257. // }
  258. service.MyService.Storage().CreateConfig(dmap, username, "onedrive")
  259. service.MyService.Storage().MountStorage("/mnt/"+username, username+":")
  260. notify["status"] = "success"
  261. notify["message"] = "Success"
  262. notify["driver"] = "Onedrive"
  263. service.MyService.Notify().SendNotify("casaos:file:recover", notify)
  264. }
  265. c.String(200, `<p>Just close the page</p><script>window.close()</script>`)
  266. }