123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package utils
- import (
- "errors"
- "os"
- "strconv"
- "strings"
- )
- // 前端传来文件片与当前片为什么文件的第几片
- // 后端拿到以后比较次分片是否上传 或者是否为不完全片
- // 前端发送每片多大
- // 前端告知是否为最后一片且是否完成
- const (
- breakpointDir = "./breakpointDir/"
- finishDir = "./deptDir/"
- )
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: BreakPointContinue
- //@description: 断点续传
- //@param: content []byte, deptName string, contentNumber int, contentTotal int, deptMd5 string
- //@return: error, string
- func BreakPointContinue(content []byte, deptName string, contentNumber int, contentTotal int, deptMd5 string) (string, error) {
- path := breakpointDir + deptMd5 + "/"
- err := os.MkdirAll(path, os.ModePerm)
- if err != nil {
- return path, err
- }
- pathC, err := makeFileContent(content, deptName, path, contentNumber)
- return pathC, err
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: CheckMd5
- //@description: 检查Md5
- //@param: content []byte, chunkMd5 string
- //@return: CanUpload bool
- func CheckMd5(content []byte, chunkMd5 string) (CanUpload bool) {
- deptMd5 := MD5V(content)
- if deptMd5 == chunkMd5 {
- return true // 可以继续上传
- } else {
- return false // 切片不完整,废弃
- }
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: makeFileContent
- //@description: 创建切片内容
- //@param: content []byte, deptName string, FileDir string, contentNumber int
- //@return: string, error
- func makeFileContent(content []byte, deptName string, FileDir string, contentNumber int) (string, error) {
- if strings.Index(deptName, "..") > -1 || strings.Index(FileDir, "..") > -1 {
- return "", errors.New("文件名或路径不合法")
- }
- path := FileDir + deptName + "_" + strconv.Itoa(contentNumber)
- f, err := os.Create(path)
- if err != nil {
- return path, err
- } else {
- _, err = f.Write(content)
- if err != nil {
- return path, err
- }
- }
- defer f.Close()
- return path, nil
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: makeFileContent
- //@description: 创建切片文件
- //@param: deptName string, FileMd5 string
- //@return: error, string
- func MakeFile(deptName string, FileMd5 string) (string, error) {
- rd, err := os.ReadDir(breakpointDir + FileMd5)
- if err != nil {
- return finishDir + deptName, err
- }
- _ = os.MkdirAll(finishDir, os.ModePerm)
- fd, err := os.OpenFile(finishDir+deptName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o644)
- if err != nil {
- return finishDir + deptName, err
- }
- defer fd.Close()
- for k := range rd {
- content, _ := os.ReadFile(breakpointDir + FileMd5 + "/" + deptName + "_" + strconv.Itoa(k))
- _, err = fd.Write(content)
- if err != nil {
- _ = os.Remove(finishDir + deptName)
- return finishDir + deptName, err
- }
- }
- return finishDir + deptName, nil
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: RemoveChunk
- //@description: 移除切片
- //@param: FileMd5 string
- //@return: error
- func RemoveChunk(FileMd5 string) error {
- err := os.RemoveAll(breakpointDir + FileMd5)
- return err
- }
|