mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-02-02 18:28:41 +08:00
Merge pull request #105 from GreatSir/v2.0
fix multipart upload merge file
This commit is contained in:
commit
bbca0e8db8
@ -15,6 +15,8 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -139,7 +141,11 @@ func MergePartFile(srcPath, dstPath string) (err error) {
|
||||
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
|
||||
})
|
||||
for _, file := range dir {
|
||||
filePath := filepath.Join(srcPath, file.Name())
|
||||
if err = gfile.PutBytesAppend(dstPath, gfile.GetBytes(filePath)); err != nil {
|
||||
|
61
server/internal/library/storager/upload_local_test.go
Normal file
61
server/internal/library/storager/upload_local_test.go
Normal file
@ -0,0 +1,61 @@
|
||||
package storager
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMergePartFile(t *testing.T) {
|
||||
srcFile, _ := os.Open("1.zip")
|
||||
defer srcFile.Close()
|
||||
fileInfo, err := srcFile.Stat()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
hash := md5.New()
|
||||
_, err = io.Copy(hash, srcFile)
|
||||
sum := hash.Sum(nil)
|
||||
fileHash := fmt.Sprintf("%x", sum)
|
||||
|
||||
var singleSize = int64(1024 * 1024 * 100)
|
||||
chunks := int(fileInfo.Size() / singleSize)
|
||||
if fileInfo.Size()%singleSize != 0 {
|
||||
chunks += 1
|
||||
}
|
||||
srcFile.Seek(0, io.SeekStart)
|
||||
for j := 0; j < chunks; j++ {
|
||||
partSize := singleSize
|
||||
if j == chunks-1 {
|
||||
partSize = fileInfo.Size() - int64(j)*singleSize
|
||||
}
|
||||
partData := make([]byte, partSize)
|
||||
_, err = io.ReadFull(srcFile, partData)
|
||||
pf, _ := os.Create(fmt.Sprintf("tmp/%d", j+1))
|
||||
_, err = pf.Write(partData)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
pf.Close()
|
||||
}
|
||||
|
||||
err = MergePartFile("tmp/", "2.zip")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
f2, _ := os.Open("2.zip")
|
||||
hash2 := md5.New()
|
||||
_, err = io.Copy(hash2, f2)
|
||||
sum2 := hash.Sum(nil)
|
||||
fileHash2 := fmt.Sprintf("%x", sum2)
|
||||
if fileHash != fileHash2 {
|
||||
t.Error(errors.New("hash mismatch"))
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user