1
1
package v1
2
2
3
3
import (
4
+ "context"
5
+ "net/http"
4
6
"strconv"
5
7
"strings"
6
8
"time"
@@ -15,49 +17,47 @@ import (
15
17
)
16
18
17
19
func GetRecoverStorage (ctx echo.Context ) error {
18
- ctx .Request ().Header .Add ("Content-Type" , "text/html; charset=utf-8" )
19
20
t := ctx .Param ("type" )
20
21
currentTime := time .Now ().UTC ()
21
22
currentDate := time .Now ().UTC ().Format ("2006-01-02" )
22
23
notify := make (map [string ]interface {})
24
+ event := "casaos:file:recover"
23
25
if t == "GoogleDrive" {
24
26
google_drive := google_drive .GetConfig ()
25
27
google_drive .Code = ctx .QueryParam ("code" )
26
28
if len (google_drive .Code ) == 0 {
27
- ctx .String (200 , `<p>Code cannot be empty</p><script>window.close()</script>` )
28
29
notify ["status" ] = "fail"
29
30
notify ["message" ] = "Code cannot be empty"
30
31
logger .Error ("Then code is empty: " , zap .String ("code" , google_drive .Code ), zap .Any ("name" , "google_drive" ))
31
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>` )
32
34
}
33
-
34
- err := google_drive .Init (ctx .Request ().Context ())
35
+ err := google_drive .Init (context .Background ())
35
36
if err != nil {
36
- ctx .String (200 , `<p>Initialization failure:` + err .Error ()+ `</p><script>window.close()</script>` )
37
37
notify ["status" ] = "fail"
38
38
notify ["message" ] = "Initialization failure"
39
39
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>` )
42
42
}
43
43
44
- username , err := google_drive .GetUserInfo (ctx . Request (). Context ())
44
+ username , err := google_drive .GetUserInfo (context . Background ())
45
45
if err != nil {
46
- ctx .String (200 , `<p>Failed to get user information:` + err .Error ()+ `</p><script>window.close()</script>` )
47
46
notify ["status" ] = "fail"
48
47
notify ["message" ] = "Failed to get user information"
49
48
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>` )
51
51
}
52
52
dmap := make (map [string ]string )
53
53
dmap ["username" ] = username
54
54
configs , err := service .MyService .Storage ().GetConfig ()
55
55
if err != nil {
56
- ctx .String (200 , `<p>Failed to get rclone config:` + err .Error ()+ `</p><script>window.close()</script>` )
57
56
notify ["status" ] = "fail"
58
57
notify ["message" ] = "Failed to get rclone config"
59
58
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>` )
61
61
}
62
62
for _ , v := range configs .Remotes {
63
63
cf , err := service .MyService .Storage ().GetConfigByName (v )
@@ -66,22 +66,21 @@ func GetRecoverStorage(ctx echo.Context) error {
66
66
continue
67
67
}
68
68
if cf ["type" ] == "drive" && cf ["username" ] == dmap ["username" ] {
69
- ctx .String (200 , `<p>The same configuration has been added</p><script>window.close()</script>` )
70
69
err := service .MyService .Storage ().CheckAndMountByName (v )
71
70
if err != nil {
72
71
logger .Error ("check and mount by name error: " , zap .Error (err ), zap .Any ("name" , cf ["username" ]))
73
72
}
74
73
notify ["status" ] = "warn"
75
74
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>` )
77
77
}
78
78
}
79
79
if len (username ) > 0 {
80
80
a := strings .Split (username , "@" )
81
81
username = a [0 ]
82
82
}
83
83
84
- // username = fileutil.NameAccumulation(username, "/mnt")
85
84
username += "_google_drive_" + strconv .FormatInt (time .Now ().Unix (), 10 )
86
85
87
86
dmap ["client_id" ] = google_drive .ClientID
@@ -95,44 +94,45 @@ func GetRecoverStorage(ctx echo.Context) error {
95
94
notify ["status" ] = "success"
96
95
notify ["message" ] = "Success"
97
96
notify ["driver" ] = "GoogleDrive"
98
- service .MyService .Notify ().SendNotify ("casaos:file:recover" , notify )
97
+ service .MyService .Notify ().SendNotify (event , notify )
99
98
} else if t == "Dropbox" {
100
99
dropbox := dropbox .GetConfig ()
100
+
101
101
dropbox .Code = ctx .QueryParam ("code" )
102
102
if len (dropbox .Code ) == 0 {
103
- ctx . String ( 200 , `<p>Code cannot be empty</p><script>window.close()</script>` )
103
+
104
104
notify ["status" ] = "fail"
105
105
notify ["message" ] = "Code cannot be empty"
106
106
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>` )
108
109
}
109
-
110
- err := dropbox .Init (ctx .Request ().Context ())
110
+ err := dropbox .Init (context .Background ())
111
111
if err != nil {
112
- ctx .String (200 , `<p>Initialization failure:` + err .Error ()+ `</p><script>window.close()</script>` )
113
112
notify ["status" ] = "fail"
114
113
notify ["message" ] = "Initialization failure"
115
114
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>` )
117
117
}
118
- username , err := dropbox .GetUserInfo (ctx . Request (). Context ())
118
+ username , err := dropbox .GetUserInfo (context . Background ())
119
119
if err != nil {
120
- ctx .String (200 , `<p>Failed to get user information:` + err .Error ()+ `</p><script>window.close()</script>` )
121
120
notify ["status" ] = "fail"
122
121
notify ["message" ] = "Failed to get user information"
123
122
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>` )
125
125
}
126
126
dmap := make (map [string ]string )
127
127
dmap ["username" ] = username
128
128
129
129
configs , err := service .MyService .Storage ().GetConfig ()
130
130
if err != nil {
131
- ctx .String (200 , `<p>Failed to get rclone config:` + err .Error ()+ `</p><script>window.close()</script>` )
132
131
notify ["status" ] = "fail"
133
132
notify ["message" ] = "Failed to get rclone config"
134
133
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>` )
136
136
}
137
137
for _ , v := range configs .Remotes {
138
138
cf , err := service .MyService .Storage ().GetConfigByName (v )
@@ -141,15 +141,14 @@ func GetRecoverStorage(ctx echo.Context) error {
141
141
continue
142
142
}
143
143
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 {
147
145
logger .Error ("check and mount by name error: " , zap .Error (err ), zap .Any ("name" , cf ["username" ]))
148
146
}
149
147
150
148
notify ["status" ] = "warn"
151
149
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>` )
153
152
}
154
153
}
155
154
if len (username ) > 0 {
@@ -162,62 +161,49 @@ func GetRecoverStorage(ctx echo.Context) error {
162
161
dmap ["client_secret" ] = dropbox .AppSecret
163
162
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"}`
164
163
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
- // }
177
164
service .MyService .Storage ().CreateConfig (dmap , username , "dropbox" )
178
165
service .MyService .Storage ().MountStorage ("/mnt/" + username , username + ":" )
179
166
180
167
notify ["status" ] = "success"
181
168
notify ["message" ] = "Success"
182
169
notify ["driver" ] = "Dropbox"
183
- service .MyService .Notify ().SendNotify ("casaos:file:recover" , notify )
170
+ service .MyService .Notify ().SendNotify (event , notify )
184
171
} else if t == "Onedrive" {
185
172
onedrive := onedrive .GetConfig ()
186
173
onedrive .Code = ctx .QueryParam ("code" )
187
174
if len (onedrive .Code ) == 0 {
188
- ctx .String (200 , `<p>Code cannot be empty</p><script>window.close()</script>` )
189
175
notify ["status" ] = "fail"
190
176
notify ["message" ] = "Code cannot be empty"
191
177
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>` )
193
180
}
194
-
195
- err := onedrive .Init (ctx .Request ().Context ())
181
+ err := onedrive .Init (context .Background ())
196
182
if err != nil {
197
- ctx .String (200 , `<p>Initialization failure:` + err .Error ()+ `</p><script>window.close()</script>` )
198
183
notify ["status" ] = "fail"
199
184
notify ["message" ] = "Initialization failure"
200
185
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>` )
202
188
}
203
- username , driveId , driveType , err := onedrive .GetInfo (ctx . Request (). Context ())
189
+ username , driveId , driveType , err := onedrive .GetInfo (context . Background ())
204
190
if err != nil {
205
- ctx .String (200 , `<p>Failed to get user information:` + err .Error ()+ `</p><script>window.close()</script>` )
206
191
notify ["status" ] = "fail"
207
192
notify ["message" ] = "Failed to get user information"
208
193
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>` )
210
196
}
211
197
dmap := make (map [string ]string )
212
198
dmap ["username" ] = username
213
199
214
200
configs , err := service .MyService .Storage ().GetConfig ()
215
201
if err != nil {
216
- ctx .String (200 , `<p>Failed to get rclone config:` + err .Error ()+ `</p><script>window.close()</script>` )
217
202
notify ["status" ] = "fail"
218
203
notify ["message" ] = "Failed to get rclone config"
219
204
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>` )
221
207
}
222
208
for _ , v := range configs .Remotes {
223
209
cf , err := service .MyService .Storage ().GetConfigByName (v )
@@ -226,15 +212,14 @@ func GetRecoverStorage(ctx echo.Context) error {
226
212
continue
227
213
}
228
214
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 {
232
216
logger .Error ("check and mount by name error: " , zap .Error (err ), zap .Any ("name" , cf ["username" ]))
233
217
}
234
218
235
219
notify ["status" ] = "warn"
236
220
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>` )
238
223
}
239
224
}
240
225
if len (username ) > 0 {
@@ -249,26 +234,14 @@ func GetRecoverStorage(ctx echo.Context) error {
249
234
dmap ["mount_point" ] = "/mnt/" + username
250
235
dmap ["drive_id" ] = driveId
251
236
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
- // }
264
237
service .MyService .Storage ().CreateConfig (dmap , username , "onedrive" )
265
238
service .MyService .Storage ().MountStorage ("/mnt/" + username , username + ":" )
266
239
267
240
notify ["status" ] = "success"
268
241
notify ["message" ] = "Success"
269
242
notify ["driver" ] = "Onedrive"
270
- service .MyService .Notify ().SendNotify ("casaos:file:recover" , notify )
243
+ service .MyService .Notify ().SendNotify (event , notify )
271
244
}
272
245
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>` )
274
247
}
0 commit comments