|
package common |
|
|
|
import ( |
|
"context" |
|
"net/http" |
|
"strings" |
|
|
|
"github.com/alist-org/alist/v3/cmd/flags" |
|
"github.com/alist-org/alist/v3/internal/conf" |
|
"github.com/gin-gonic/gin" |
|
log "github.com/sirupsen/logrus" |
|
) |
|
|
|
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 |
|
} |
|
|
|
|
|
|
|
func ErrorResp(c *gin.Context, err error, code int, l ...bool) { |
|
ErrorWithDataResp(c, err, code, nil, l...) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
func ErrorWithDataResp(c *gin.Context, err error, code int, data interface{}, l ...bool) { |
|
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: data, |
|
}) |
|
c.Abort() |
|
} |
|
|
|
func ErrorStrResp(c *gin.Context, str string, code int, l ...bool) { |
|
if len(l) != 0 && l[0] { |
|
log.Error(str) |
|
} |
|
c.JSON(200, Resp[interface{}]{ |
|
Code: code, |
|
Message: hidePrivacy(str), |
|
Data: nil, |
|
}) |
|
c.Abort() |
|
} |
|
|
|
func SuccessResp(c *gin.Context, data ...interface{}) { |
|
if len(data) == 0 { |
|
c.JSON(200, Resp[interface{}]{ |
|
Code: 200, |
|
Message: "success", |
|
Data: nil, |
|
}) |
|
return |
|
} |
|
c.JSON(200, Resp[interface{}]{ |
|
Code: 200, |
|
Message: "success", |
|
Data: data[0], |
|
}) |
|
} |
|
|
|
func GetHttpReq(ctx context.Context) *http.Request { |
|
if c, ok := ctx.(*gin.Context); ok { |
|
return c.Request |
|
} |
|
return nil |
|
} |
|
|