2022-06-25 13:34:44 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2024-12-25 13:13:54 +00:00
|
|
|
"context"
|
|
|
|
"net/http"
|
2022-08-08 04:52:54 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-08-07 05:09:59 +00:00
|
|
|
"github.com/alist-org/alist/v3/cmd/flags"
|
2022-08-08 04:52:54 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/conf"
|
2022-06-25 13:34:44 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-08-08 04:52:54 +00:00
|
|
|
func hidePrivacy(msg string) string {
|
|
|
|
for _, r := range conf.PrivacyReg {
|
|
|
|
msg = r.ReplaceAllStringFunc(msg, func(s string) string {
|
|
|
|
return strings.Repeat("*", len(s))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2022-06-26 11:20:19 +00:00
|
|
|
// ErrorResp is used to return error response
|
2022-06-28 10:12:53 +00:00
|
|
|
// @param l: if true, log error
|
|
|
|
func ErrorResp(c *gin.Context, err error, code int, l ...bool) {
|
2022-11-13 12:17:10 +00:00
|
|
|
ErrorWithDataResp(c, err, code, nil, l...)
|
|
|
|
//if len(l) > 0 && l[0] {
|
|
|
|
// if flags.Debug || flags.Dev {
|
|
|
|
// log.Errorf("%+v", err)
|
|
|
|
// } else {
|
|
|
|
// log.Errorf("%v", err)
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//c.JSON(200, Resp[interface{}]{
|
|
|
|
// Code: code,
|
|
|
|
// Message: hidePrivacy(err.Error()),
|
|
|
|
// Data: nil,
|
|
|
|
//})
|
|
|
|
//c.Abort()
|
|
|
|
}
|
|
|
|
|
|
|
|
func ErrorWithDataResp(c *gin.Context, err error, code int, data interface{}, l ...bool) {
|
2022-06-28 10:12:53 +00:00
|
|
|
if len(l) > 0 && l[0] {
|
2022-08-07 05:09:59 +00:00
|
|
|
if flags.Debug || flags.Dev {
|
2022-06-28 10:12:53 +00:00
|
|
|
log.Errorf("%+v", err)
|
|
|
|
} else {
|
|
|
|
log.Errorf("%v", err)
|
|
|
|
}
|
2022-06-25 14:05:02 +00:00
|
|
|
}
|
2022-10-27 02:54:49 +00:00
|
|
|
c.JSON(200, Resp[interface{}]{
|
2022-06-25 13:34:44 +00:00
|
|
|
Code: code,
|
2022-08-08 04:52:54 +00:00
|
|
|
Message: hidePrivacy(err.Error()),
|
2022-11-13 12:17:10 +00:00
|
|
|
Data: data,
|
2022-06-25 13:34:44 +00:00
|
|
|
})
|
|
|
|
c.Abort()
|
|
|
|
}
|
|
|
|
|
2022-06-26 11:20:19 +00:00
|
|
|
func ErrorStrResp(c *gin.Context, str string, code int, l ...bool) {
|
|
|
|
if len(l) != 0 && l[0] {
|
|
|
|
log.Error(str)
|
|
|
|
}
|
2022-10-27 02:54:49 +00:00
|
|
|
c.JSON(200, Resp[interface{}]{
|
2022-06-25 13:34:44 +00:00
|
|
|
Code: code,
|
2022-08-08 04:52:54 +00:00
|
|
|
Message: hidePrivacy(str),
|
2022-06-25 13:34:44 +00:00
|
|
|
Data: nil,
|
|
|
|
})
|
|
|
|
c.Abort()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SuccessResp(c *gin.Context, data ...interface{}) {
|
|
|
|
if len(data) == 0 {
|
2022-10-27 02:54:49 +00:00
|
|
|
c.JSON(200, Resp[interface{}]{
|
2022-06-25 13:34:44 +00:00
|
|
|
Code: 200,
|
|
|
|
Message: "success",
|
|
|
|
Data: nil,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-10-27 02:54:49 +00:00
|
|
|
c.JSON(200, Resp[interface{}]{
|
2022-06-25 13:34:44 +00:00
|
|
|
Code: 200,
|
|
|
|
Message: "success",
|
|
|
|
Data: data[0],
|
|
|
|
})
|
|
|
|
}
|
2024-12-25 13:13:54 +00:00
|
|
|
|
|
|
|
func GetHttpReq(ctx context.Context) *http.Request {
|
|
|
|
if c, ok := ctx.(*gin.Context); ok {
|
|
|
|
return c.Request
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|