Skip to content

Commit 048ee9c

Browse files
authored
feat(server): adapting #1099 to #991 (#1102)
1 parent 2339454 commit 048ee9c

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

server/handles/archive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func ArchiveProxy(c *gin.Context) {
375375
}
376376
proxy(c, link, file, storage.GetStorage().ProxyRange)
377377
} else {
378-
common.ErrorStrResp(c, "proxy not allowed", 403)
378+
common.ErrorPage(c, errors.New("proxy not allowed"), 403)
379379
return
380380
}
381381
}

server/handles/down.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func Proxy(c *gin.Context) {
7272
}
7373
proxy(c, link, file, storage.GetStorage().ProxyRange)
7474
} else {
75-
common.ErrorStrResp(c, "proxy not allowed", 403)
75+
common.ErrorPage(c, errors.New("proxy not allowed"), 403)
7676
return
7777
}
7878
}

server/handles/sharing.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,16 @@ func SharingDown(c *gin.Context) {
206206
err = errors.New("cannot get sharing root link")
207207
}
208208
}
209-
if dealError(c, err) {
209+
if dealErrorPage(c, err) {
210210
return
211211
}
212212
unwrapPath, err := op.GetSharingUnwrapPath(s, path)
213213
if err != nil {
214-
common.ErrorStrResp(c, "failed get sharing unwrap path", 500)
214+
common.ErrorPage(c, errors.New("failed get sharing unwrap path"), 500)
215215
return
216216
}
217217
storage, actualPath, err := op.GetStorageAndActualPath(unwrapPath)
218-
if dealError(c, err) {
218+
if dealErrorPage(c, err) {
219219
return
220220
}
221221
if setting.GetBool(conf.ShareForceProxy) || common.ShouldProxy(storage, stdpath.Base(actualPath)) {
@@ -224,7 +224,7 @@ func SharingDown(c *gin.Context) {
224224
Type: c.Query("type"),
225225
})
226226
if err != nil {
227-
common.ErrorResp(c, errors.WithMessage(err, "failed get sharing link"), 500)
227+
common.ErrorPage(c, errors.WithMessage(err, "failed get sharing link"), 500)
228228
return
229229
}
230230
_ = countAccess(c.ClientIP(), s)
@@ -237,7 +237,7 @@ func SharingDown(c *gin.Context) {
237237
Redirect: true,
238238
})
239239
if err != nil {
240-
common.ErrorResp(c, errors.WithMessage(err, "failed get sharing link"), 500)
240+
common.ErrorPage(c, errors.WithMessage(err, "failed get sharing link"), 500)
241241
return
242242
}
243243
_ = countAccess(c.ClientIP(), s)
@@ -247,7 +247,7 @@ func SharingDown(c *gin.Context) {
247247

248248
func SharingArchiveExtract(c *gin.Context) {
249249
if !setting.GetBool(conf.ShareArchivePreview) {
250-
common.ErrorStrResp(c, "sharing archives previewing is not allowed", 403)
250+
common.ErrorPage(c, errors.New("sharing archives previewing is not allowed"), 403)
251251
return
252252
}
253253
sid := c.Request.Context().Value(conf.SharingIDKey).(string)
@@ -265,16 +265,16 @@ func SharingArchiveExtract(c *gin.Context) {
265265
err = errors.New("cannot extract sharing root")
266266
}
267267
}
268-
if dealError(c, err) {
268+
if dealErrorPage(c, err) {
269269
return
270270
}
271271
unwrapPath, err := op.GetSharingUnwrapPath(s, path)
272272
if err != nil {
273-
common.ErrorStrResp(c, "failed get sharing unwrap path", 500)
273+
common.ErrorPage(c, errors.New("failed get sharing unwrap path"), 500)
274274
return
275275
}
276276
storage, actualPath, err := op.GetStorageAndActualPath(unwrapPath)
277-
if dealError(c, err) {
277+
if dealErrorPage(c, err) {
278278
return
279279
}
280280
args := model.ArchiveInnerArgs{
@@ -290,21 +290,21 @@ func SharingArchiveExtract(c *gin.Context) {
290290
if _, ok := storage.(driver.ArchiveReader); ok {
291291
if setting.GetBool(conf.ShareForceProxy) || common.ShouldProxy(storage, stdpath.Base(actualPath)) {
292292
link, obj, err := op.DriverExtract(c.Request.Context(), storage, actualPath, args)
293-
if dealError(c, err) {
293+
if dealErrorPage(c, err) {
294294
return
295295
}
296296
proxy(c, link, obj, storage.GetStorage().ProxyRange)
297297
} else {
298298
args.Redirect = true
299299
link, _, err := op.DriverExtract(c.Request.Context(), storage, actualPath, args)
300-
if dealError(c, err) {
300+
if dealErrorPage(c, err) {
301301
return
302302
}
303303
redirect(c, link)
304304
}
305305
} else {
306306
rc, size, err := op.InternalExtract(c.Request.Context(), storage, actualPath, args)
307-
if dealError(c, err) {
307+
if dealErrorPage(c, err) {
308308
return
309309
}
310310
fileName := stdpath.Base(innerPath)
@@ -329,6 +329,23 @@ func dealError(c *gin.Context, err error) bool {
329329
return true
330330
}
331331

332+
func dealErrorPage(c *gin.Context, err error) bool {
333+
if err == nil {
334+
return false
335+
} else if errors.Is(err, errs.SharingNotFound) {
336+
common.ErrorPage(c, errors.New("the share does not exist"), 500)
337+
} else if errors.Is(err, errs.InvalidSharing) {
338+
common.ErrorPage(c, errors.New("the share has expired or is no longer valid"), 500)
339+
} else if errors.Is(err, errs.WrongShareCode) {
340+
common.ErrorPage(c, err, 403)
341+
} else if errors.Is(err, errs.WrongArchivePassword) {
342+
common.ErrorPage(c, err, 202)
343+
} else {
344+
common.ErrorPage(c, err, 500)
345+
}
346+
return true
347+
}
348+
332349
type SharingResp struct {
333350
*model.Sharing
334351
CreatorName string `json:"creator"`

0 commit comments

Comments
 (0)