parent
3a0914a4a6
commit
01ff237e03
@ -0,0 +1,42 @@
|
||||
package biz
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"moredoc/middleware/auth"
|
||||
"moredoc/model"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func checkGinPermission(dbModel *model.DBModel, ctx *gin.Context) (userClaims *auth.UserClaims, statusCode int, err error) {
|
||||
var ok bool
|
||||
userClaims, ok = ctx.Value(auth.CtxKeyUserClaims.String()).(*auth.UserClaims)
|
||||
if !ok || dbModel.IsInvalidToken(userClaims.UUID) {
|
||||
statusCode = http.StatusUnauthorized
|
||||
return nil, statusCode, errors.New(ErrorMessageInvalidToken)
|
||||
}
|
||||
|
||||
if yes := dbModel.CheckPermissionByUserId(userClaims.UserId, ctx.Request.URL.Path, ctx.Request.Method); !yes {
|
||||
statusCode = http.StatusForbidden
|
||||
return nil, statusCode, errors.New(ErrorMessagePermissionDenied)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func checkGRPCPermission(dbModel *model.DBModel, ctx context.Context) (userClaims *auth.UserClaims, err error) {
|
||||
var ok bool
|
||||
userClaims, ok = ctx.Value(auth.CtxKeyUserClaims).(*auth.UserClaims)
|
||||
if !ok || dbModel.IsInvalidToken(userClaims.UUID) {
|
||||
return nil, status.Errorf(codes.Unauthenticated, ErrorMessageInvalidToken)
|
||||
}
|
||||
|
||||
fullMethod, _ := ctx.Value(auth.CtxKeyFullMethod).(string)
|
||||
if yes := dbModel.CheckPermissionByUserId(userClaims.UserId, fullMethod); !yes {
|
||||
return nil, status.Errorf(codes.PermissionDenied, ErrorMessagePermissionDenied)
|
||||
}
|
||||
return
|
||||
}
|
@ -1,15 +1,31 @@
|
||||
package serve
|
||||
|
||||
import (
|
||||
"moredoc/biz"
|
||||
"moredoc/middleware/auth"
|
||||
"moredoc/model"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// RegisterGinRouter 注册gin路由
|
||||
func RegisterGinRouter(app *gin.Engine) (err error) {
|
||||
func RegisterGinRouter(app *gin.Engine, dbModel *model.DBModel, logger *zap.Logger, auth *auth.Auth) (err error) {
|
||||
attachmentAPIService := biz.NewAttachmentAPIService(dbModel, logger)
|
||||
|
||||
app.GET("/helloworld", func(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, "hello world")
|
||||
})
|
||||
|
||||
checkPermissionGroup := app.Group("/api/v1/upload")
|
||||
checkPermissionGroup.Use(auth.AuthGin())
|
||||
{
|
||||
checkPermissionGroup.POST("avatar", attachmentAPIService.UploadAvatar)
|
||||
checkPermissionGroup.POST("banner", attachmentAPIService.UploadBanner)
|
||||
checkPermissionGroup.POST("document", attachmentAPIService.UploadDocument)
|
||||
checkPermissionGroup.POST("category/cover", attachmentAPIService.UploadCategoryCover)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -0,0 +1,70 @@
|
||||
package filetil
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"image"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
)
|
||||
|
||||
var imagesExt = map[string]struct{}{
|
||||
".jpg": {},
|
||||
".jpeg": {},
|
||||
".png": {},
|
||||
".gif": {},
|
||||
// ".bmp": {},
|
||||
// ".webp": {},
|
||||
}
|
||||
|
||||
// IsImage 判断文件是否是图片
|
||||
func IsImage(ext string) bool {
|
||||
_, ok := imagesExt[ext]
|
||||
return ok
|
||||
}
|
||||
|
||||
// GetFileMD5 获取文件MD5值
|
||||
func GetFileMD5(filePath string) (string, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
hash := md5.New()
|
||||
_, _ = io.Copy(hash, file)
|
||||
return hex.EncodeToString(hash.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// CropImage 居中裁剪图片
|
||||
func CropImage(file string, width, height int) (err error) {
|
||||
var img image.Image
|
||||
img, err = imaging.Open(file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ext := strings.ToLower(filepath.Ext(file))
|
||||
switch ext {
|
||||
case ".jpeg", ".jpg", ".png", ".gif":
|
||||
img = imaging.Fill(img, width, height, imaging.Center, imaging.CatmullRom)
|
||||
default:
|
||||
err = errors.New("unsupported image format")
|
||||
return
|
||||
}
|
||||
return imaging.Save(img, file)
|
||||
}
|
||||
|
||||
// GetImageSize 获取图片宽高信息
|
||||
func GetImageSize(file string) (width, height int, err error) {
|
||||
var img image.Image
|
||||
img, err = imaging.Open(file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
width = img.Bounds().Max.X
|
||||
height = img.Bounds().Max.Y
|
||||
return
|
||||
}
|
Loading…
Reference in new issue