Browse Source

优化基础库的一些错误处理

main
songzhibin97 4 years ago
parent
commit
e1dd5eb58e
  1. 9
      server/service/exa_simple_uploader.go
  2. 10
      server/utils/breakpoint_continue.go

9
server/service/exa_simple_uploader.go

@ -36,7 +36,12 @@ func MergeFileMd5(md5 string, fileName string) (err error) {
rd, err := ioutil.ReadDir(dir) rd, err := ioutil.ReadDir(dir)
_ = os.MkdirAll(finishDir, os.ModePerm) _ = os.MkdirAll(finishDir, os.ModePerm)
//创建目标文件 //创建目标文件
fd, _ := os.OpenFile(finishDir+fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
fd, err := os.OpenFile(finishDir+fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return
}
//关闭文件
defer fd.Close()
//将切片文件按照顺序写入 //将切片文件按照顺序写入
for k := range rd { for k := range rd {
content, _ := ioutil.ReadFile(dir + "/" + fileName + strconv.Itoa(k+1)) content, _ := ioutil.ReadFile(dir + "/" + fileName + strconv.Itoa(k+1))
@ -45,8 +50,6 @@ func MergeFileMd5(md5 string, fileName string) (err error) {
_ = os.Remove(finishDir + fileName) _ = os.Remove(finishDir + fileName)
} }
} }
//关闭文件
defer fd.Close()
if err != nil { if err != nil {
return err return err

10
server/utils/breakpoint_continue.go

@ -37,7 +37,6 @@ func CheckMd5(content []byte, chunkMd5 string) (CanUpload bool) {
func makeFileContent(content []byte, fileName string, FileDir string, contentNumber int) (error, string) { func makeFileContent(content []byte, fileName string, FileDir string, contentNumber int) (error, string) {
path := FileDir + fileName + "_" + strconv.Itoa(contentNumber) path := FileDir + fileName + "_" + strconv.Itoa(contentNumber)
f, err := os.Create(path) f, err := os.Create(path)
defer f.Close()
if err != nil { if err != nil {
return err, path return err, path
} else { } else {
@ -46,6 +45,7 @@ func makeFileContent(content []byte, fileName string, FileDir string, contentNum
return err, path return err, path
} }
} }
defer f.Close()
return nil, path return nil, path
} }
@ -55,7 +55,11 @@ func MakeFile(fileName string, FileMd5 string) (error, string) {
return err, finishDir + fileName return err, finishDir + fileName
} }
_ = os.MkdirAll(finishDir, os.ModePerm) _ = os.MkdirAll(finishDir, os.ModePerm)
fd, _ := os.OpenFile(finishDir+fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
fd, err := os.OpenFile(finishDir+fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return err, finishDir + fileName
}
defer fd.Close()
for k := range rd { for k := range rd {
content, _ := ioutil.ReadFile(breakpointDir + FileMd5 + "/" + fileName + "_" + strconv.Itoa(k)) content, _ := ioutil.ReadFile(breakpointDir + FileMd5 + "/" + fileName + "_" + strconv.Itoa(k))
_, err = fd.Write(content) _, err = fd.Write(content)
@ -64,7 +68,7 @@ func MakeFile(fileName string, FileMd5 string) (error, string) {
return err, finishDir + fileName return err, finishDir + fileName
} }
} }
defer fd.Close()
return nil, finishDir + fileName return nil, finishDir + fileName
} }

Loading…
Cancel
Save