chore: seed migration objects batch 5
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
@@ -137,20 +138,53 @@ func (d *LocalDrive) UploadPart(ctx context.Context, in *UploadPartParams) (res
|
||||
|
||||
// MergePartFile 合并分片文件
|
||||
func MergePartFile(srcPath, dstPath string) (err error) {
|
||||
dir, err := os.ReadDir(srcPath)
|
||||
entries, err := os.ReadDir(srcPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sort.Slice(dir, func(i, j int) bool {
|
||||
fiIndex, _ := strconv.Atoi(dir[i].Name())
|
||||
fjIndex, _ := strconv.Atoi(dir[j].Name())
|
||||
return fiIndex < fjIndex
|
||||
type filePart struct {
|
||||
index int
|
||||
path string
|
||||
}
|
||||
parts := make([]filePart, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
index, parseErr := strconv.Atoi(entry.Name())
|
||||
if parseErr != nil || index <= 0 {
|
||||
return gerror.Newf("无效的分片文件名:%s", entry.Name())
|
||||
}
|
||||
parts = append(parts, filePart{index: index, path: filepath.Join(srcPath, entry.Name())})
|
||||
}
|
||||
if len(parts) == 0 {
|
||||
return gerror.New("没有可合并的分片文件")
|
||||
}
|
||||
sort.Slice(parts, func(i, j int) bool {
|
||||
return parts[i].index < parts[j].index
|
||||
})
|
||||
for _, file := range dir {
|
||||
filePath := filepath.Join(srcPath, file.Name())
|
||||
if err = gfile.PutBytesAppend(dstPath, gfile.GetBytes(filePath)); err != nil {
|
||||
return err
|
||||
if err = os.MkdirAll(filepath.Dir(dstPath), 0o755); err != nil {
|
||||
return gerror.Wrap(err, "创建目标目录失败")
|
||||
}
|
||||
dstFile, err := os.OpenFile(dstPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "创建合并文件失败")
|
||||
}
|
||||
defer dstFile.Close()
|
||||
|
||||
for _, part := range parts {
|
||||
srcFile, openErr := os.Open(part.path)
|
||||
if openErr != nil {
|
||||
return gerror.Wrapf(openErr, "打开分片%d失败", part.index)
|
||||
}
|
||||
_, copyErr := io.Copy(dstFile, srcFile)
|
||||
closeErr := srcFile.Close()
|
||||
if copyErr != nil {
|
||||
return gerror.Wrapf(copyErr, "合并分片%d失败", part.index)
|
||||
}
|
||||
if closeErr != nil {
|
||||
return gerror.Wrapf(closeErr, "关闭分片%d失败", part.index)
|
||||
}
|
||||
}
|
||||
return
|
||||
return dstFile.Sync()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user