initial commit
This commit is contained in:
51
file/internal/logic/deletelogic.go
Normal file
51
file/internal/logic/deletelogic.go
Normal 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
|
||||
}
|
||||
57
file/internal/logic/getfileurllogic.go
Normal file
57
file/internal/logic/getfileurllogic.go
Normal 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
|
||||
}
|
||||
88
file/internal/logic/uploadlogic.go
Normal file
88
file/internal/logic/uploadlogic.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user