mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 16:00:25 +08:00
feat: 新增统一文件模块,统一文件操作
This commit is contained in:
@@ -2,15 +2,11 @@ package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"mayfly-go/internal/db/config"
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/internal/db/domain/repository"
|
||||
fileapp "mayfly-go/internal/file/application"
|
||||
"mayfly-go/pkg/base"
|
||||
"mayfly-go/pkg/model"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type DbTransferFile interface {
|
||||
@@ -22,14 +18,14 @@ type DbTransferFile interface {
|
||||
Save(ctx context.Context, instanceEntity *entity.DbTransferFile) error
|
||||
|
||||
Delete(ctx context.Context, id ...uint64) error
|
||||
|
||||
GetFilePath(ent *entity.DbTransferFile) string
|
||||
}
|
||||
|
||||
var _ DbTransferFile = (*dbTransferFileAppImpl)(nil)
|
||||
|
||||
type dbTransferFileAppImpl struct {
|
||||
base.AppImpl[*entity.DbTransferFile, repository.DbTransferFile]
|
||||
|
||||
fileApp fileapp.File `inject:"FileApp"`
|
||||
}
|
||||
|
||||
func (app *dbTransferFileAppImpl) InjectDbTransferFileRepo(repo repository.DbTransferFile) {
|
||||
@@ -51,28 +47,16 @@ func (app *dbTransferFileAppImpl) Save(ctx context.Context, taskEntity *entity.D
|
||||
}
|
||||
|
||||
func (app *dbTransferFileAppImpl) Delete(ctx context.Context, id ...uint64) error {
|
||||
|
||||
arr, err := app.GetByIds(id, "task_id", "file_uuid")
|
||||
arr, err := app.GetByIds(id, "task_id", "file_key")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 删除对应的文件
|
||||
for _, file := range arr {
|
||||
_ = os.Remove(app.GetFilePath(file))
|
||||
_ = app.fileApp.Remove(ctx, file.FileKey)
|
||||
}
|
||||
|
||||
// 删除数据
|
||||
return app.DeleteById(ctx, id...)
|
||||
}
|
||||
|
||||
func (app *dbTransferFileAppImpl) GetFilePath(ent *entity.DbTransferFile) string {
|
||||
brc := config.GetDbBackupRestore()
|
||||
if ent.FileUuid == "" {
|
||||
ent.FileUuid = uuid.New().String()
|
||||
}
|
||||
|
||||
filePath := filepath.Join(fmt.Sprintf("%s/%d/%s.sql", brc.TransferPath, ent.TaskId, ent.FileUuid))
|
||||
|
||||
return filePath
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user