Skip to content

Commit c26cf4d

Browse files
authored
fix: cloud storage (#2096)
1 parent 958a483 commit c26cf4d

File tree

1 file changed

+46
-73
lines changed

1 file changed

+46
-73
lines changed

route/v1/recover.go

+46-73
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package v1
22

33
import (
4+
"context"
5+
"net/http"
46
"strconv"
57
"strings"
68
"time"
@@ -15,49 +17,47 @@ import (
1517
)
1618

1719
func GetRecoverStorage(ctx echo.Context) error {
18-
ctx.Request().Header.Add("Content-Type", "text/html; charset=utf-8")
1920
t := ctx.Param("type")
2021
currentTime := time.Now().UTC()
2122
currentDate := time.Now().UTC().Format("2006-01-02")
2223
notify := make(map[string]interface{})
24+
event := "casaos:file:recover"
2325
if t == "GoogleDrive" {
2426
google_drive := google_drive.GetConfig()
2527
google_drive.Code = ctx.QueryParam("code")
2628
if len(google_drive.Code) == 0 {
27-
ctx.String(200, `<p>Code cannot be empty</p><script>window.close()</script>`)
2829
notify["status"] = "fail"
2930
notify["message"] = "Code cannot be empty"
3031
logger.Error("Then code is empty: ", zap.String("code", google_drive.Code), zap.Any("name", "google_drive"))
3132
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
33+
return ctx.HTML(http.StatusOK, `<p>Code cannot be empty</p><script>window.close()</script>`)
3234
}
33-
34-
err := google_drive.Init(ctx.Request().Context())
35+
err := google_drive.Init(context.Background())
3536
if err != nil {
36-
ctx.String(200, `<p>Initialization failure:`+err.Error()+`</p><script>window.close()</script>`)
3737
notify["status"] = "fail"
3838
notify["message"] = "Initialization failure"
3939
logger.Error("Then init error: ", zap.Error(err), zap.Any("name", "google_drive"))
40-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
41-
40+
service.MyService.Notify().SendNotify(event, notify)
41+
return ctx.HTML(http.StatusOK, `<p>Initialization failure:`+err.Error()+`</p><script>window.close()</script>`)
4242
}
4343

44-
username, err := google_drive.GetUserInfo(ctx.Request().Context())
44+
username, err := google_drive.GetUserInfo(context.Background())
4545
if err != nil {
46-
ctx.String(200, `<p>Failed to get user information:`+err.Error()+`</p><script>window.close()</script>`)
4746
notify["status"] = "fail"
4847
notify["message"] = "Failed to get user information"
4948
logger.Error("Then get user info error: ", zap.Error(err), zap.Any("name", "google_drive"))
50-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
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>`)
5151
}
5252
dmap := make(map[string]string)
5353
dmap["username"] = username
5454
configs, err := service.MyService.Storage().GetConfig()
5555
if err != nil {
56-
ctx.String(200, `<p>Failed to get rclone config:`+err.Error()+`</p><script>window.close()</script>`)
5756
notify["status"] = "fail"
5857
notify["message"] = "Failed to get rclone config"
5958
logger.Error("Then get config error: ", zap.Error(err), zap.Any("name", "google_drive"))
60-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
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>`)
6161
}
6262
for _, v := range configs.Remotes {
6363
cf, err := service.MyService.Storage().GetConfigByName(v)
@@ -66,22 +66,21 @@ func GetRecoverStorage(ctx echo.Context) error {
6666
continue
6767
}
6868
if cf["type"] == "drive" && cf["username"] == dmap["username"] {
69-
ctx.String(200, `<p>The same configuration has been added</p><script>window.close()</script>`)
7069
err := service.MyService.Storage().CheckAndMountByName(v)
7170
if err != nil {
7271
logger.Error("check and mount by name error: ", zap.Error(err), zap.Any("name", cf["username"]))
7372
}
7473
notify["status"] = "warn"
7574
notify["message"] = "The same configuration has been added"
76-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
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>`)
7777
}
7878
}
7979
if len(username) > 0 {
8080
a := strings.Split(username, "@")
8181
username = a[0]
8282
}
8383

84-
// username = fileutil.NameAccumulation(username, "/mnt")
8584
username += "_google_drive_" + strconv.FormatInt(time.Now().Unix(), 10)
8685

8786
dmap["client_id"] = google_drive.ClientID
@@ -95,44 +94,45 @@ func GetRecoverStorage(ctx echo.Context) error {
9594
notify["status"] = "success"
9695
notify["message"] = "Success"
9796
notify["driver"] = "GoogleDrive"
98-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
97+
service.MyService.Notify().SendNotify(event, notify)
9998
} else if t == "Dropbox" {
10099
dropbox := dropbox.GetConfig()
100+
101101
dropbox.Code = ctx.QueryParam("code")
102102
if len(dropbox.Code) == 0 {
103-
ctx.String(200, `<p>Code cannot be empty</p><script>window.close()</script>`)
103+
104104
notify["status"] = "fail"
105105
notify["message"] = "Code cannot be empty"
106106
logger.Error("Then code is empty error: ", zap.String("code", dropbox.Code), zap.Any("name", "dropbox"))
107-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
107+
service.MyService.Notify().SendNotify(event, notify)
108+
return ctx.HTML(http.StatusOK, `<p>Code cannot be empty</p><script>window.close()</script>`)
108109
}
109-
110-
err := dropbox.Init(ctx.Request().Context())
110+
err := dropbox.Init(context.Background())
111111
if err != nil {
112-
ctx.String(200, `<p>Initialization failure:`+err.Error()+`</p><script>window.close()</script>`)
113112
notify["status"] = "fail"
114113
notify["message"] = "Initialization failure"
115114
logger.Error("Then init error: ", zap.Error(err), zap.Any("name", "dropbox"))
116-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
115+
service.MyService.Notify().SendNotify(event, notify)
116+
return ctx.HTML(http.StatusOK, `<p>Initialization failure:`+err.Error()+`</p><script>window.close()</script>`)
117117
}
118-
username, err := dropbox.GetUserInfo(ctx.Request().Context())
118+
username, err := dropbox.GetUserInfo(context.Background())
119119
if err != nil {
120-
ctx.String(200, `<p>Failed to get user information:`+err.Error()+`</p><script>window.close()</script>`)
121120
notify["status"] = "fail"
122121
notify["message"] = "Failed to get user information"
123122
logger.Error("Then get user information: ", zap.Error(err), zap.Any("name", "dropbox"))
124-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
123+
service.MyService.Notify().SendNotify(event, notify)
124+
return ctx.HTML(http.StatusOK, `<p>Failed to get user information:`+err.Error()+`</p><script>window.close()</script>`)
125125
}
126126
dmap := make(map[string]string)
127127
dmap["username"] = username
128128

129129
configs, err := service.MyService.Storage().GetConfig()
130130
if err != nil {
131-
ctx.String(200, `<p>Failed to get rclone config:`+err.Error()+`</p><script>window.close()</script>`)
132131
notify["status"] = "fail"
133132
notify["message"] = "Failed to get rclone config"
134133
logger.Error("Then get config error: ", zap.Error(err), zap.Any("name", "dropbox"))
135-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
134+
service.MyService.Notify().SendNotify(event, notify)
135+
return ctx.HTML(http.StatusOK, `<p>Failed to get rclone config:`+err.Error()+`</p><script>window.close()</script>`)
136136
}
137137
for _, v := range configs.Remotes {
138138
cf, err := service.MyService.Storage().GetConfigByName(v)
@@ -141,15 +141,14 @@ func GetRecoverStorage(ctx echo.Context) error {
141141
continue
142142
}
143143
if cf["type"] == "dropbox" && cf["username"] == dmap["username"] {
144-
ctx.String(200, `<p>The same configuration has been added</p><script>window.close()</script>`)
145-
err := service.MyService.Storage().CheckAndMountByName(v)
146-
if err != nil {
144+
if err := service.MyService.Storage().CheckAndMountByName(v); err != nil {
147145
logger.Error("check and mount by name error: ", zap.Error(err), zap.Any("name", cf["username"]))
148146
}
149147

150148
notify["status"] = "warn"
151149
notify["message"] = "The same configuration has been added"
152-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
150+
service.MyService.Notify().SendNotify(event, notify)
151+
return ctx.HTML(http.StatusOK, `<p>The same configuration has been added</p><script>window.close()</script>`)
153152
}
154153
}
155154
if len(username) > 0 {
@@ -162,62 +161,49 @@ func GetRecoverStorage(ctx echo.Context) error {
162161
dmap["client_secret"] = dropbox.AppSecret
163162
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"}`
164163
dmap["mount_point"] = "/mnt/" + username
165-
// data.SetValue(username, "type", "dropbox")
166-
// data.SetValue(username, "client_id", add.AppKey)
167-
// data.SetValue(username, "client_secret", add.AppSecret)
168-
// data.SetValue(username, "mount_point", "/mnt/"+username)
169-
170-
// 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"}`)
171-
// e = data.Save()
172-
// if e != nil {
173-
// return ctx.String(200, `<p>保存配置失败:`+e.Error()+`</p>`)
174-
175-
// return
176-
// }
177164
service.MyService.Storage().CreateConfig(dmap, username, "dropbox")
178165
service.MyService.Storage().MountStorage("/mnt/"+username, username+":")
179166

180167
notify["status"] = "success"
181168
notify["message"] = "Success"
182169
notify["driver"] = "Dropbox"
183-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
170+
service.MyService.Notify().SendNotify(event, notify)
184171
} else if t == "Onedrive" {
185172
onedrive := onedrive.GetConfig()
186173
onedrive.Code = ctx.QueryParam("code")
187174
if len(onedrive.Code) == 0 {
188-
ctx.String(200, `<p>Code cannot be empty</p><script>window.close()</script>`)
189175
notify["status"] = "fail"
190176
notify["message"] = "Code cannot be empty"
191177
logger.Error("Then code is empty error: ", zap.String("code", onedrive.Code), zap.Any("name", "onedrive"))
192-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
178+
service.MyService.Notify().SendNotify(event, notify)
179+
return ctx.HTML(http.StatusOK, `<p>Code cannot be empty</p><script>window.close()</script>`)
193180
}
194-
195-
err := onedrive.Init(ctx.Request().Context())
181+
err := onedrive.Init(context.Background())
196182
if err != nil {
197-
ctx.String(200, `<p>Initialization failure:`+err.Error()+`</p><script>window.close()</script>`)
198183
notify["status"] = "fail"
199184
notify["message"] = "Initialization failure"
200185
logger.Error("Then init error: ", zap.Error(err), zap.Any("name", "onedrive"))
201-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
186+
service.MyService.Notify().SendNotify(event, notify)
187+
return ctx.HTML(http.StatusOK, `<p>Initialization failure:`+err.Error()+`</p><script>window.close()</script>`)
202188
}
203-
username, driveId, driveType, err := onedrive.GetInfo(ctx.Request().Context())
189+
username, driveId, driveType, err := onedrive.GetInfo(context.Background())
204190
if err != nil {
205-
ctx.String(200, `<p>Failed to get user information:`+err.Error()+`</p><script>window.close()</script>`)
206191
notify["status"] = "fail"
207192
notify["message"] = "Failed to get user information"
208193
logger.Error("Then get user information: ", zap.Error(err), zap.Any("name", "onedrive"))
209-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
194+
service.MyService.Notify().SendNotify(event, notify)
195+
return ctx.HTML(http.StatusOK, `<p>Failed to get user information:`+err.Error()+`</p><script>window.close()</script>`)
210196
}
211197
dmap := make(map[string]string)
212198
dmap["username"] = username
213199

214200
configs, err := service.MyService.Storage().GetConfig()
215201
if err != nil {
216-
ctx.String(200, `<p>Failed to get rclone config:`+err.Error()+`</p><script>window.close()</script>`)
217202
notify["status"] = "fail"
218203
notify["message"] = "Failed to get rclone config"
219204
logger.Error("Then get config error: ", zap.Error(err), zap.Any("name", "onedrive"))
220-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
205+
service.MyService.Notify().SendNotify(event, notify)
206+
return ctx.HTML(http.StatusOK, `<p>Failed to get rclone config:`+err.Error()+`</p><script>window.close()</script>`)
221207
}
222208
for _, v := range configs.Remotes {
223209
cf, err := service.MyService.Storage().GetConfigByName(v)
@@ -226,15 +212,14 @@ func GetRecoverStorage(ctx echo.Context) error {
226212
continue
227213
}
228214
if cf["type"] == "onedrive" && cf["username"] == dmap["username"] {
229-
ctx.String(200, `<p>The same configuration has been added</p><script>window.close()</script>`)
230-
err := service.MyService.Storage().CheckAndMountByName(v)
231-
if err != nil {
215+
if err := service.MyService.Storage().CheckAndMountByName(v); err != nil {
232216
logger.Error("check and mount by name error: ", zap.Error(err), zap.Any("name", cf["username"]))
233217
}
234218

235219
notify["status"] = "warn"
236220
notify["message"] = "The same configuration has been added"
237-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
221+
service.MyService.Notify().SendNotify(event, notify)
222+
return ctx.HTML(http.StatusOK, `<p>The same configuration has been added</p><script>window.close()</script>`)
238223
}
239224
}
240225
if len(username) > 0 {
@@ -249,26 +234,14 @@ func GetRecoverStorage(ctx echo.Context) error {
249234
dmap["mount_point"] = "/mnt/" + username
250235
dmap["drive_id"] = driveId
251236
dmap["drive_type"] = driveType
252-
// data.SetValue(username, "type", "dropbox")
253-
// data.SetValue(username, "client_id", add.AppKey)
254-
// data.SetValue(username, "client_secret", add.AppSecret)
255-
// data.SetValue(username, "mount_point", "/mnt/"+username)
256-
257-
// 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"}`)
258-
// e = data.Save()
259-
// if e != nil {
260-
// return ctx.String(200, `<p>保存配置失败:`+e.Error()+`</p>`)
261-
262-
// return
263-
// }
264237
service.MyService.Storage().CreateConfig(dmap, username, "onedrive")
265238
service.MyService.Storage().MountStorage("/mnt/"+username, username+":")
266239

267240
notify["status"] = "success"
268241
notify["message"] = "Success"
269242
notify["driver"] = "Onedrive"
270-
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
243+
service.MyService.Notify().SendNotify(event, notify)
271244
}
272245

273-
return ctx.String(200, `<p>Just close the page</p><script>window.close()</script>`)
246+
return ctx.HTML(200, `<p>Just close the page</p><script>window.close()</script>`)
274247
}

0 commit comments

Comments
 (0)