(ctx echo.Context)
| 17 | ) |
| 18 | |
| 19 | func GetRecoverStorage(ctx echo.Context) error { |
| 20 | t := strings.TrimSuffix(ctx.Param("type"), "/") |
| 21 | currentTime := time.Now().UTC() |
| 22 | currentDate := time.Now().UTC().Format("2006-01-02") |
| 23 | notify := make(map[string]interface{}) |
| 24 | event := "casaos:file:recover" |
| 25 | if t == "GoogleDrive" { |
| 26 | google_drive := google_drive.GetConfig() |
| 27 | google_drive.Code = ctx.QueryParam("code") |
| 28 | if len(google_drive.Code) == 0 { |
| 29 | notify["status"] = "fail" |
| 30 | notify["message"] = "Code cannot be empty" |
| 31 | logger.Error("Then code is empty: ", zap.String("code", google_drive.Code), zap.Any("name", "google_drive")) |
| 32 | service.MyService.Notify().SendNotify("casaos:file:recover", notify) |
| 33 | return ctx.HTML(http.StatusOK, `<p>Code cannot be empty</p><script>window.close()</script>`) |
| 34 | } |
| 35 | err := google_drive.Init(context.Background()) |
| 36 | if err != nil { |
| 37 | notify["status"] = "fail" |
| 38 | notify["message"] = "Initialization failure" |
| 39 | logger.Error("Then init error: ", zap.Error(err), zap.Any("name", "google_drive")) |
| 40 | service.MyService.Notify().SendNotify(event, notify) |
| 41 | return ctx.HTML(http.StatusOK, `<p>Initialization failure:`+err.Error()+`</p><script>window.close()</script>`) |
| 42 | } |
| 43 | |
| 44 | username, err := google_drive.GetUserInfo(context.Background()) |
| 45 | if err != nil { |
| 46 | notify["status"] = "fail" |
| 47 | notify["message"] = "Failed to get user information" |
| 48 | logger.Error("Then get user info error: ", zap.Error(err), zap.Any("name", "google_drive")) |
| 49 | service.MyService.Notify().SendNotify(event, notify) |
| 50 | return ctx.HTML(http.StatusOK, `<p>Failed to get user information:`+err.Error()+`</p><script>window.close()</script>`) |
| 51 | } |
| 52 | dmap := make(map[string]string) |
| 53 | dmap["username"] = username |
| 54 | configs, err := service.MyService.Storage().GetConfig() |
| 55 | if err != nil { |
| 56 | notify["status"] = "fail" |
| 57 | notify["message"] = "Failed to get rclone config" |
| 58 | logger.Error("Then get config error: ", zap.Error(err), zap.Any("name", "google_drive")) |
| 59 | service.MyService.Notify().SendNotify(event, notify) |
| 60 | return ctx.HTML(http.StatusOK, `<p>Failed to get rclone config:`+err.Error()+`</p><script>window.close()</script>`) |
| 61 | } |
| 62 | for _, v := range configs.Remotes { |
| 63 | cf, err := service.MyService.Storage().GetConfigByName(v) |
| 64 | if err != nil { |
| 65 | logger.Error("then get config by name error: ", zap.Error(err), zap.Any("name", v)) |
| 66 | continue |
| 67 | } |
| 68 | if cf["type"] == "drive" && cf["username"] == dmap["username"] { |
| 69 | err := service.MyService.Storage().CheckAndMountByName(v) |
| 70 | if err != nil { |
| 71 | logger.Error("check and mount by name error: ", zap.Error(err), zap.Any("name", cf["username"])) |
| 72 | } |
| 73 | notify["status"] = "warn" |
| 74 | notify["message"] = "The same configuration has been added" |
| 75 | service.MyService.Notify().SendNotify(event, notify) |
| 76 | return ctx.HTML(http.StatusOK, `<p>The same configuration has been added</p><script>window.close()</script>`) |
nothing calls this directly
no test coverage detected