feat(server): adapting #1099 to #991 (#1102)

This commit is contained in:
KirCute
2025-08-19 15:48:59 +08:00
committed by GitHub
parent 23394548ca
commit 048ee9c2e5
3 changed files with 31 additions and 14 deletions

View File

@ -375,7 +375,7 @@ func ArchiveProxy(c *gin.Context) {
} }
proxy(c, link, file, storage.GetStorage().ProxyRange) proxy(c, link, file, storage.GetStorage().ProxyRange)
} else { } else {
common.ErrorStrResp(c, "proxy not allowed", 403) common.ErrorPage(c, errors.New("proxy not allowed"), 403)
return return
} }
} }

View File

@ -72,7 +72,7 @@ func Proxy(c *gin.Context) {
} }
proxy(c, link, file, storage.GetStorage().ProxyRange) proxy(c, link, file, storage.GetStorage().ProxyRange)
} else { } else {
common.ErrorStrResp(c, "proxy not allowed", 403) common.ErrorPage(c, errors.New("proxy not allowed"), 403)
return return
} }
} }

View File

@ -206,16 +206,16 @@ func SharingDown(c *gin.Context) {
err = errors.New("cannot get sharing root link") err = errors.New("cannot get sharing root link")
} }
} }
if dealError(c, err) { if dealErrorPage(c, err) {
return return
} }
unwrapPath, err := op.GetSharingUnwrapPath(s, path) unwrapPath, err := op.GetSharingUnwrapPath(s, path)
if err != nil { if err != nil {
common.ErrorStrResp(c, "failed get sharing unwrap path", 500) common.ErrorPage(c, errors.New("failed get sharing unwrap path"), 500)
return return
} }
storage, actualPath, err := op.GetStorageAndActualPath(unwrapPath) storage, actualPath, err := op.GetStorageAndActualPath(unwrapPath)
if dealError(c, err) { if dealErrorPage(c, err) {
return return
} }
if setting.GetBool(conf.ShareForceProxy) || common.ShouldProxy(storage, stdpath.Base(actualPath)) { if setting.GetBool(conf.ShareForceProxy) || common.ShouldProxy(storage, stdpath.Base(actualPath)) {
@ -224,7 +224,7 @@ func SharingDown(c *gin.Context) {
Type: c.Query("type"), Type: c.Query("type"),
}) })
if err != nil { if err != nil {
common.ErrorResp(c, errors.WithMessage(err, "failed get sharing link"), 500) common.ErrorPage(c, errors.WithMessage(err, "failed get sharing link"), 500)
return return
} }
_ = countAccess(c.ClientIP(), s) _ = countAccess(c.ClientIP(), s)
@ -237,7 +237,7 @@ func SharingDown(c *gin.Context) {
Redirect: true, Redirect: true,
}) })
if err != nil { if err != nil {
common.ErrorResp(c, errors.WithMessage(err, "failed get sharing link"), 500) common.ErrorPage(c, errors.WithMessage(err, "failed get sharing link"), 500)
return return
} }
_ = countAccess(c.ClientIP(), s) _ = countAccess(c.ClientIP(), s)
@ -247,7 +247,7 @@ func SharingDown(c *gin.Context) {
func SharingArchiveExtract(c *gin.Context) { func SharingArchiveExtract(c *gin.Context) {
if !setting.GetBool(conf.ShareArchivePreview) { if !setting.GetBool(conf.ShareArchivePreview) {
common.ErrorStrResp(c, "sharing archives previewing is not allowed", 403) common.ErrorPage(c, errors.New("sharing archives previewing is not allowed"), 403)
return return
} }
sid := c.Request.Context().Value(conf.SharingIDKey).(string) sid := c.Request.Context().Value(conf.SharingIDKey).(string)
@ -265,16 +265,16 @@ func SharingArchiveExtract(c *gin.Context) {
err = errors.New("cannot extract sharing root") err = errors.New("cannot extract sharing root")
} }
} }
if dealError(c, err) { if dealErrorPage(c, err) {
return return
} }
unwrapPath, err := op.GetSharingUnwrapPath(s, path) unwrapPath, err := op.GetSharingUnwrapPath(s, path)
if err != nil { if err != nil {
common.ErrorStrResp(c, "failed get sharing unwrap path", 500) common.ErrorPage(c, errors.New("failed get sharing unwrap path"), 500)
return return
} }
storage, actualPath, err := op.GetStorageAndActualPath(unwrapPath) storage, actualPath, err := op.GetStorageAndActualPath(unwrapPath)
if dealError(c, err) { if dealErrorPage(c, err) {
return return
} }
args := model.ArchiveInnerArgs{ args := model.ArchiveInnerArgs{
@ -290,21 +290,21 @@ func SharingArchiveExtract(c *gin.Context) {
if _, ok := storage.(driver.ArchiveReader); ok { if _, ok := storage.(driver.ArchiveReader); ok {
if setting.GetBool(conf.ShareForceProxy) || common.ShouldProxy(storage, stdpath.Base(actualPath)) { if setting.GetBool(conf.ShareForceProxy) || common.ShouldProxy(storage, stdpath.Base(actualPath)) {
link, obj, err := op.DriverExtract(c.Request.Context(), storage, actualPath, args) link, obj, err := op.DriverExtract(c.Request.Context(), storage, actualPath, args)
if dealError(c, err) { if dealErrorPage(c, err) {
return return
} }
proxy(c, link, obj, storage.GetStorage().ProxyRange) proxy(c, link, obj, storage.GetStorage().ProxyRange)
} else { } else {
args.Redirect = true args.Redirect = true
link, _, err := op.DriverExtract(c.Request.Context(), storage, actualPath, args) link, _, err := op.DriverExtract(c.Request.Context(), storage, actualPath, args)
if dealError(c, err) { if dealErrorPage(c, err) {
return return
} }
redirect(c, link) redirect(c, link)
} }
} else { } else {
rc, size, err := op.InternalExtract(c.Request.Context(), storage, actualPath, args) rc, size, err := op.InternalExtract(c.Request.Context(), storage, actualPath, args)
if dealError(c, err) { if dealErrorPage(c, err) {
return return
} }
fileName := stdpath.Base(innerPath) fileName := stdpath.Base(innerPath)
@ -329,6 +329,23 @@ func dealError(c *gin.Context, err error) bool {
return true return true
} }
func dealErrorPage(c *gin.Context, err error) bool {
if err == nil {
return false
} else if errors.Is(err, errs.SharingNotFound) {
common.ErrorPage(c, errors.New("the share does not exist"), 500)
} else if errors.Is(err, errs.InvalidSharing) {
common.ErrorPage(c, errors.New("the share has expired or is no longer valid"), 500)
} else if errors.Is(err, errs.WrongShareCode) {
common.ErrorPage(c, err, 403)
} else if errors.Is(err, errs.WrongArchivePassword) {
common.ErrorPage(c, err, 202)
} else {
common.ErrorPage(c, err, 500)
}
return true
}
type SharingResp struct { type SharingResp struct {
*model.Sharing *model.Sharing
CreatorName string `json:"creator"` CreatorName string `json:"creator"`