initial commit

This commit is contained in:
2025-05-22 19:39:08 +08:00
commit 531bb42d01
103 changed files with 10291 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package config
import "github.com/zeromicro/go-zero/zrpc"
type Minio struct {
Endpoint string
Id string
Secret string
Token string
Secure bool
}
type Config struct {
zrpc.RpcServerConf
Minio Minio
}

View File

@ -0,0 +1,51 @@
package logic
import (
"context"
"strings"
"godemo/file/file"
"godemo/file/internal/svc"
"github.com/minio/minio-go/v7"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type DeleteLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteLogic {
return &DeleteLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// 删除文件
func (l *DeleteLogic) Delete(in *file.DeleteRequest) (*file.DeleteResponse, error) {
// 假设 fileId 格式为: "bucketName/objectName"
parts := strings.SplitN(in.FileId, "/", 2)
if len(parts) != 2 {
return nil, status.Error(codes.InvalidArgument, "invalid fileId format")
}
bucketName := parts[0]
objectName := parts[1]
// 删除对象
err := l.svcCtx.MinioClient.RemoveObject(l.ctx, bucketName, objectName, minio.RemoveObjectOptions{})
if err != nil {
// return nil, err
return nil, status.Error(codes.Internal, err.Error())
}
return &file.DeleteResponse{
Success: true,
}, nil
}

View File

@ -0,0 +1,57 @@
package logic
import (
"context"
"net/url"
"strings"
"time"
"godemo/file/file"
"godemo/file/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type GetFileUrlLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetFileUrlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFileUrlLogic {
return &GetFileUrlLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// 获取文件访问链接(带签名,防盗链)
func (l *GetFileUrlLogic) GetFileUrl(in *file.GetFileUrlRequest) (*file.GetFileUrlResponse, error) {
// 假设 fileId 格式为: "bucketName/objectName"
parts := strings.SplitN(in.FileId, "/", 2)
if len(parts) != 2 {
return nil, status.Error(codes.InvalidArgument, "invalid fileId format")
}
bucketName := parts[0]
objectName := parts[1]
// 生成预签名 URL有效期 1 小时
presignedUrl, err := l.svcCtx.MinioClient.PresignedGetObject(
l.ctx,
bucketName,
objectName,
time.Hour,
url.Values{},
)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &file.GetFileUrlResponse{
Url: presignedUrl.String(),
}, nil
}

View File

@ -0,0 +1,88 @@
package logic
import (
"bytes"
"context"
"net/url"
"path/filepath"
"time"
"godemo/file/file"
"godemo/file/internal/svc"
"github.com/google/uuid"
"github.com/minio/minio-go/v7"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type UploadLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogic {
return &UploadLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// 上传文件(图片/头像/壁纸等)
func (l *UploadLogic) Upload(in *file.UploadRequest) (*file.UploadResponse, error) {
bucketName := "default"
if in.Folder != "" {
bucketName = in.Folder
}
// 确保 bucket 存在(自动创建)
exists, err := l.svcCtx.MinioClient.BucketExists(l.ctx, bucketName)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if !exists {
err := l.svcCtx.MinioClient.MakeBucket(l.ctx, bucketName, minio.MakeBucketOptions{})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
}
// 生成唯一文件名
ext := getFileExtension(in.Filename)
newFileName := uuid.New().String() + ext
// 上传对象
_, err = l.svcCtx.MinioClient.PutObject(l.ctx, bucketName, newFileName,
bytes.NewReader(in.Content),
int64(len(in.Content)),
minio.PutObjectOptions{
ContentType: in.ContentType,
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
// 生成预签名 URL有效期1小时
reqParams := make(url.Values)
presignedUrl, err := l.svcCtx.MinioClient.PresignedGetObject(l.ctx, bucketName, newFileName, time.Hour, reqParams)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &file.UploadResponse{
FileId: newFileName,
Url: presignedUrl.String(),
}, nil
}
// 获取文件扩展名(默认 .jpg
func getFileExtension(name string) string {
ext := filepath.Ext(name)
if ext == "" {
return ".jpg"
}
return ext
}

View File

@ -0,0 +1,42 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.8.3
// Source: file.proto
package server
import (
"context"
"godemo/file/file"
"godemo/file/internal/logic"
"godemo/file/internal/svc"
)
type FileServer struct {
svcCtx *svc.ServiceContext
file.UnimplementedFileServer
}
func NewFileServer(svcCtx *svc.ServiceContext) *FileServer {
return &FileServer{
svcCtx: svcCtx,
}
}
// 上传文件(图片/头像/壁纸等)
func (s *FileServer) Upload(ctx context.Context, in *file.UploadRequest) (*file.UploadResponse, error) {
l := logic.NewUploadLogic(ctx, s.svcCtx)
return l.Upload(in)
}
// 获取文件访问链接(带签名,防盗链)
func (s *FileServer) GetFileUrl(ctx context.Context, in *file.GetFileUrlRequest) (*file.GetFileUrlResponse, error) {
l := logic.NewGetFileUrlLogic(ctx, s.svcCtx)
return l.GetFileUrl(in)
}
// 删除文件
func (s *FileServer) Delete(ctx context.Context, in *file.DeleteRequest) (*file.DeleteResponse, error) {
l := logic.NewDeleteLogic(ctx, s.svcCtx)
return l.Delete(in)
}

View File

@ -0,0 +1,29 @@
package svc
import (
"godemo/file/internal/config"
"log"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
type ServiceContext struct {
Config config.Config
MinioClient *minio.Client
}
func NewServiceContext(c config.Config) *ServiceContext {
// 初始化 MinIO 客户端
minioClient, err := minio.New(c.Minio.Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(c.Minio.Id, c.Minio.Secret, c.Minio.Token),
Secure: c.Minio.Secure, // 本地部署不用 https
})
if err != nil {
log.Fatalf("failed to initialize minio client: %v", err)
}
return &ServiceContext{
Config: c,
MinioClient: minioClient,
}
}