diff --git a/route/v1/recover.go b/route/v1/recover.go index c5b0e762..70acc7f0 100644 --- a/route/v1/recover.go +++ b/route/v1/recover.go @@ -1,6 +1,8 @@ package v1 import ( + "context" + "net/http" "strconv" "strings" "time" @@ -15,49 +17,47 @@ import ( ) func GetRecoverStorage(ctx echo.Context) error { - ctx.Request().Header.Add("Content-Type", "text/html; charset=utf-8") t := ctx.Param("type") currentTime := time.Now().UTC() currentDate := time.Now().UTC().Format("2006-01-02") notify := make(map[string]interface{}) + event := "casaos:file:recover" if t == "GoogleDrive" { google_drive := google_drive.GetConfig() google_drive.Code = ctx.QueryParam("code") if len(google_drive.Code) == 0 { - ctx.String(200, `
Code cannot be empty
`) notify["status"] = "fail" notify["message"] = "Code cannot be empty" logger.Error("Then code is empty: ", zap.String("code", google_drive.Code), zap.Any("name", "google_drive")) service.MyService.Notify().SendNotify("casaos:file:recover", notify) + return ctx.HTML(http.StatusOK, `Code cannot be empty
`) } - - err := google_drive.Init(ctx.Request().Context()) + err := google_drive.Init(context.Background()) if err != nil { - ctx.String(200, `Initialization failure:`+err.Error()+`
`) notify["status"] = "fail" notify["message"] = "Initialization failure" logger.Error("Then init error: ", zap.Error(err), zap.Any("name", "google_drive")) - service.MyService.Notify().SendNotify("casaos:file:recover", notify) - + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `Initialization failure:`+err.Error()+`
`) } - username, err := google_drive.GetUserInfo(ctx.Request().Context()) + username, err := google_drive.GetUserInfo(context.Background()) if err != nil { - ctx.String(200, `Failed to get user information:`+err.Error()+`
`) notify["status"] = "fail" notify["message"] = "Failed to get user information" logger.Error("Then get user info error: ", zap.Error(err), zap.Any("name", "google_drive")) - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `Failed to get user information:`+err.Error()+`
`) } dmap := make(map[string]string) dmap["username"] = username configs, err := service.MyService.Storage().GetConfig() if err != nil { - ctx.String(200, `Failed to get rclone config:`+err.Error()+`
`) notify["status"] = "fail" notify["message"] = "Failed to get rclone config" logger.Error("Then get config error: ", zap.Error(err), zap.Any("name", "google_drive")) - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `Failed to get rclone config:`+err.Error()+`
`) } for _, v := range configs.Remotes { cf, err := service.MyService.Storage().GetConfigByName(v) @@ -66,14 +66,14 @@ func GetRecoverStorage(ctx echo.Context) error { continue } if cf["type"] == "drive" && cf["username"] == dmap["username"] { - ctx.String(200, `The same configuration has been added
`) err := service.MyService.Storage().CheckAndMountByName(v) if err != nil { logger.Error("check and mount by name error: ", zap.Error(err), zap.Any("name", cf["username"])) } notify["status"] = "warn" notify["message"] = "The same configuration has been added" - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `The same configuration has been added
`) } } if len(username) > 0 { @@ -81,7 +81,6 @@ func GetRecoverStorage(ctx echo.Context) error { username = a[0] } - // username = fileutil.NameAccumulation(username, "/mnt") username += "_google_drive_" + strconv.FormatInt(time.Now().Unix(), 10) dmap["client_id"] = google_drive.ClientID @@ -95,44 +94,45 @@ func GetRecoverStorage(ctx echo.Context) error { notify["status"] = "success" notify["message"] = "Success" notify["driver"] = "GoogleDrive" - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) } else if t == "Dropbox" { dropbox := dropbox.GetConfig() + dropbox.Code = ctx.QueryParam("code") if len(dropbox.Code) == 0 { - ctx.String(200, `Code cannot be empty
`) + notify["status"] = "fail" notify["message"] = "Code cannot be empty" logger.Error("Then code is empty error: ", zap.String("code", dropbox.Code), zap.Any("name", "dropbox")) - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `Code cannot be empty
`) } - - err := dropbox.Init(ctx.Request().Context()) + err := dropbox.Init(context.Background()) if err != nil { - ctx.String(200, `Initialization failure:`+err.Error()+`
`) notify["status"] = "fail" notify["message"] = "Initialization failure" logger.Error("Then init error: ", zap.Error(err), zap.Any("name", "dropbox")) - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `Initialization failure:`+err.Error()+`
`) } - username, err := dropbox.GetUserInfo(ctx.Request().Context()) + username, err := dropbox.GetUserInfo(context.Background()) if err != nil { - ctx.String(200, `Failed to get user information:`+err.Error()+`
`) notify["status"] = "fail" notify["message"] = "Failed to get user information" logger.Error("Then get user information: ", zap.Error(err), zap.Any("name", "dropbox")) - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `Failed to get user information:`+err.Error()+`
`) } dmap := make(map[string]string) dmap["username"] = username configs, err := service.MyService.Storage().GetConfig() if err != nil { - ctx.String(200, `Failed to get rclone config:`+err.Error()+`
`) notify["status"] = "fail" notify["message"] = "Failed to get rclone config" logger.Error("Then get config error: ", zap.Error(err), zap.Any("name", "dropbox")) - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `Failed to get rclone config:`+err.Error()+`
`) } for _, v := range configs.Remotes { cf, err := service.MyService.Storage().GetConfigByName(v) @@ -141,15 +141,14 @@ func GetRecoverStorage(ctx echo.Context) error { continue } if cf["type"] == "dropbox" && cf["username"] == dmap["username"] { - ctx.String(200, `The same configuration has been added
`) - err := service.MyService.Storage().CheckAndMountByName(v) - if err != nil { + if err := service.MyService.Storage().CheckAndMountByName(v); err != nil { logger.Error("check and mount by name error: ", zap.Error(err), zap.Any("name", cf["username"])) } notify["status"] = "warn" notify["message"] = "The same configuration has been added" - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `The same configuration has been added
`) } } if len(username) > 0 { @@ -162,62 +161,49 @@ func GetRecoverStorage(ctx echo.Context) error { dmap["client_secret"] = dropbox.AppSecret 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"}` dmap["mount_point"] = "/mnt/" + username - // data.SetValue(username, "type", "dropbox") - // data.SetValue(username, "client_id", add.AppKey) - // data.SetValue(username, "client_secret", add.AppSecret) - // data.SetValue(username, "mount_point", "/mnt/"+username) - - // 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"}`) - // e = data.Save() - // if e != nil { - // return ctx.String(200, `保存配置失败:`+e.Error()+`
`) - - // return - // } service.MyService.Storage().CreateConfig(dmap, username, "dropbox") service.MyService.Storage().MountStorage("/mnt/"+username, username+":") notify["status"] = "success" notify["message"] = "Success" notify["driver"] = "Dropbox" - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) } else if t == "Onedrive" { onedrive := onedrive.GetConfig() onedrive.Code = ctx.QueryParam("code") if len(onedrive.Code) == 0 { - ctx.String(200, `Code cannot be empty
`) notify["status"] = "fail" notify["message"] = "Code cannot be empty" logger.Error("Then code is empty error: ", zap.String("code", onedrive.Code), zap.Any("name", "onedrive")) - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `Code cannot be empty
`) } - - err := onedrive.Init(ctx.Request().Context()) + err := onedrive.Init(context.Background()) if err != nil { - ctx.String(200, `Initialization failure:`+err.Error()+`
`) notify["status"] = "fail" notify["message"] = "Initialization failure" logger.Error("Then init error: ", zap.Error(err), zap.Any("name", "onedrive")) - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `Initialization failure:`+err.Error()+`
`) } - username, driveId, driveType, err := onedrive.GetInfo(ctx.Request().Context()) + username, driveId, driveType, err := onedrive.GetInfo(context.Background()) if err != nil { - ctx.String(200, `Failed to get user information:`+err.Error()+`
`) notify["status"] = "fail" notify["message"] = "Failed to get user information" logger.Error("Then get user information: ", zap.Error(err), zap.Any("name", "onedrive")) - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `Failed to get user information:`+err.Error()+`
`) } dmap := make(map[string]string) dmap["username"] = username configs, err := service.MyService.Storage().GetConfig() if err != nil { - ctx.String(200, `Failed to get rclone config:`+err.Error()+`
`) notify["status"] = "fail" notify["message"] = "Failed to get rclone config" logger.Error("Then get config error: ", zap.Error(err), zap.Any("name", "onedrive")) - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `Failed to get rclone config:`+err.Error()+`
`) } for _, v := range configs.Remotes { cf, err := service.MyService.Storage().GetConfigByName(v) @@ -226,15 +212,14 @@ func GetRecoverStorage(ctx echo.Context) error { continue } if cf["type"] == "onedrive" && cf["username"] == dmap["username"] { - ctx.String(200, `The same configuration has been added
`) - err := service.MyService.Storage().CheckAndMountByName(v) - if err != nil { + if err := service.MyService.Storage().CheckAndMountByName(v); err != nil { logger.Error("check and mount by name error: ", zap.Error(err), zap.Any("name", cf["username"])) } notify["status"] = "warn" notify["message"] = "The same configuration has been added" - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) + return ctx.HTML(http.StatusOK, `The same configuration has been added
`) } } if len(username) > 0 { @@ -249,26 +234,14 @@ func GetRecoverStorage(ctx echo.Context) error { dmap["mount_point"] = "/mnt/" + username dmap["drive_id"] = driveId dmap["drive_type"] = driveType - // data.SetValue(username, "type", "dropbox") - // data.SetValue(username, "client_id", add.AppKey) - // data.SetValue(username, "client_secret", add.AppSecret) - // data.SetValue(username, "mount_point", "/mnt/"+username) - - // 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"}`) - // e = data.Save() - // if e != nil { - // return ctx.String(200, `保存配置失败:`+e.Error()+`
`) - - // return - // } service.MyService.Storage().CreateConfig(dmap, username, "onedrive") service.MyService.Storage().MountStorage("/mnt/"+username, username+":") notify["status"] = "success" notify["message"] = "Success" notify["driver"] = "Onedrive" - service.MyService.Notify().SendNotify("casaos:file:recover", notify) + service.MyService.Notify().SendNotify(event, notify) } - return ctx.String(200, `Just close the page
`) + return ctx.HTML(200, `Just close the page
`) }