feat: rbac完善,file接入完成
This commit is contained in:
@@ -4,6 +4,8 @@ Log:
|
||||
Encoding: plain
|
||||
Host: 0.0.0.0
|
||||
Port: 9002
|
||||
Timeout: 300000
|
||||
MaxBytes: 1048576000
|
||||
|
||||
Auth:
|
||||
AccessSecret: 9149f2eb-d517-4a50-a03a-231dbcf0d872
|
||||
|
||||
+83
-1
@@ -28,10 +28,67 @@ type (
|
||||
PageSize int `json:"pageSize,optional"`
|
||||
Name string `json:"name,optional"`
|
||||
}
|
||||
FileListResp {
|
||||
List []FileInfo `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
// 文件ID请求
|
||||
FileIdReq {
|
||||
Id string `path:"id"`
|
||||
}
|
||||
// ---------- 存储配置 ----------
|
||||
StorageConfigInfo {
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
AccessKeyId string `json:"accessKeyId"`
|
||||
AccessKeySecret string `json:"accessKeySecret"`
|
||||
BucketName string `json:"bucketName"`
|
||||
BucketUrl string `json:"bucketUrl"`
|
||||
Region string `json:"region"`
|
||||
IsDefault int `json:"isDefault"`
|
||||
Status int `json:"status"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
CreateStorageConfigReq {
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
AccessKeyId string `json:"accessKeyId"`
|
||||
AccessKeySecret string `json:"accessKeySecret"`
|
||||
BucketName string `json:"bucketName"`
|
||||
BucketUrl string `json:"bucketUrl"`
|
||||
Region string `json:"region,optional"`
|
||||
Status int `json:"status,optional"`
|
||||
Remark string `json:"remark,optional"`
|
||||
}
|
||||
UpdateStorageConfigReq {
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
AccessKeyId string `json:"accessKeyId"`
|
||||
AccessKeySecret string `json:"accessKeySecret"`
|
||||
BucketName string `json:"bucketName"`
|
||||
BucketUrl string `json:"bucketUrl"`
|
||||
Region string `json:"region,optional"`
|
||||
Status int `json:"status,optional"`
|
||||
Remark string `json:"remark,optional"`
|
||||
}
|
||||
SetDefaultStorageConfigReq {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
StorageConfigListReq {
|
||||
Current int `json:"current,optional"`
|
||||
PageSize int `json:"pageSize,optional"`
|
||||
Type string `json:"type,optional"`
|
||||
Name string `json:"name,optional"`
|
||||
}
|
||||
StorageConfigListResp {
|
||||
List []StorageConfigInfo `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
)
|
||||
|
||||
// ========== 需要鉴权的接口 ==========
|
||||
@@ -51,10 +108,35 @@ service file-api {
|
||||
|
||||
@doc "文件列表"
|
||||
@handler GetFileList
|
||||
post /list (FileListReq)
|
||||
post /list (FileListReq) returns (FileListResp)
|
||||
|
||||
@doc "获取文件信息"
|
||||
@handler GetFileById
|
||||
get /:id (FileIdReq) returns (FileInfo)
|
||||
|
||||
@doc "下载文件"
|
||||
@handler DownloadFile
|
||||
get /download/:id (FileIdReq)
|
||||
|
||||
// ---------- 存储配置管理 ----------
|
||||
@doc "创建存储配置"
|
||||
@handler CreateStorageConfig
|
||||
post /config/create (CreateStorageConfigReq)
|
||||
|
||||
@doc "更新存储配置"
|
||||
@handler UpdateStorageConfig
|
||||
post /config/update (UpdateStorageConfigReq)
|
||||
|
||||
@doc "删除存储配置"
|
||||
@handler DeleteStorageConfig
|
||||
post /config/delete (IdsReq)
|
||||
|
||||
@doc "设置默认存储配置"
|
||||
@handler SetDefaultStorageConfig
|
||||
post /config/setDefault (SetDefaultStorageConfigReq)
|
||||
|
||||
@doc "获取存储配置列表"
|
||||
@handler GetStorageConfigList
|
||||
post /config/list (StorageConfigListReq) returns (StorageConfigListResp)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/file/api/internal/logic/file"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 创建存储配置
|
||||
func CreateStorageConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.CreateStorageConfigReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := file.NewCreateStorageConfigLogic(r.Context(), svcCtx)
|
||||
err := l.CreateStorageConfig(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.OkWithData(w, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/file/api/internal/logic/file"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 删除存储配置
|
||||
func DeleteStorageConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.IdsReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := file.NewDeleteStorageConfigLogic(r.Context(), svcCtx)
|
||||
err := l.DeleteStorageConfig(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.OkWithData(w, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/file/api/internal/logic/file"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
)
|
||||
|
||||
// 下载文件
|
||||
func DownloadFileHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.FileIdReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := file.NewDownloadFileLogic(r.Context(), svcCtx)
|
||||
err := l.DownloadFile(w, &req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,11 +23,11 @@ func GetFileListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
|
||||
l := file.NewGetFileListLogic(r.Context(), svcCtx)
|
||||
err := l.GetFileList(&req)
|
||||
resp, err := l.GetFileList(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.Ok(w)
|
||||
response.OkWithData(w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/file/api/internal/logic/file"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 获取存储配置列表
|
||||
func GetStorageConfigListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.StorageConfigListReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := file.NewGetStorageConfigListLogic(r.Context(), svcCtx)
|
||||
resp, err := l.GetStorageConfigList(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.OkWithData(w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/file/api/internal/logic/file"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 设置默认存储配置
|
||||
func SetDefaultStorageConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.SetDefaultStorageConfigReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := file.NewSetDefaultStorageConfigLogic(r.Context(), svcCtx)
|
||||
err := l.SetDefaultStorageConfig(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.OkWithData(w, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"sundynix-micro-go/app/file/api/internal/logic/file"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
"sundynix-micro-go/common/response"
|
||||
)
|
||||
|
||||
// 更新存储配置
|
||||
func UpdateStorageConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.UpdateStorageConfigReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := file.NewUpdateStorageConfigLogic(r.Context(), svcCtx)
|
||||
err := l.UpdateStorageConfig(&req)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
response.OkWithData(w, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
func UploadFileHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
l := file.NewUploadFileLogic(r.Context(), svcCtx)
|
||||
resp, err := l.UploadFile()
|
||||
resp, err := l.UploadFile(r)
|
||||
if err != nil {
|
||||
response.Fail(w, err.Error())
|
||||
} else {
|
||||
|
||||
@@ -21,12 +21,48 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/:id",
|
||||
Handler: file.GetFileByIdHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 创建存储配置
|
||||
Method: http.MethodPost,
|
||||
Path: "/config/create",
|
||||
Handler: file.CreateStorageConfigHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 删除存储配置
|
||||
Method: http.MethodPost,
|
||||
Path: "/config/delete",
|
||||
Handler: file.DeleteStorageConfigHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 获取存储配置列表
|
||||
Method: http.MethodPost,
|
||||
Path: "/config/list",
|
||||
Handler: file.GetStorageConfigListHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 设置默认存储配置
|
||||
Method: http.MethodPost,
|
||||
Path: "/config/setDefault",
|
||||
Handler: file.SetDefaultStorageConfigHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 更新存储配置
|
||||
Method: http.MethodPost,
|
||||
Path: "/config/update",
|
||||
Handler: file.UpdateStorageConfigHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 删除文件
|
||||
Method: http.MethodDelete,
|
||||
Method: http.MethodPost,
|
||||
Path: "/delete",
|
||||
Handler: file.DeleteFileHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 下载文件
|
||||
Method: http.MethodGet,
|
||||
Path: "/download/:id",
|
||||
Handler: file.DownloadFileHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 文件列表
|
||||
Method: http.MethodPost,
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
"sundynix-micro-go/app/file/rpc/fileservice"
|
||||
)
|
||||
|
||||
type CreateStorageConfigLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 创建存储配置
|
||||
func NewCreateStorageConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateStorageConfigLogic {
|
||||
return &CreateStorageConfigLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CreateStorageConfigLogic) CreateStorageConfig(req *types.CreateStorageConfigReq) error {
|
||||
_, err := l.svcCtx.FileRpc.CreateStorageConfig(l.ctx, &fileservice.CreateStorageConfigReq{
|
||||
Type: req.Type,
|
||||
Name: req.Name,
|
||||
Endpoint: req.Endpoint,
|
||||
AccessKeyId: req.AccessKeyId,
|
||||
AccessKeySecret: req.AccessKeySecret,
|
||||
BucketName: req.BucketName,
|
||||
BucketUrl: req.BucketUrl,
|
||||
Region: req.Region,
|
||||
Status: int32(req.Status),
|
||||
Remark: req.Remark,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -6,10 +6,11 @@ package file
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/api/internal/oss_core"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/rpc/fileservice"
|
||||
)
|
||||
|
||||
type DeleteFileLogic struct {
|
||||
@@ -28,7 +29,33 @@ func NewDeleteFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Delete
|
||||
}
|
||||
|
||||
func (l *DeleteFileLogic) DeleteFile(req *types.IdsReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
if len(req.Ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
// 1. 查询文件记录获取OSS Key
|
||||
resp, err := l.svcCtx.FileRpc.GetFilesByIds(l.ctx, &fileservice.GetFilesByIdsReq{
|
||||
Ids: req.Ids,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 2. 从OSS物理删除
|
||||
factory := oss_core.NewOSSFactory(l.svcCtx.FileRpc)
|
||||
uploader, err := factory.GetActiveUploader(l.ctx)
|
||||
if err == nil && uploader != nil {
|
||||
for _, file := range resp.Files {
|
||||
if file.Key != "" {
|
||||
_ = uploader.DeleteFile(l.ctx, file.Key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 从数据库删除
|
||||
_, err = l.svcCtx.FileRpc.DeleteFiles(l.ctx, &fileservice.DeleteFilesReq{
|
||||
Ids: req.Ids,
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
"sundynix-micro-go/app/file/rpc/fileservice"
|
||||
)
|
||||
|
||||
type DeleteStorageConfigLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 删除存储配置
|
||||
func NewDeleteStorageConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteStorageConfigLogic {
|
||||
return &DeleteStorageConfigLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DeleteStorageConfigLogic) DeleteStorageConfig(req *types.IdsReq) error {
|
||||
_, err := l.svcCtx.FileRpc.DeleteStorageConfig(l.ctx, &fileservice.DeleteFilesReq{
|
||||
Ids: req.Ids,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/api/internal/oss_core"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
"sundynix-micro-go/app/file/rpc/fileservice"
|
||||
)
|
||||
|
||||
type DownloadFileLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 下载文件
|
||||
func NewDownloadFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DownloadFileLogic {
|
||||
return &DownloadFileLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DownloadFileLogic) DownloadFile(w http.ResponseWriter, req *types.FileIdReq) error {
|
||||
// 1. 获取文件记录
|
||||
resp, err := l.svcCtx.FileRpc.GetFileById(l.ctx, &fileservice.GetFileByIdReq{
|
||||
Id: req.Id,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 2. 获取具体底层client
|
||||
factory := oss_core.NewOSSFactory(l.svcCtx.FileRpc)
|
||||
uploader, err := factory.GetActiveUploader(l.ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 3. 读取流
|
||||
reader, err := uploader.DownloadFile(l.ctx, resp.File.Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
// 4. 返回文件流
|
||||
w.Header().Set("Content-Disposition", "attachment; filename=\""+resp.File.Name+"\"")
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
_, err = io.Copy(w, reader)
|
||||
return err
|
||||
}
|
||||
@@ -6,10 +6,10 @@ package file
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/rpc/fileservice"
|
||||
)
|
||||
|
||||
type GetFileByIdLogic struct {
|
||||
@@ -28,7 +28,20 @@ func NewGetFileByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFi
|
||||
}
|
||||
|
||||
func (l *GetFileByIdLogic) GetFileById(req *types.FileIdReq) (resp *types.FileInfo, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
respRpc, err := l.svcCtx.FileRpc.GetFileById(l.ctx, &fileservice.GetFileByIdReq{
|
||||
Id: req.Id,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return
|
||||
return &types.FileInfo{
|
||||
Id: respRpc.File.Id,
|
||||
Name: respRpc.File.Name,
|
||||
Url: respRpc.File.Url,
|
||||
Tag: respRpc.File.Tag,
|
||||
Key: respRpc.File.Key,
|
||||
Suffix: respRpc.File.Suffix,
|
||||
Md5: respRpc.File.Md5,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -6,10 +6,10 @@ package file
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/rpc/fileservice"
|
||||
)
|
||||
|
||||
type GetFileListLogic struct {
|
||||
@@ -27,8 +27,31 @@ func NewGetFileListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFi
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetFileListLogic) GetFileList(req *types.FileListReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
func (l *GetFileListLogic) GetFileList(req *types.FileListReq) (resp *types.FileListResp, err error) {
|
||||
respRpc, err := l.svcCtx.FileRpc.GetFileList(l.ctx, &fileservice.GetFileListReq{
|
||||
Current: int32(req.Current),
|
||||
PageSize: int32(req.PageSize),
|
||||
Name: req.Name,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil
|
||||
var list []types.FileInfo
|
||||
for _, item := range respRpc.List {
|
||||
list = append(list, types.FileInfo{
|
||||
Id: item.Id,
|
||||
Name: item.Name,
|
||||
Url: item.Url,
|
||||
Tag: item.Tag,
|
||||
Key: item.Key,
|
||||
Suffix: item.Suffix,
|
||||
Md5: item.Md5,
|
||||
})
|
||||
}
|
||||
|
||||
return &types.FileListResp{
|
||||
List: list,
|
||||
Total: respRpc.Total,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
"sundynix-micro-go/app/file/rpc/fileservice"
|
||||
)
|
||||
|
||||
type GetStorageConfigListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 获取存储配置列表
|
||||
func NewGetStorageConfigListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetStorageConfigListLogic {
|
||||
return &GetStorageConfigListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetStorageConfigListLogic) GetStorageConfigList(req *types.StorageConfigListReq) (resp *types.StorageConfigListResp, err error) {
|
||||
respRpc, err := l.svcCtx.FileRpc.GetStorageConfigList(l.ctx, &fileservice.StorageConfigListReq{
|
||||
Current: int32(req.Current),
|
||||
PageSize: int32(req.PageSize),
|
||||
Type: req.Type,
|
||||
Name: req.Name,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var list []types.StorageConfigInfo
|
||||
for _, item := range respRpc.List {
|
||||
list = append(list, types.StorageConfigInfo{
|
||||
Id: item.Id,
|
||||
Type: item.Type,
|
||||
Name: item.Name,
|
||||
Endpoint: item.Endpoint,
|
||||
AccessKeyId: item.AccessKeyId,
|
||||
AccessKeySecret: item.AccessKeySecret,
|
||||
BucketName: item.BucketName,
|
||||
BucketUrl: item.BucketUrl,
|
||||
Region: item.Region,
|
||||
IsDefault: int(item.IsDefault),
|
||||
Status: int(item.Status),
|
||||
Remark: item.Remark,
|
||||
})
|
||||
}
|
||||
|
||||
return &types.StorageConfigListResp{
|
||||
List: list,
|
||||
Total: respRpc.Total,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
"sundynix-micro-go/app/file/rpc/fileservice"
|
||||
)
|
||||
|
||||
type SetDefaultStorageConfigLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 设置默认存储配置
|
||||
func NewSetDefaultStorageConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetDefaultStorageConfigLogic {
|
||||
return &SetDefaultStorageConfigLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SetDefaultStorageConfigLogic) SetDefaultStorageConfig(req *types.SetDefaultStorageConfigReq) error {
|
||||
_, err := l.svcCtx.FileRpc.SetDefaultStorageConfig(l.ctx, &fileservice.SetDefaultStorageConfigReq{
|
||||
Id: req.Id,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
"sundynix-micro-go/app/file/rpc/fileservice"
|
||||
)
|
||||
|
||||
type UpdateStorageConfigLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 更新存储配置
|
||||
func NewUpdateStorageConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateStorageConfigLogic {
|
||||
return &UpdateStorageConfigLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateStorageConfigLogic) UpdateStorageConfig(req *types.UpdateStorageConfigReq) error {
|
||||
_, err := l.svcCtx.FileRpc.UpdateStorageConfig(l.ctx, &fileservice.UpdateStorageConfigReq{
|
||||
Id: req.Id,
|
||||
Type: req.Type,
|
||||
Name: req.Name,
|
||||
Endpoint: req.Endpoint,
|
||||
AccessKeyId: req.AccessKeyId,
|
||||
AccessKeySecret: req.AccessKeySecret,
|
||||
BucketName: req.BucketName,
|
||||
BucketUrl: req.BucketUrl,
|
||||
Region: req.Region,
|
||||
Status: int32(req.Status),
|
||||
Remark: req.Remark,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -5,9 +5,17 @@ package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"sundynix-micro-go/app/file/api/internal/oss_core"
|
||||
"sundynix-micro-go/app/file/api/internal/svc"
|
||||
"sundynix-micro-go/app/file/api/internal/types"
|
||||
"sundynix-micro-go/app/file/rpc/fileservice"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -27,8 +35,84 @@ func NewUploadFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Upload
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UploadFileLogic) UploadFile() (resp *types.FileInfo, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
func (l *UploadFileLogic) UploadFile(r *http.Request) (resp *types.FileInfo, err error) {
|
||||
err = r.ParseMultipartForm(32 << 20) // 32MB max memory
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("解析表单失败: %v", err)
|
||||
}
|
||||
file, fileHeader, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取文件失败: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
return
|
||||
// 计算文件 MD5
|
||||
hash := md5.New()
|
||||
if _, err := io.Copy(hash, file); err != nil {
|
||||
return nil, fmt.Errorf("计算文件MD5失败: %v", err)
|
||||
}
|
||||
fileMd5 := hex.EncodeToString(hash.Sum(nil))
|
||||
|
||||
// 调用 RPC 检查文件是否已存在(秒传)
|
||||
checkResp, err := l.svcCtx.FileRpc.CheckFileByMd5(l.ctx, &fileservice.CheckFileByMd5Req{Md5: fileMd5})
|
||||
if err != nil {
|
||||
l.Logger.Errorf("调用FileRpc检查MD5失败: %v", err)
|
||||
return nil, fmt.Errorf("服务器内部异常")
|
||||
}
|
||||
if checkResp.Exists && checkResp.File != nil {
|
||||
l.Logger.Infof("文件已存在,触发秒传: %s", fileMd5)
|
||||
return &types.FileInfo{
|
||||
Id: checkResp.File.Id,
|
||||
Name: checkResp.File.Name,
|
||||
Url: checkResp.File.Url,
|
||||
Tag: checkResp.File.Tag,
|
||||
Key: checkResp.File.Key,
|
||||
Suffix: checkResp.File.Suffix,
|
||||
Md5: checkResp.File.Md5,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 此时需要真正上传,先将文件指针拨回开头
|
||||
if _, err := file.Seek(0, 0); err != nil {
|
||||
return nil, fmt.Errorf("读取文件流失败: %v", err)
|
||||
}
|
||||
|
||||
factory := oss_core.NewOSSFactory(l.svcCtx.FileRpc)
|
||||
uploader, err := factory.GetActiveUploader(l.ctx)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("获取OSS客户端失败: %v", err)
|
||||
return nil, fmt.Errorf("存储服务未配置或不可用")
|
||||
}
|
||||
|
||||
url, key, err := uploader.UploadFile(l.ctx, file, fileHeader)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("上传文件失败: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ext := filepath.Ext(fileHeader.Filename)
|
||||
|
||||
// 把记录存入数据库
|
||||
rpcResp, err := l.svcCtx.FileRpc.CreateFile(l.ctx, &fileservice.CreateFileReq{
|
||||
Name: fileHeader.Filename,
|
||||
Url: url,
|
||||
Tag: "default",
|
||||
Key: key,
|
||||
Suffix: ext,
|
||||
Md5: fileMd5, // 保存计算出的MD5
|
||||
})
|
||||
if err != nil {
|
||||
l.Logger.Errorf("调用FileRpc创建文件记录失败: %v", err)
|
||||
return nil, fmt.Errorf("保存文件记录失败")
|
||||
}
|
||||
|
||||
return &types.FileInfo{
|
||||
Id: rpcResp.File.Id,
|
||||
Name: rpcResp.File.Name,
|
||||
Url: rpcResp.File.Url,
|
||||
Tag: rpcResp.File.Tag,
|
||||
Key: rpcResp.File.Key,
|
||||
Suffix: rpcResp.File.Suffix,
|
||||
Md5: rpcResp.File.Md5,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package oss_core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/common/utils/hash"
|
||||
)
|
||||
|
||||
type AliyunUploader struct {
|
||||
bucket *oss.Bucket
|
||||
config *file.StorageConfigInfo
|
||||
}
|
||||
|
||||
func NewAliyunUploader(conf *file.StorageConfigInfo) (Uploader, error) {
|
||||
client, err := oss.New(conf.Endpoint, conf.AccessKeyId, conf.AccessKeySecret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bucket, err := client.Bucket(conf.BucketName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &AliyunUploader{
|
||||
bucket: bucket,
|
||||
config: conf,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *AliyunUploader) UploadFile(ctx context.Context, file multipart.File, fileHeader *multipart.FileHeader) (string, string, error) {
|
||||
ext := filepath.Ext(fileHeader.Filename)
|
||||
filename := hash.MD5([]byte(strings.TrimSuffix(fileHeader.Filename, ext))) + ext
|
||||
timestr := fmt.Sprintf("%d", time.Now().UnixMicro())
|
||||
objectKey := time.Now().Format("2006-01-02") + "/" + timestr + "-" + filename
|
||||
|
||||
err := a.bucket.PutObject(objectKey, file)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("上传阿里云OSS失败: %v", err)
|
||||
}
|
||||
|
||||
url := a.config.BucketUrl + "/" + objectKey
|
||||
return url, objectKey, nil
|
||||
}
|
||||
|
||||
func (a *AliyunUploader) DeleteFile(ctx context.Context, key string) error {
|
||||
return a.bucket.DeleteObject(key)
|
||||
}
|
||||
|
||||
func (a *AliyunUploader) DownloadFile(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
return a.bucket.GetObject(key)
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package oss_core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/fileservice"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type CachedUploader struct {
|
||||
Uploader Uploader
|
||||
Config *file.StorageConfigInfo
|
||||
}
|
||||
|
||||
var (
|
||||
uploaderCache sync.Map // map[string]*CachedUploader
|
||||
cacheMutex sync.Mutex
|
||||
)
|
||||
|
||||
// OSSFactory 工厂结构体
|
||||
type OSSFactory struct {
|
||||
fileRpc fileservice.FileService
|
||||
}
|
||||
|
||||
func NewOSSFactory(fileRpc fileservice.FileService) *OSSFactory {
|
||||
return &OSSFactory{
|
||||
fileRpc: fileRpc,
|
||||
}
|
||||
}
|
||||
|
||||
// GetActiveUploader 获取当前激活的存储上传实例
|
||||
func (f *OSSFactory) GetActiveUploader(ctx context.Context) (Uploader, error) {
|
||||
resp, err := f.fileRpc.GetDefaultStorageConfig(ctx, &fileservice.GetDefaultStorageConfigReq{})
|
||||
if err != nil || resp.Config == nil {
|
||||
return nil, fmt.Errorf("未找到激活的存储配置: %v", err)
|
||||
}
|
||||
|
||||
conf := resp.Config
|
||||
|
||||
// 1. 尝试从缓存获取
|
||||
if cachedVal, ok := uploaderCache.Load(conf.Id); ok {
|
||||
cachedItem := cachedVal.(*CachedUploader)
|
||||
// 优雅之处:深度对比配置是否发生变化,完美支持前端动态修改配置后实时热加载
|
||||
if reflect.DeepEqual(cachedItem.Config, conf) {
|
||||
return cachedItem.Uploader, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 加锁防止高并发下的重复实例化
|
||||
cacheMutex.Lock()
|
||||
defer cacheMutex.Unlock()
|
||||
|
||||
// 3. Double-Check
|
||||
if cachedVal, ok := uploaderCache.Load(conf.Id); ok {
|
||||
cachedItem := cachedVal.(*CachedUploader)
|
||||
if reflect.DeepEqual(cachedItem.Config, conf) {
|
||||
return cachedItem.Uploader, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 实例化新的 Uploader
|
||||
var uploader Uploader
|
||||
switch conf.Type {
|
||||
case "minio":
|
||||
uploader, err = NewMinioUploader(conf)
|
||||
case "aliyun":
|
||||
uploader, err = NewAliyunUploader(conf)
|
||||
case "tencent":
|
||||
uploader, err = NewTencentUploader(conf)
|
||||
case "qiniu":
|
||||
uploader, err = NewQiniuUploader(conf)
|
||||
default:
|
||||
return nil, fmt.Errorf("不支持的存储类型: %s", conf.Type)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 5. 更新缓存
|
||||
uploaderCache.Store(conf.Id, &CachedUploader{
|
||||
Uploader: uploader,
|
||||
Config: conf,
|
||||
})
|
||||
|
||||
return uploader, nil
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package oss_core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/common/utils/hash"
|
||||
)
|
||||
|
||||
type MinioUploader struct {
|
||||
client *minio.Client
|
||||
config *file.StorageConfigInfo
|
||||
}
|
||||
|
||||
func NewMinioUploader(conf *file.StorageConfigInfo) (Uploader, error) {
|
||||
// 判断如果是 https 或者是类似云服务,可以根据 endpoint 后缀猜测 secure
|
||||
useSSL := strings.HasPrefix(conf.Endpoint, "https://") || strings.Contains(conf.Endpoint, "aliyuncs.com") || conf.Endpoint == "oss-cn-hangzhou.aliyuncs.com" // 这里只是示例
|
||||
if strings.HasPrefix(conf.Endpoint, "http://") || strings.HasPrefix(conf.Endpoint, "https://") {
|
||||
// 移除协议头给 minio 客户端
|
||||
conf.Endpoint = strings.TrimPrefix(conf.Endpoint, "http://")
|
||||
conf.Endpoint = strings.TrimPrefix(conf.Endpoint, "https://")
|
||||
} else {
|
||||
// 默认非 SSL
|
||||
useSSL = false
|
||||
}
|
||||
|
||||
client, err := minio.New(conf.Endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(conf.AccessKeyId, conf.AccessKeySecret, ""),
|
||||
Secure: useSSL,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 这里可以加上检查 bucket
|
||||
return &MinioUploader{
|
||||
client: client,
|
||||
config: conf,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *MinioUploader) UploadFile(ctx context.Context, file multipart.File, fileHeader *multipart.FileHeader) (string, string, error) {
|
||||
// 直接重置文件指针(保险起见)
|
||||
file.Seek(0, 0)
|
||||
|
||||
ext := filepath.Ext(fileHeader.Filename)
|
||||
filename := hash.MD5([]byte(strings.TrimSuffix(fileHeader.Filename, ext))) + ext
|
||||
timestr := fmt.Sprintf("%d", time.Now().UnixMicro())
|
||||
objectKey := time.Now().Format("2006-01-02") + "/" + timestr + "-" + filename
|
||||
|
||||
bucketName := m.config.BucketName
|
||||
|
||||
// 直接执行 PutObject(流式上传,不再全部读入内存)
|
||||
info, err := m.client.PutObject(ctx, bucketName, objectKey, file, fileHeader.Size, minio.PutObjectOptions{
|
||||
ContentType: "application/octet-stream",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
// 如果是因为 bucket 不存在,则尝试创建并重试
|
||||
if minio.ToErrorResponse(err).Code == "NoSuchBucket" {
|
||||
m.client.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{})
|
||||
// 重置文件流位置,准备重试
|
||||
file.Seek(0, 0)
|
||||
info, err = m.client.PutObject(ctx, bucketName, objectKey, file, fileHeader.Size, minio.PutObjectOptions{
|
||||
ContentType: "application/octet-stream",
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("上传Minio失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
url := m.config.BucketUrl + "/" + info.Key
|
||||
return url, info.Key, nil
|
||||
}
|
||||
|
||||
func (m *MinioUploader) DeleteFile(ctx context.Context, key string) error {
|
||||
return m.client.RemoveObject(ctx, m.config.BucketName, key, minio.RemoveObjectOptions{})
|
||||
}
|
||||
|
||||
func (m *MinioUploader) DownloadFile(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
obj, err := m.client.GetObject(ctx, m.config.BucketName, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return obj, nil
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package oss_core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/qiniu/go-sdk/v7/auth/qbox"
|
||||
"github.com/qiniu/go-sdk/v7/storage"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/common/utils/hash"
|
||||
)
|
||||
|
||||
type QiniuUploader struct {
|
||||
mac *qbox.Mac
|
||||
config *file.StorageConfigInfo
|
||||
}
|
||||
|
||||
func NewQiniuUploader(conf *file.StorageConfigInfo) (Uploader, error) {
|
||||
mac := qbox.NewMac(conf.AccessKeyId, conf.AccessKeySecret)
|
||||
return &QiniuUploader{
|
||||
mac: mac,
|
||||
config: conf,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (q *QiniuUploader) UploadFile(ctx context.Context, file multipart.File, fileHeader *multipart.FileHeader) (string, string, error) {
|
||||
ext := filepath.Ext(fileHeader.Filename)
|
||||
filename := hash.MD5([]byte(strings.TrimSuffix(fileHeader.Filename, ext))) + ext
|
||||
timestr := fmt.Sprintf("%d", time.Now().UnixMicro())
|
||||
objectKey := time.Now().Format("2006-01-02") + "/" + timestr + "-" + filename
|
||||
|
||||
putPolicy := storage.PutPolicy{
|
||||
Scope: q.config.BucketName,
|
||||
}
|
||||
upToken := putPolicy.UploadToken(q.mac)
|
||||
|
||||
cfg := storage.Config{}
|
||||
// 根据配置中的 Region 判断
|
||||
// 这里简单写死,如果要自适应可以根据 config.Region 给定
|
||||
cfg.Zone = &storage.ZoneHuadong
|
||||
cfg.UseHTTPS = false
|
||||
cfg.UseCdnDomains = false
|
||||
|
||||
formUploader := storage.NewFormUploader(&cfg)
|
||||
ret := storage.PutRet{}
|
||||
|
||||
err := formUploader.Put(ctx, &ret, upToken, objectKey, file, fileHeader.Size, nil)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("上传七牛云失败: %v", err)
|
||||
}
|
||||
|
||||
url := q.config.BucketUrl + "/" + ret.Key
|
||||
return url, ret.Key, nil
|
||||
}
|
||||
|
||||
func (q *QiniuUploader) DeleteFile(ctx context.Context, key string) error {
|
||||
cfg := storage.Config{}
|
||||
cfg.Zone = &storage.ZoneHuadong
|
||||
bucketManager := storage.NewBucketManager(q.mac, &cfg)
|
||||
return bucketManager.Delete(q.config.BucketName, key)
|
||||
}
|
||||
|
||||
func (q *QiniuUploader) DownloadFile(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
mac := qbox.NewMac(q.config.AccessKeyId, q.config.AccessKeySecret)
|
||||
domain := q.config.BucketUrl
|
||||
deadline := time.Now().Add(time.Second * 3600).Unix() // 1小时有效期
|
||||
privateAccessURL := storage.MakePrivateURL(mac, domain, key, deadline)
|
||||
|
||||
resp, err := http.Get(privateAccessURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
resp.Body.Close()
|
||||
return nil, fmt.Errorf("qiniu download failed with status: %s", resp.Status)
|
||||
}
|
||||
return resp.Body, nil
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package oss_core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/common/utils/hash"
|
||||
)
|
||||
|
||||
type TencentUploader struct {
|
||||
client *cos.Client
|
||||
config *file.StorageConfigInfo
|
||||
}
|
||||
|
||||
func NewTencentUploader(conf *file.StorageConfigInfo) (Uploader, error) {
|
||||
// Endpoint should be something like https://bucket-appid.cos.ap-guangzhou.myqcloud.com
|
||||
// But usually users just put bucket name and region, here we assume endpoint is full bucket url
|
||||
// Or we use bucketUrl as the endpoint for client initialization if endpoint is not formatted well
|
||||
u, err := url.Parse(conf.Endpoint)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("解析Endpoint失败: %v", err)
|
||||
}
|
||||
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
client := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: conf.AccessKeyId,
|
||||
SecretKey: conf.AccessKeySecret,
|
||||
},
|
||||
})
|
||||
|
||||
return &TencentUploader{
|
||||
client: client,
|
||||
config: conf,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *TencentUploader) UploadFile(ctx context.Context, file multipart.File, fileHeader *multipart.FileHeader) (string, string, error) {
|
||||
ext := filepath.Ext(fileHeader.Filename)
|
||||
filename := hash.MD5([]byte(strings.TrimSuffix(fileHeader.Filename, ext))) + ext
|
||||
timestr := fmt.Sprintf("%d", time.Now().UnixMicro())
|
||||
objectKey := time.Now().Format("2006-01-02") + "/" + timestr + "-" + filename
|
||||
|
||||
_, err := t.client.Object.Put(ctx, objectKey, file, nil)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("上传腾讯云COS失败: %v", err)
|
||||
}
|
||||
|
||||
fileUrl := t.config.BucketUrl + "/" + objectKey
|
||||
return fileUrl, objectKey, nil
|
||||
}
|
||||
|
||||
func (t *TencentUploader) DeleteFile(ctx context.Context, key string) error {
|
||||
_, err := t.client.Object.Delete(ctx, key)
|
||||
return err
|
||||
}
|
||||
|
||||
func (t *TencentUploader) DownloadFile(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
resp, err := t.client.Object.Get(ctx, key, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Body, nil
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package oss_core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
)
|
||||
|
||||
// Uploader 统一存储上传接口
|
||||
type Uploader interface {
|
||||
// UploadFile 接收文件流并上传,返回存储的具体url,标识key和可能的错误
|
||||
UploadFile(ctx context.Context, file multipart.File, fileHeader *multipart.FileHeader) (url string, key string, err error)
|
||||
// DeleteFile 删除远端存储的文件
|
||||
DeleteFile(ctx context.Context, key string) error
|
||||
// DownloadFile 获取文件下载的数据流
|
||||
DownloadFile(ctx context.Context, key string) (io.ReadCloser, error)
|
||||
}
|
||||
@@ -3,6 +3,19 @@
|
||||
|
||||
package types
|
||||
|
||||
type CreateStorageConfigReq struct {
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
AccessKeyId string `json:"accessKeyId"`
|
||||
AccessKeySecret string `json:"accessKeySecret"`
|
||||
BucketName string `json:"bucketName"`
|
||||
BucketUrl string `json:"bucketUrl"`
|
||||
Region string `json:"region,optional"`
|
||||
Status int `json:"status,optional"`
|
||||
Remark string `json:"remark,optional"`
|
||||
}
|
||||
|
||||
type FileIdReq struct {
|
||||
Id string `path:"id"`
|
||||
}
|
||||
@@ -23,6 +36,56 @@ type FileListReq struct {
|
||||
Name string `json:"name,optional"`
|
||||
}
|
||||
|
||||
type FileListResp struct {
|
||||
List []FileInfo `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
type IdsReq struct {
|
||||
Ids []string `json:"ids"`
|
||||
}
|
||||
|
||||
type SetDefaultStorageConfigReq struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
type StorageConfigInfo struct {
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
AccessKeyId string `json:"accessKeyId"`
|
||||
AccessKeySecret string `json:"accessKeySecret"`
|
||||
BucketName string `json:"bucketName"`
|
||||
BucketUrl string `json:"bucketUrl"`
|
||||
Region string `json:"region"`
|
||||
IsDefault int `json:"isDefault"`
|
||||
Status int `json:"status"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
type StorageConfigListReq struct {
|
||||
Current int `json:"current,optional"`
|
||||
PageSize int `json:"pageSize,optional"`
|
||||
Type string `json:"type,optional"`
|
||||
Name string `json:"name,optional"`
|
||||
}
|
||||
|
||||
type StorageConfigListResp struct {
|
||||
List []StorageConfigInfo `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
type UpdateStorageConfigReq struct {
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
AccessKeyId string `json:"accessKeyId"`
|
||||
AccessKeySecret string `json:"accessKeySecret"`
|
||||
BucketName string `json:"bucketName"`
|
||||
BucketUrl string `json:"bucketUrl"`
|
||||
Region string `json:"region,optional"`
|
||||
Status int `json:"status,optional"`
|
||||
Remark string `json:"remark,optional"`
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"sundynix-micro-go/common/model"
|
||||
)
|
||||
|
||||
// StorageConfig 动态存储配置表
|
||||
type StorageConfig struct {
|
||||
model.BaseModel
|
||||
Type string `json:"type" gorm:"column:type;type:varchar(20);comment:存储类型(minio,aliyun,tencent,qiniu)"`
|
||||
Name string `json:"name" gorm:"column:name;type:varchar(50);comment:配置名称"`
|
||||
Endpoint string `json:"endpoint" gorm:"column:endpoint;type:varchar(100);comment:访问端点"`
|
||||
AccessKeyId string `json:"accessKeyId" gorm:"column:access_key_id;type:varchar(100);comment:访问秘钥ID"`
|
||||
AccessKeySecret string `json:"accessKeySecret" gorm:"column:access_key_secret;type:varchar(200);comment:访问秘钥"`
|
||||
BucketName string `json:"bucketName" gorm:"column:bucket_name;type:varchar(50);comment:存储桶名称"`
|
||||
BucketUrl string `json:"bucketUrl" gorm:"column:bucket_url;type:varchar(100);comment:绑定的外部访问域名"`
|
||||
Region string `json:"region" gorm:"column:region;type:varchar(50);comment:地域节点"`
|
||||
IsDefault int `json:"isDefault" gorm:"column:is_default;type:tinyint(1);default:0;comment:是否默认激活(1=是,0=否)"`
|
||||
Status int `json:"status" gorm:"column:status;type:tinyint(1);default:1;comment:状态(1=启用,0=禁用)"`
|
||||
Remark string `json:"remark" gorm:"column:remark;type:varchar(255);comment:备注"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (StorageConfig) TableName() string {
|
||||
return "sundynix_storage_config"
|
||||
}
|
||||
+1149
-21
File diff suppressed because it is too large
Load Diff
@@ -19,8 +19,18 @@ import (
|
||||
const _ = grpc.SupportPackageIsVersion9
|
||||
|
||||
const (
|
||||
FileService_GetFileById_FullMethodName = "/file.FileService/GetFileById"
|
||||
FileService_GetFilesByIds_FullMethodName = "/file.FileService/GetFilesByIds"
|
||||
FileService_GetFileById_FullMethodName = "/file.FileService/GetFileById"
|
||||
FileService_GetFilesByIds_FullMethodName = "/file.FileService/GetFilesByIds"
|
||||
FileService_CreateFile_FullMethodName = "/file.FileService/CreateFile"
|
||||
FileService_CheckFileByMd5_FullMethodName = "/file.FileService/CheckFileByMd5"
|
||||
FileService_DeleteFiles_FullMethodName = "/file.FileService/DeleteFiles"
|
||||
FileService_GetFileList_FullMethodName = "/file.FileService/GetFileList"
|
||||
FileService_CreateStorageConfig_FullMethodName = "/file.FileService/CreateStorageConfig"
|
||||
FileService_UpdateStorageConfig_FullMethodName = "/file.FileService/UpdateStorageConfig"
|
||||
FileService_DeleteStorageConfig_FullMethodName = "/file.FileService/DeleteStorageConfig"
|
||||
FileService_SetDefaultStorageConfig_FullMethodName = "/file.FileService/SetDefaultStorageConfig"
|
||||
FileService_GetStorageConfigList_FullMethodName = "/file.FileService/GetStorageConfigList"
|
||||
FileService_GetDefaultStorageConfig_FullMethodName = "/file.FileService/GetDefaultStorageConfig"
|
||||
)
|
||||
|
||||
// FileServiceClient is the client API for FileService service.
|
||||
@@ -31,6 +41,21 @@ type FileServiceClient interface {
|
||||
GetFileById(ctx context.Context, in *GetFileByIdReq, opts ...grpc.CallOption) (*GetFileByIdResp, error)
|
||||
// 根据ID列表批量获取文件信息
|
||||
GetFilesByIds(ctx context.Context, in *GetFilesByIdsReq, opts ...grpc.CallOption) (*GetFilesByIdsResp, error)
|
||||
// 创建文件记录
|
||||
CreateFile(ctx context.Context, in *CreateFileReq, opts ...grpc.CallOption) (*CreateFileResp, error)
|
||||
// 通过MD5检查文件是否存在
|
||||
CheckFileByMd5(ctx context.Context, in *CheckFileByMd5Req, opts ...grpc.CallOption) (*CheckFileByMd5Resp, error)
|
||||
// 删除文件记录
|
||||
DeleteFiles(ctx context.Context, in *DeleteFilesReq, opts ...grpc.CallOption) (*CommonResp, error)
|
||||
// 获取文件列表
|
||||
GetFileList(ctx context.Context, in *GetFileListReq, opts ...grpc.CallOption) (*GetFileListResp, error)
|
||||
// ---------- 存储配置 ----------
|
||||
CreateStorageConfig(ctx context.Context, in *CreateStorageConfigReq, opts ...grpc.CallOption) (*CommonResp, error)
|
||||
UpdateStorageConfig(ctx context.Context, in *UpdateStorageConfigReq, opts ...grpc.CallOption) (*CommonResp, error)
|
||||
DeleteStorageConfig(ctx context.Context, in *DeleteFilesReq, opts ...grpc.CallOption) (*CommonResp, error)
|
||||
SetDefaultStorageConfig(ctx context.Context, in *SetDefaultStorageConfigReq, opts ...grpc.CallOption) (*CommonResp, error)
|
||||
GetStorageConfigList(ctx context.Context, in *StorageConfigListReq, opts ...grpc.CallOption) (*StorageConfigListResp, error)
|
||||
GetDefaultStorageConfig(ctx context.Context, in *GetDefaultStorageConfigReq, opts ...grpc.CallOption) (*GetDefaultStorageConfigResp, error)
|
||||
}
|
||||
|
||||
type fileServiceClient struct {
|
||||
@@ -61,6 +86,106 @@ func (c *fileServiceClient) GetFilesByIds(ctx context.Context, in *GetFilesByIds
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *fileServiceClient) CreateFile(ctx context.Context, in *CreateFileReq, opts ...grpc.CallOption) (*CreateFileResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CreateFileResp)
|
||||
err := c.cc.Invoke(ctx, FileService_CreateFile_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *fileServiceClient) CheckFileByMd5(ctx context.Context, in *CheckFileByMd5Req, opts ...grpc.CallOption) (*CheckFileByMd5Resp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CheckFileByMd5Resp)
|
||||
err := c.cc.Invoke(ctx, FileService_CheckFileByMd5_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *fileServiceClient) DeleteFiles(ctx context.Context, in *DeleteFilesReq, opts ...grpc.CallOption) (*CommonResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CommonResp)
|
||||
err := c.cc.Invoke(ctx, FileService_DeleteFiles_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *fileServiceClient) GetFileList(ctx context.Context, in *GetFileListReq, opts ...grpc.CallOption) (*GetFileListResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(GetFileListResp)
|
||||
err := c.cc.Invoke(ctx, FileService_GetFileList_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *fileServiceClient) CreateStorageConfig(ctx context.Context, in *CreateStorageConfigReq, opts ...grpc.CallOption) (*CommonResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CommonResp)
|
||||
err := c.cc.Invoke(ctx, FileService_CreateStorageConfig_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *fileServiceClient) UpdateStorageConfig(ctx context.Context, in *UpdateStorageConfigReq, opts ...grpc.CallOption) (*CommonResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CommonResp)
|
||||
err := c.cc.Invoke(ctx, FileService_UpdateStorageConfig_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *fileServiceClient) DeleteStorageConfig(ctx context.Context, in *DeleteFilesReq, opts ...grpc.CallOption) (*CommonResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CommonResp)
|
||||
err := c.cc.Invoke(ctx, FileService_DeleteStorageConfig_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *fileServiceClient) SetDefaultStorageConfig(ctx context.Context, in *SetDefaultStorageConfigReq, opts ...grpc.CallOption) (*CommonResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CommonResp)
|
||||
err := c.cc.Invoke(ctx, FileService_SetDefaultStorageConfig_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *fileServiceClient) GetStorageConfigList(ctx context.Context, in *StorageConfigListReq, opts ...grpc.CallOption) (*StorageConfigListResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(StorageConfigListResp)
|
||||
err := c.cc.Invoke(ctx, FileService_GetStorageConfigList_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *fileServiceClient) GetDefaultStorageConfig(ctx context.Context, in *GetDefaultStorageConfigReq, opts ...grpc.CallOption) (*GetDefaultStorageConfigResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(GetDefaultStorageConfigResp)
|
||||
err := c.cc.Invoke(ctx, FileService_GetDefaultStorageConfig_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// FileServiceServer is the server API for FileService service.
|
||||
// All implementations must embed UnimplementedFileServiceServer
|
||||
// for forward compatibility.
|
||||
@@ -69,6 +194,21 @@ type FileServiceServer interface {
|
||||
GetFileById(context.Context, *GetFileByIdReq) (*GetFileByIdResp, error)
|
||||
// 根据ID列表批量获取文件信息
|
||||
GetFilesByIds(context.Context, *GetFilesByIdsReq) (*GetFilesByIdsResp, error)
|
||||
// 创建文件记录
|
||||
CreateFile(context.Context, *CreateFileReq) (*CreateFileResp, error)
|
||||
// 通过MD5检查文件是否存在
|
||||
CheckFileByMd5(context.Context, *CheckFileByMd5Req) (*CheckFileByMd5Resp, error)
|
||||
// 删除文件记录
|
||||
DeleteFiles(context.Context, *DeleteFilesReq) (*CommonResp, error)
|
||||
// 获取文件列表
|
||||
GetFileList(context.Context, *GetFileListReq) (*GetFileListResp, error)
|
||||
// ---------- 存储配置 ----------
|
||||
CreateStorageConfig(context.Context, *CreateStorageConfigReq) (*CommonResp, error)
|
||||
UpdateStorageConfig(context.Context, *UpdateStorageConfigReq) (*CommonResp, error)
|
||||
DeleteStorageConfig(context.Context, *DeleteFilesReq) (*CommonResp, error)
|
||||
SetDefaultStorageConfig(context.Context, *SetDefaultStorageConfigReq) (*CommonResp, error)
|
||||
GetStorageConfigList(context.Context, *StorageConfigListReq) (*StorageConfigListResp, error)
|
||||
GetDefaultStorageConfig(context.Context, *GetDefaultStorageConfigReq) (*GetDefaultStorageConfigResp, error)
|
||||
mustEmbedUnimplementedFileServiceServer()
|
||||
}
|
||||
|
||||
@@ -85,6 +225,36 @@ func (UnimplementedFileServiceServer) GetFileById(context.Context, *GetFileByIdR
|
||||
func (UnimplementedFileServiceServer) GetFilesByIds(context.Context, *GetFilesByIdsReq) (*GetFilesByIdsResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method GetFilesByIds not implemented")
|
||||
}
|
||||
func (UnimplementedFileServiceServer) CreateFile(context.Context, *CreateFileReq) (*CreateFileResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method CreateFile not implemented")
|
||||
}
|
||||
func (UnimplementedFileServiceServer) CheckFileByMd5(context.Context, *CheckFileByMd5Req) (*CheckFileByMd5Resp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method CheckFileByMd5 not implemented")
|
||||
}
|
||||
func (UnimplementedFileServiceServer) DeleteFiles(context.Context, *DeleteFilesReq) (*CommonResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method DeleteFiles not implemented")
|
||||
}
|
||||
func (UnimplementedFileServiceServer) GetFileList(context.Context, *GetFileListReq) (*GetFileListResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method GetFileList not implemented")
|
||||
}
|
||||
func (UnimplementedFileServiceServer) CreateStorageConfig(context.Context, *CreateStorageConfigReq) (*CommonResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method CreateStorageConfig not implemented")
|
||||
}
|
||||
func (UnimplementedFileServiceServer) UpdateStorageConfig(context.Context, *UpdateStorageConfigReq) (*CommonResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method UpdateStorageConfig not implemented")
|
||||
}
|
||||
func (UnimplementedFileServiceServer) DeleteStorageConfig(context.Context, *DeleteFilesReq) (*CommonResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method DeleteStorageConfig not implemented")
|
||||
}
|
||||
func (UnimplementedFileServiceServer) SetDefaultStorageConfig(context.Context, *SetDefaultStorageConfigReq) (*CommonResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method SetDefaultStorageConfig not implemented")
|
||||
}
|
||||
func (UnimplementedFileServiceServer) GetStorageConfigList(context.Context, *StorageConfigListReq) (*StorageConfigListResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method GetStorageConfigList not implemented")
|
||||
}
|
||||
func (UnimplementedFileServiceServer) GetDefaultStorageConfig(context.Context, *GetDefaultStorageConfigReq) (*GetDefaultStorageConfigResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method GetDefaultStorageConfig not implemented")
|
||||
}
|
||||
func (UnimplementedFileServiceServer) mustEmbedUnimplementedFileServiceServer() {}
|
||||
func (UnimplementedFileServiceServer) testEmbeddedByValue() {}
|
||||
|
||||
@@ -142,6 +312,186 @@ func _FileService_GetFilesByIds_Handler(srv interface{}, ctx context.Context, de
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _FileService_CreateFile_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(CreateFileReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FileServiceServer).CreateFile(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: FileService_CreateFile_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FileServiceServer).CreateFile(ctx, req.(*CreateFileReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _FileService_CheckFileByMd5_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(CheckFileByMd5Req)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FileServiceServer).CheckFileByMd5(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: FileService_CheckFileByMd5_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FileServiceServer).CheckFileByMd5(ctx, req.(*CheckFileByMd5Req))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _FileService_DeleteFiles_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(DeleteFilesReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FileServiceServer).DeleteFiles(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: FileService_DeleteFiles_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FileServiceServer).DeleteFiles(ctx, req.(*DeleteFilesReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _FileService_GetFileList_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetFileListReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FileServiceServer).GetFileList(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: FileService_GetFileList_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FileServiceServer).GetFileList(ctx, req.(*GetFileListReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _FileService_CreateStorageConfig_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(CreateStorageConfigReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FileServiceServer).CreateStorageConfig(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: FileService_CreateStorageConfig_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FileServiceServer).CreateStorageConfig(ctx, req.(*CreateStorageConfigReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _FileService_UpdateStorageConfig_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(UpdateStorageConfigReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FileServiceServer).UpdateStorageConfig(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: FileService_UpdateStorageConfig_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FileServiceServer).UpdateStorageConfig(ctx, req.(*UpdateStorageConfigReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _FileService_DeleteStorageConfig_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(DeleteFilesReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FileServiceServer).DeleteStorageConfig(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: FileService_DeleteStorageConfig_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FileServiceServer).DeleteStorageConfig(ctx, req.(*DeleteFilesReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _FileService_SetDefaultStorageConfig_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(SetDefaultStorageConfigReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FileServiceServer).SetDefaultStorageConfig(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: FileService_SetDefaultStorageConfig_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FileServiceServer).SetDefaultStorageConfig(ctx, req.(*SetDefaultStorageConfigReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _FileService_GetStorageConfigList_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(StorageConfigListReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FileServiceServer).GetStorageConfigList(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: FileService_GetStorageConfigList_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FileServiceServer).GetStorageConfigList(ctx, req.(*StorageConfigListReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _FileService_GetDefaultStorageConfig_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetDefaultStorageConfigReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FileServiceServer).GetDefaultStorageConfig(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: FileService_GetDefaultStorageConfig_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FileServiceServer).GetDefaultStorageConfig(ctx, req.(*GetDefaultStorageConfigReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// FileService_ServiceDesc is the grpc.ServiceDesc for FileService service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
@@ -157,6 +507,46 @@ var FileService_ServiceDesc = grpc.ServiceDesc{
|
||||
MethodName: "GetFilesByIds",
|
||||
Handler: _FileService_GetFilesByIds_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "CreateFile",
|
||||
Handler: _FileService_CreateFile_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "CheckFileByMd5",
|
||||
Handler: _FileService_CheckFileByMd5_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "DeleteFiles",
|
||||
Handler: _FileService_DeleteFiles_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetFileList",
|
||||
Handler: _FileService_GetFileList_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "CreateStorageConfig",
|
||||
Handler: _FileService_CreateStorageConfig_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "UpdateStorageConfig",
|
||||
Handler: _FileService_UpdateStorageConfig_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "DeleteStorageConfig",
|
||||
Handler: _FileService_DeleteStorageConfig_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "SetDefaultStorageConfig",
|
||||
Handler: _FileService_SetDefaultStorageConfig_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetStorageConfigList",
|
||||
Handler: _FileService_GetStorageConfigList_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetDefaultStorageConfig",
|
||||
Handler: _FileService_GetDefaultStorageConfig_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "pb/file.proto",
|
||||
|
||||
@@ -14,18 +14,48 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
CommonResp = file.CommonResp
|
||||
FileInfo = file.FileInfo
|
||||
GetFileByIdReq = file.GetFileByIdReq
|
||||
GetFileByIdResp = file.GetFileByIdResp
|
||||
GetFilesByIdsReq = file.GetFilesByIdsReq
|
||||
GetFilesByIdsResp = file.GetFilesByIdsResp
|
||||
CheckFileByMd5Req = file.CheckFileByMd5Req
|
||||
CheckFileByMd5Resp = file.CheckFileByMd5Resp
|
||||
CommonResp = file.CommonResp
|
||||
CreateFileReq = file.CreateFileReq
|
||||
CreateFileResp = file.CreateFileResp
|
||||
CreateStorageConfigReq = file.CreateStorageConfigReq
|
||||
DeleteFilesReq = file.DeleteFilesReq
|
||||
FileInfo = file.FileInfo
|
||||
GetDefaultStorageConfigReq = file.GetDefaultStorageConfigReq
|
||||
GetDefaultStorageConfigResp = file.GetDefaultStorageConfigResp
|
||||
GetFileByIdReq = file.GetFileByIdReq
|
||||
GetFileByIdResp = file.GetFileByIdResp
|
||||
GetFileListReq = file.GetFileListReq
|
||||
GetFileListResp = file.GetFileListResp
|
||||
GetFilesByIdsReq = file.GetFilesByIdsReq
|
||||
GetFilesByIdsResp = file.GetFilesByIdsResp
|
||||
SetDefaultStorageConfigReq = file.SetDefaultStorageConfigReq
|
||||
StorageConfigInfo = file.StorageConfigInfo
|
||||
StorageConfigListReq = file.StorageConfigListReq
|
||||
StorageConfigListResp = file.StorageConfigListResp
|
||||
UpdateStorageConfigReq = file.UpdateStorageConfigReq
|
||||
|
||||
FileService interface {
|
||||
// 根据ID获取文件信息
|
||||
GetFileById(ctx context.Context, in *GetFileByIdReq, opts ...grpc.CallOption) (*GetFileByIdResp, error)
|
||||
// 根据ID列表批量获取文件信息
|
||||
GetFilesByIds(ctx context.Context, in *GetFilesByIdsReq, opts ...grpc.CallOption) (*GetFilesByIdsResp, error)
|
||||
// 创建文件记录
|
||||
CreateFile(ctx context.Context, in *CreateFileReq, opts ...grpc.CallOption) (*CreateFileResp, error)
|
||||
// 通过MD5检查文件是否存在
|
||||
CheckFileByMd5(ctx context.Context, in *CheckFileByMd5Req, opts ...grpc.CallOption) (*CheckFileByMd5Resp, error)
|
||||
// 删除文件记录
|
||||
DeleteFiles(ctx context.Context, in *DeleteFilesReq, opts ...grpc.CallOption) (*CommonResp, error)
|
||||
// 获取文件列表
|
||||
GetFileList(ctx context.Context, in *GetFileListReq, opts ...grpc.CallOption) (*GetFileListResp, error)
|
||||
// ---------- 存储配置 ----------
|
||||
CreateStorageConfig(ctx context.Context, in *CreateStorageConfigReq, opts ...grpc.CallOption) (*CommonResp, error)
|
||||
UpdateStorageConfig(ctx context.Context, in *UpdateStorageConfigReq, opts ...grpc.CallOption) (*CommonResp, error)
|
||||
DeleteStorageConfig(ctx context.Context, in *DeleteFilesReq, opts ...grpc.CallOption) (*CommonResp, error)
|
||||
SetDefaultStorageConfig(ctx context.Context, in *SetDefaultStorageConfigReq, opts ...grpc.CallOption) (*CommonResp, error)
|
||||
GetStorageConfigList(ctx context.Context, in *StorageConfigListReq, opts ...grpc.CallOption) (*StorageConfigListResp, error)
|
||||
GetDefaultStorageConfig(ctx context.Context, in *GetDefaultStorageConfigReq, opts ...grpc.CallOption) (*GetDefaultStorageConfigResp, error)
|
||||
}
|
||||
|
||||
defaultFileService struct {
|
||||
@@ -50,3 +80,58 @@ func (m *defaultFileService) GetFilesByIds(ctx context.Context, in *GetFilesById
|
||||
client := file.NewFileServiceClient(m.cli.Conn())
|
||||
return client.GetFilesByIds(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 创建文件记录
|
||||
func (m *defaultFileService) CreateFile(ctx context.Context, in *CreateFileReq, opts ...grpc.CallOption) (*CreateFileResp, error) {
|
||||
client := file.NewFileServiceClient(m.cli.Conn())
|
||||
return client.CreateFile(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 通过MD5检查文件是否存在
|
||||
func (m *defaultFileService) CheckFileByMd5(ctx context.Context, in *CheckFileByMd5Req, opts ...grpc.CallOption) (*CheckFileByMd5Resp, error) {
|
||||
client := file.NewFileServiceClient(m.cli.Conn())
|
||||
return client.CheckFileByMd5(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 删除文件记录
|
||||
func (m *defaultFileService) DeleteFiles(ctx context.Context, in *DeleteFilesReq, opts ...grpc.CallOption) (*CommonResp, error) {
|
||||
client := file.NewFileServiceClient(m.cli.Conn())
|
||||
return client.DeleteFiles(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 获取文件列表
|
||||
func (m *defaultFileService) GetFileList(ctx context.Context, in *GetFileListReq, opts ...grpc.CallOption) (*GetFileListResp, error) {
|
||||
client := file.NewFileServiceClient(m.cli.Conn())
|
||||
return client.GetFileList(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// ---------- 存储配置 ----------
|
||||
func (m *defaultFileService) CreateStorageConfig(ctx context.Context, in *CreateStorageConfigReq, opts ...grpc.CallOption) (*CommonResp, error) {
|
||||
client := file.NewFileServiceClient(m.cli.Conn())
|
||||
return client.CreateStorageConfig(ctx, in, opts...)
|
||||
}
|
||||
|
||||
func (m *defaultFileService) UpdateStorageConfig(ctx context.Context, in *UpdateStorageConfigReq, opts ...grpc.CallOption) (*CommonResp, error) {
|
||||
client := file.NewFileServiceClient(m.cli.Conn())
|
||||
return client.UpdateStorageConfig(ctx, in, opts...)
|
||||
}
|
||||
|
||||
func (m *defaultFileService) DeleteStorageConfig(ctx context.Context, in *DeleteFilesReq, opts ...grpc.CallOption) (*CommonResp, error) {
|
||||
client := file.NewFileServiceClient(m.cli.Conn())
|
||||
return client.DeleteStorageConfig(ctx, in, opts...)
|
||||
}
|
||||
|
||||
func (m *defaultFileService) SetDefaultStorageConfig(ctx context.Context, in *SetDefaultStorageConfigReq, opts ...grpc.CallOption) (*CommonResp, error) {
|
||||
client := file.NewFileServiceClient(m.cli.Conn())
|
||||
return client.SetDefaultStorageConfig(ctx, in, opts...)
|
||||
}
|
||||
|
||||
func (m *defaultFileService) GetStorageConfigList(ctx context.Context, in *StorageConfigListReq, opts ...grpc.CallOption) (*StorageConfigListResp, error) {
|
||||
client := file.NewFileServiceClient(m.cli.Conn())
|
||||
return client.GetStorageConfigList(ctx, in, opts...)
|
||||
}
|
||||
|
||||
func (m *defaultFileService) GetDefaultStorageConfig(ctx context.Context, in *GetDefaultStorageConfigReq, opts ...grpc.CallOption) (*GetDefaultStorageConfigResp, error) {
|
||||
client := file.NewFileServiceClient(m.cli.Conn())
|
||||
return client.GetDefaultStorageConfig(ctx, in, opts...)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"errors"
|
||||
"sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CheckFileByMd5Logic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewCheckFileByMd5Logic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckFileByMd5Logic {
|
||||
return &CheckFileByMd5Logic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 通过MD5检查文件是否存在
|
||||
func (l *CheckFileByMd5Logic) CheckFileByMd5(in *file.CheckFileByMd5Req) (*file.CheckFileByMd5Resp, error) {
|
||||
if in.Md5 == "" {
|
||||
return &file.CheckFileByMd5Resp{Exists: false}, nil
|
||||
}
|
||||
|
||||
var ossRecord model.SundynixOss
|
||||
err := l.svcCtx.DB.Where("md5 = ?", in.Md5).First(&ossRecord).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return &file.CheckFileByMd5Resp{Exists: false}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &file.CheckFileByMd5Resp{
|
||||
Exists: true,
|
||||
File: &file.FileInfo{
|
||||
Id: ossRecord.ID,
|
||||
Name: ossRecord.Name,
|
||||
Url: ossRecord.Url,
|
||||
Tag: ossRecord.Tag,
|
||||
Key: ossRecord.Key,
|
||||
Suffix: ossRecord.Suffix,
|
||||
Md5: ossRecord.MD5,
|
||||
CreatedAt: ossRecord.CreatedAt.Unix(),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
)
|
||||
|
||||
type CreateFileLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewCreateFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateFileLogic {
|
||||
return &CreateFileLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 创建文件记录
|
||||
func (l *CreateFileLogic) CreateFile(in *file.CreateFileReq) (*file.CreateFileResp, error) {
|
||||
ossRecord := &model.SundynixOss{
|
||||
Name: in.Name,
|
||||
Url: in.Url,
|
||||
Tag: in.Tag,
|
||||
Key: in.Key,
|
||||
Suffix: in.Suffix,
|
||||
MD5: in.Md5,
|
||||
}
|
||||
|
||||
err := l.svcCtx.DB.Create(ossRecord).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &file.CreateFileResp{
|
||||
File: &file.FileInfo{
|
||||
Id: ossRecord.ID,
|
||||
Name: ossRecord.Name,
|
||||
Url: ossRecord.Url,
|
||||
Tag: ossRecord.Tag,
|
||||
Key: ossRecord.Key,
|
||||
Suffix: ossRecord.Suffix,
|
||||
Md5: ossRecord.MD5,
|
||||
CreatedAt: ossRecord.CreatedAt.Unix(),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
)
|
||||
|
||||
type CreateStorageConfigLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewCreateStorageConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateStorageConfigLogic {
|
||||
return &CreateStorageConfigLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- 存储配置 ----------
|
||||
func (l *CreateStorageConfigLogic) CreateStorageConfig(in *file.CreateStorageConfigReq) (*file.CommonResp, error) {
|
||||
conf := &model.StorageConfig{
|
||||
Type: in.Type,
|
||||
Name: in.Name,
|
||||
Endpoint: in.Endpoint,
|
||||
AccessKeyId: in.AccessKeyId,
|
||||
AccessKeySecret: in.AccessKeySecret,
|
||||
BucketName: in.BucketName,
|
||||
BucketUrl: in.BucketUrl,
|
||||
Region: in.Region,
|
||||
IsDefault: 0, // 默认不激活,除非通过 setDefault
|
||||
Status: int(in.Status),
|
||||
Remark: in.Remark,
|
||||
}
|
||||
|
||||
err := l.svcCtx.DB.Create(conf).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &file.CommonResp{Code: 0, Msg: "创建成功"}, nil
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
)
|
||||
|
||||
type DeleteFilesLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewDeleteFilesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteFilesLogic {
|
||||
return &DeleteFilesLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 删除文件记录
|
||||
func (l *DeleteFilesLogic) DeleteFiles(in *file.DeleteFilesReq) (*file.CommonResp, error) {
|
||||
if len(in.Ids) == 0 {
|
||||
return &file.CommonResp{Code: 400, Msg: "ids不能为空"}, nil
|
||||
}
|
||||
|
||||
// 这里的删除只删数据库记录,真实文件的删除由 file-api 在调用这个 RPC 之前处理
|
||||
err := l.svcCtx.DB.Where("id IN ?", in.Ids).Delete(&model.SundynixOss{}).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &file.CommonResp{Code: 0, Msg: "删除成功"}, nil
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
)
|
||||
|
||||
type DeleteStorageConfigLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewDeleteStorageConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteStorageConfigLogic {
|
||||
return &DeleteStorageConfigLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DeleteStorageConfigLogic) DeleteStorageConfig(in *file.DeleteFilesReq) (*file.CommonResp, error) {
|
||||
if len(in.Ids) == 0 {
|
||||
return &file.CommonResp{Code: 400, Msg: "ids不能为空"}, nil
|
||||
}
|
||||
err := l.svcCtx.DB.Where("id IN ?", in.Ids).Delete(&model.StorageConfig{}).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &file.CommonResp{Code: 0, Msg: "删除成功"}, nil
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
)
|
||||
|
||||
type GetDefaultStorageConfigLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetDefaultStorageConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDefaultStorageConfigLogic {
|
||||
return &GetDefaultStorageConfigLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetDefaultStorageConfigLogic) GetDefaultStorageConfig(in *file.GetDefaultStorageConfigReq) (*file.GetDefaultStorageConfigResp, error) {
|
||||
conf := &model.StorageConfig{}
|
||||
err := l.svcCtx.DB.Where("is_default = ?", 1).First(conf).Error
|
||||
if err != nil {
|
||||
return nil, err // let the factory handle the error
|
||||
}
|
||||
|
||||
return &file.GetDefaultStorageConfigResp{
|
||||
Config: &file.StorageConfigInfo{
|
||||
Id: conf.ID,
|
||||
Type: conf.Type,
|
||||
Name: conf.Name,
|
||||
Endpoint: conf.Endpoint,
|
||||
AccessKeyId: conf.AccessKeyId,
|
||||
AccessKeySecret: conf.AccessKeySecret,
|
||||
BucketName: conf.BucketName,
|
||||
BucketUrl: conf.BucketUrl,
|
||||
Region: conf.Region,
|
||||
IsDefault: int32(conf.IsDefault),
|
||||
Status: int32(conf.Status),
|
||||
Remark: conf.Remark,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -3,14 +3,13 @@ package logic
|
||||
import (
|
||||
"context"
|
||||
|
||||
fileModel "sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gorm.io/gorm"
|
||||
"sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
)
|
||||
|
||||
type GetFileByIdLogic struct {
|
||||
@@ -29,7 +28,7 @@ func NewGetFileByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFi
|
||||
|
||||
// 根据ID获取文件信息
|
||||
func (l *GetFileByIdLogic) GetFileById(in *file.GetFileByIdReq) (*file.GetFileByIdResp, error) {
|
||||
var oss fileModel.SundynixOss
|
||||
var oss model.SundynixOss
|
||||
err := l.svcCtx.DB.Where("id = ?", in.Id).First(&oss).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
@@ -44,7 +43,7 @@ func (l *GetFileByIdLogic) GetFileById(in *file.GetFileByIdReq) (*file.GetFileBy
|
||||
}, nil
|
||||
}
|
||||
|
||||
func convertOssToProto(oss *fileModel.SundynixOss) *file.FileInfo {
|
||||
func convertOssToProto(oss *model.SundynixOss) *file.FileInfo {
|
||||
return &file.FileInfo{
|
||||
Id: oss.ID,
|
||||
Name: oss.Name,
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
)
|
||||
|
||||
type GetFileListLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetFileListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFileListLogic {
|
||||
return &GetFileListLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 获取文件列表
|
||||
func (l *GetFileListLogic) GetFileList(in *file.GetFileListReq) (*file.GetFileListResp, error) {
|
||||
var list []model.SundynixOss
|
||||
var total int64
|
||||
|
||||
db := l.svcCtx.DB.Model(&model.SundynixOss{})
|
||||
if in.Name != "" {
|
||||
db = db.Where("name LIKE ?", "%"+in.Name+"%")
|
||||
}
|
||||
|
||||
db.Count(&total)
|
||||
|
||||
if in.Current > 0 && in.PageSize > 0 {
|
||||
offset := (in.Current - 1) * in.PageSize
|
||||
db = db.Offset(int(offset)).Limit(int(in.PageSize))
|
||||
}
|
||||
|
||||
if err := db.Find(&list).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var respList []*file.FileInfo
|
||||
for _, item := range list {
|
||||
respList = append(respList, &file.FileInfo{
|
||||
Id: item.ID,
|
||||
Name: item.Name,
|
||||
Url: item.Url,
|
||||
Tag: item.Tag,
|
||||
Key: item.Key,
|
||||
Suffix: item.Suffix,
|
||||
Md5: item.MD5,
|
||||
CreatedAt: item.CreatedAt.Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
return &file.GetFileListResp{
|
||||
List: respList,
|
||||
Total: total,
|
||||
}, nil
|
||||
}
|
||||
@@ -3,13 +3,12 @@ package logic
|
||||
import (
|
||||
"context"
|
||||
|
||||
fileModel "sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
)
|
||||
|
||||
type GetFilesByIdsLogic struct {
|
||||
@@ -32,7 +31,7 @@ func (l *GetFilesByIdsLogic) GetFilesByIds(in *file.GetFilesByIdsReq) (*file.Get
|
||||
return &file.GetFilesByIdsResp{Files: []*file.FileInfo{}}, nil
|
||||
}
|
||||
|
||||
var ossList []fileModel.SundynixOss
|
||||
var ossList []model.SundynixOss
|
||||
err := l.svcCtx.DB.Where("id IN ?", in.Ids).Find(&ossList).Error
|
||||
if err != nil {
|
||||
l.Errorf("批量查询文件失败: %v", err)
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
)
|
||||
|
||||
type GetStorageConfigListLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetStorageConfigListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetStorageConfigListLogic {
|
||||
return &GetStorageConfigListLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetStorageConfigListLogic) GetStorageConfigList(in *file.StorageConfigListReq) (*file.StorageConfigListResp, error) {
|
||||
var list []model.StorageConfig
|
||||
var total int64
|
||||
|
||||
db := l.svcCtx.DB.Model(&model.StorageConfig{})
|
||||
if in.Type != "" {
|
||||
db = db.Where("type = ?", in.Type)
|
||||
}
|
||||
if in.Name != "" {
|
||||
db = db.Where("name LIKE ?", "%"+in.Name+"%")
|
||||
}
|
||||
|
||||
db.Count(&total)
|
||||
|
||||
if in.Current > 0 && in.PageSize > 0 {
|
||||
offset := (in.Current - 1) * in.PageSize
|
||||
db = db.Offset(int(offset)).Limit(int(in.PageSize))
|
||||
}
|
||||
|
||||
if err := db.Find(&list).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var respList []*file.StorageConfigInfo
|
||||
for _, item := range list {
|
||||
respList = append(respList, &file.StorageConfigInfo{
|
||||
Id: item.ID,
|
||||
Type: item.Type,
|
||||
Name: item.Name,
|
||||
Endpoint: item.Endpoint,
|
||||
AccessKeyId: item.AccessKeyId,
|
||||
AccessKeySecret: item.AccessKeySecret,
|
||||
BucketName: item.BucketName,
|
||||
BucketUrl: item.BucketUrl,
|
||||
Region: item.Region,
|
||||
IsDefault: int32(item.IsDefault),
|
||||
Status: int32(item.Status),
|
||||
Remark: item.Remark,
|
||||
})
|
||||
}
|
||||
|
||||
return &file.StorageConfigListResp{
|
||||
List: respList,
|
||||
Total: total,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
)
|
||||
|
||||
type SetDefaultStorageConfigLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewSetDefaultStorageConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetDefaultStorageConfigLogic {
|
||||
return &SetDefaultStorageConfigLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SetDefaultStorageConfigLogic) SetDefaultStorageConfig(in *file.SetDefaultStorageConfigReq) (*file.CommonResp, error) {
|
||||
tx := l.svcCtx.DB.Begin()
|
||||
if err := tx.Model(&model.StorageConfig{}).Where("1 = 1").Update("is_default", 0).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
if err := tx.Model(&model.StorageConfig{}).Where("id = ?", in.Id).Update("is_default", 1).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
return &file.CommonResp{Code: 0, Msg: "设置成功"}, nil
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/file/model"
|
||||
"sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/file/rpc/internal/svc"
|
||||
)
|
||||
|
||||
type UpdateStorageConfigLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewUpdateStorageConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateStorageConfigLogic {
|
||||
return &UpdateStorageConfigLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateStorageConfigLogic) UpdateStorageConfig(in *file.UpdateStorageConfigReq) (*file.CommonResp, error) {
|
||||
conf := &model.StorageConfig{}
|
||||
if err := l.svcCtx.DB.Where("id = ?", in.Id).First(conf).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
conf.Type = in.Type
|
||||
conf.Name = in.Name
|
||||
conf.Endpoint = in.Endpoint
|
||||
conf.AccessKeyId = in.AccessKeyId
|
||||
conf.AccessKeySecret = in.AccessKeySecret
|
||||
conf.BucketName = in.BucketName
|
||||
conf.BucketUrl = in.BucketUrl
|
||||
conf.Region = in.Region
|
||||
conf.Status = int(in.Status)
|
||||
conf.Remark = in.Remark
|
||||
|
||||
if err := l.svcCtx.DB.Save(conf).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &file.CommonResp{Code: 0, Msg: "更新成功"}, nil
|
||||
}
|
||||
@@ -34,3 +34,58 @@ func (s *FileServiceServer) GetFilesByIds(ctx context.Context, in *file.GetFiles
|
||||
l := logic.NewGetFilesByIdsLogic(ctx, s.svcCtx)
|
||||
return l.GetFilesByIds(in)
|
||||
}
|
||||
|
||||
// 创建文件记录
|
||||
func (s *FileServiceServer) CreateFile(ctx context.Context, in *file.CreateFileReq) (*file.CreateFileResp, error) {
|
||||
l := logic.NewCreateFileLogic(ctx, s.svcCtx)
|
||||
return l.CreateFile(in)
|
||||
}
|
||||
|
||||
// 通过MD5检查文件是否存在
|
||||
func (s *FileServiceServer) CheckFileByMd5(ctx context.Context, in *file.CheckFileByMd5Req) (*file.CheckFileByMd5Resp, error) {
|
||||
l := logic.NewCheckFileByMd5Logic(ctx, s.svcCtx)
|
||||
return l.CheckFileByMd5(in)
|
||||
}
|
||||
|
||||
// 删除文件记录
|
||||
func (s *FileServiceServer) DeleteFiles(ctx context.Context, in *file.DeleteFilesReq) (*file.CommonResp, error) {
|
||||
l := logic.NewDeleteFilesLogic(ctx, s.svcCtx)
|
||||
return l.DeleteFiles(in)
|
||||
}
|
||||
|
||||
// 获取文件列表
|
||||
func (s *FileServiceServer) GetFileList(ctx context.Context, in *file.GetFileListReq) (*file.GetFileListResp, error) {
|
||||
l := logic.NewGetFileListLogic(ctx, s.svcCtx)
|
||||
return l.GetFileList(in)
|
||||
}
|
||||
|
||||
// ---------- 存储配置 ----------
|
||||
func (s *FileServiceServer) CreateStorageConfig(ctx context.Context, in *file.CreateStorageConfigReq) (*file.CommonResp, error) {
|
||||
l := logic.NewCreateStorageConfigLogic(ctx, s.svcCtx)
|
||||
return l.CreateStorageConfig(in)
|
||||
}
|
||||
|
||||
func (s *FileServiceServer) UpdateStorageConfig(ctx context.Context, in *file.UpdateStorageConfigReq) (*file.CommonResp, error) {
|
||||
l := logic.NewUpdateStorageConfigLogic(ctx, s.svcCtx)
|
||||
return l.UpdateStorageConfig(in)
|
||||
}
|
||||
|
||||
func (s *FileServiceServer) DeleteStorageConfig(ctx context.Context, in *file.DeleteFilesReq) (*file.CommonResp, error) {
|
||||
l := logic.NewDeleteStorageConfigLogic(ctx, s.svcCtx)
|
||||
return l.DeleteStorageConfig(in)
|
||||
}
|
||||
|
||||
func (s *FileServiceServer) SetDefaultStorageConfig(ctx context.Context, in *file.SetDefaultStorageConfigReq) (*file.CommonResp, error) {
|
||||
l := logic.NewSetDefaultStorageConfigLogic(ctx, s.svcCtx)
|
||||
return l.SetDefaultStorageConfig(in)
|
||||
}
|
||||
|
||||
func (s *FileServiceServer) GetStorageConfigList(ctx context.Context, in *file.StorageConfigListReq) (*file.StorageConfigListResp, error) {
|
||||
l := logic.NewGetStorageConfigListLogic(ctx, s.svcCtx)
|
||||
return l.GetStorageConfigList(in)
|
||||
}
|
||||
|
||||
func (s *FileServiceServer) GetDefaultStorageConfig(ctx context.Context, in *file.GetDefaultStorageConfigReq) (*file.GetDefaultStorageConfigResp, error) {
|
||||
l := logic.NewGetDefaultStorageConfigLogic(ctx, s.svcCtx)
|
||||
return l.GetDefaultStorageConfig(in)
|
||||
}
|
||||
|
||||
@@ -25,7 +25,10 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
panic(err)
|
||||
}
|
||||
// 自动迁移
|
||||
if err := db.AutoMigrate(&fileModel.SundynixOss{}); err != nil {
|
||||
if err := db.AutoMigrate(
|
||||
&fileModel.SundynixOss{},
|
||||
&fileModel.StorageConfig{},
|
||||
); err != nil {
|
||||
logx.Errorf("数据库迁移失败: %v", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,108 @@ message GetFilesByIdsResp {
|
||||
repeated FileInfo files = 1;
|
||||
}
|
||||
|
||||
// ---------- 存储配置 ----------
|
||||
|
||||
message StorageConfigInfo {
|
||||
string id = 1;
|
||||
string type = 2;
|
||||
string name = 3;
|
||||
string endpoint = 4;
|
||||
string accessKeyId = 5;
|
||||
string accessKeySecret = 6;
|
||||
string bucketName = 7;
|
||||
string bucketUrl = 8;
|
||||
string region = 9;
|
||||
int32 isDefault = 10;
|
||||
int32 status = 11;
|
||||
string remark = 12;
|
||||
}
|
||||
|
||||
message CreateStorageConfigReq {
|
||||
string type = 1;
|
||||
string name = 2;
|
||||
string endpoint = 3;
|
||||
string accessKeyId = 4;
|
||||
string accessKeySecret = 5;
|
||||
string bucketName = 6;
|
||||
string bucketUrl = 7;
|
||||
string region = 8;
|
||||
int32 status = 9;
|
||||
string remark = 10;
|
||||
}
|
||||
|
||||
message UpdateStorageConfigReq {
|
||||
string id = 1;
|
||||
string type = 2;
|
||||
string name = 3;
|
||||
string endpoint = 4;
|
||||
string accessKeyId = 5;
|
||||
string accessKeySecret = 6;
|
||||
string bucketName = 7;
|
||||
string bucketUrl = 8;
|
||||
string region = 9;
|
||||
int32 status = 10;
|
||||
string remark = 11;
|
||||
}
|
||||
|
||||
message SetDefaultStorageConfigReq {
|
||||
string id = 1;
|
||||
}
|
||||
|
||||
message StorageConfigListReq {
|
||||
int32 current = 1;
|
||||
int32 pageSize = 2;
|
||||
string type = 3;
|
||||
string name = 4;
|
||||
}
|
||||
|
||||
message StorageConfigListResp {
|
||||
repeated StorageConfigInfo list = 1;
|
||||
int64 total = 2;
|
||||
}
|
||||
|
||||
message GetDefaultStorageConfigReq {}
|
||||
message GetDefaultStorageConfigResp {
|
||||
StorageConfigInfo config = 1;
|
||||
}
|
||||
|
||||
message CreateFileReq {
|
||||
string name = 1;
|
||||
string url = 2;
|
||||
string tag = 3;
|
||||
string key = 4;
|
||||
string suffix = 5;
|
||||
string md5 = 6;
|
||||
}
|
||||
|
||||
message CreateFileResp {
|
||||
FileInfo file = 1;
|
||||
}
|
||||
|
||||
message CheckFileByMd5Req {
|
||||
string md5 = 1;
|
||||
}
|
||||
|
||||
message CheckFileByMd5Resp {
|
||||
bool exists = 1;
|
||||
FileInfo file = 2;
|
||||
}
|
||||
|
||||
message DeleteFilesReq {
|
||||
repeated string ids = 1;
|
||||
}
|
||||
|
||||
message GetFileListReq {
|
||||
int32 current = 1;
|
||||
int32 pageSize = 2;
|
||||
string name = 3;
|
||||
}
|
||||
|
||||
message GetFileListResp {
|
||||
repeated FileInfo list = 1;
|
||||
int64 total = 2;
|
||||
}
|
||||
|
||||
// ---------- 服务定义 ----------
|
||||
|
||||
service FileService {
|
||||
@@ -49,4 +151,20 @@ service FileService {
|
||||
rpc GetFileById(GetFileByIdReq) returns (GetFileByIdResp);
|
||||
// 根据ID列表批量获取文件信息
|
||||
rpc GetFilesByIds(GetFilesByIdsReq) returns (GetFilesByIdsResp);
|
||||
// 创建文件记录
|
||||
rpc CreateFile(CreateFileReq) returns (CreateFileResp);
|
||||
// 通过MD5检查文件是否存在
|
||||
rpc CheckFileByMd5(CheckFileByMd5Req) returns (CheckFileByMd5Resp);
|
||||
// 删除文件记录
|
||||
rpc DeleteFiles(DeleteFilesReq) returns (CommonResp);
|
||||
// 获取文件列表
|
||||
rpc GetFileList(GetFileListReq) returns (GetFileListResp);
|
||||
|
||||
// ---------- 存储配置 ----------
|
||||
rpc CreateStorageConfig(CreateStorageConfigReq) returns (CommonResp);
|
||||
rpc UpdateStorageConfig(UpdateStorageConfigReq) returns (CommonResp);
|
||||
rpc DeleteStorageConfig(DeleteFilesReq) returns (CommonResp); // 复用idsReq
|
||||
rpc SetDefaultStorageConfig(SetDefaultStorageConfigReq) returns (CommonResp);
|
||||
rpc GetStorageConfigList(StorageConfigListReq) returns (StorageConfigListResp);
|
||||
rpc GetDefaultStorageConfig(GetDefaultStorageConfigReq) returns (GetDefaultStorageConfigResp);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user