chore: initial commit before Kratos migration

This commit is contained in:
向宁 2026-05-25 12:29:31 +08:00
parent 3a18ca0579
commit 654b7d9bb6
4 changed files with 2930 additions and 14 deletions

File diff suppressed because it is too large Load Diff

View File

@ -128,6 +128,8 @@ func (e *FolderEndpoint) UploadToFolder(c *gin.Context) {
Data: file, Data: file,
S3Bucket: s3Bucket, S3Bucket: s3Bucket,
OwnerID: ownerID, OwnerID: ownerID,
Size: header.Size,
ContentType: header.Header.Get("Content-Type"),
} }
result, err := mediator.Send[handlers.UploadToFolderCommand, *model.FileMeta](e.Mediator, c.Request.Context(), cmd) result, err := mediator.Send[handlers.UploadToFolderCommand, *model.FileMeta](e.Mediator, c.Request.Context(), cmd)

View File

@ -19,6 +19,8 @@ type UploadToFolderCommand struct {
Data io.Reader Data io.Reader
S3Bucket string S3Bucket string
OwnerID string OwnerID string
Size int64
ContentType string
} }
type MoveFileCommand struct { type MoveFileCommand struct {
@ -60,6 +62,11 @@ func (h *UploadToFolderHandler) Handle(ctx context.Context, cmd UploadToFolderCo
return nil, fmt.Errorf("S3 upload failed: %w", err) return nil, fmt.Errorf("S3 upload failed: %w", err)
} }
contentType := cmd.ContentType
if contentType == "" {
contentType = "application/octet-stream"
}
now := time.Now() now := time.Now()
fileMeta := &model.FileMeta{ fileMeta := &model.FileMeta{
ID: uuid.New().String(), ID: uuid.New().String(),
@ -67,7 +74,8 @@ func (h *UploadToFolderHandler) Handle(ctx context.Context, cmd UploadToFolderCo
Name: cmd.FileName, Name: cmd.FileName,
S3Key: s3Key, S3Key: s3Key,
S3Bucket: cmd.S3Bucket, S3Bucket: cmd.S3Bucket,
ContentType: "application/octet-stream", Size: cmd.Size,
ContentType: contentType,
OwnerID: cmd.OwnerID, OwnerID: cmd.OwnerID,
CreatedAt: now, CreatedAt: now,
UpdatedAt: now, UpdatedAt: now,

View File

@ -7,14 +7,13 @@ import (
) )
const API_KEY_HEADER = "X-API-Key" const API_KEY_HEADER = "X-API-Key"
const APIKeyUserID = "api-key-user"
// AuthMiddleware 验证API密钥的中间件 // AuthMiddleware 验证API密钥的中间件
func AuthMiddleware(apiKey string) gin.HandlerFunc { func AuthMiddleware(apiKey string) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
// 从请求头中获取API密钥
key := c.GetHeader(API_KEY_HEADER) key := c.GetHeader(API_KEY_HEADER)
// 验证密钥是否正确
if key != apiKey { if key != apiKey {
c.JSON(http.StatusUnauthorized, gin.H{ c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized, "code": http.StatusUnauthorized,
@ -25,7 +24,8 @@ func AuthMiddleware(apiKey string) gin.HandlerFunc {
return return
} }
// 密钥验证通过,继续处理请求 c.Set(ContextKeyUserID, APIKeyUserID)
c.Set(ContextKeyUsername, "api-key-user")
c.Next() c.Next()
} }
} }