Browse Source

Merge pull request #290 from songzhibin97/develop

优化基础库的一些错误处理
main
奇淼(piexlmax 4 years ago
committed by GitHub
parent
commit
1260af39b0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      server/service/exa_simple_uploader.go
  2. 10
      server/utils/breakpoint_continue.go
  3. 12
      server/utils/zipfiles.go

9
server/service/exa_simple_uploader.go

@ -51,7 +51,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))
@ -60,8 +65,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

@ -55,7 +55,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 {
@ -64,6 +63,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
} }
@ -79,7 +79,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)
@ -88,7 +92,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
} }

12
server/utils/zipfiles.go

@ -31,14 +31,12 @@ func ZipFiles(filename string, files []string, oldForm, newForm string) error {
// 把files添加到zip中 // 把files添加到zip中
for _, file := range files { for _, file := range files {
err = func(file string) error {
zipFile, err := os.Open(file) zipFile, err := os.Open(file)
if err != nil { if err != nil {
return err return err
} }
err = func() error {
defer func() {
_ = zipFile.Close()
}()
defer zipFile.Close()
// 获取file的基础信息 // 获取file的基础信息
info, err := zipFile.Stat() info, err := zipFile.Stat()
if err != nil { if err != nil {
@ -50,8 +48,8 @@ func ZipFiles(filename string, files []string, oldForm, newForm string) error {
return err return err
} }
// 使用上面的FileInfoHeader() 就可以把文件保存的路径替换成我们自己想要的了,如下面
header.Name = strings.Replace(file, oldForm, newForm, -1)
// 使用上面的FileInforHeader() 就可以把文件保存的路径替换成我们自己想要的了,如下面
header.Name = strings.Replace(file, oldform, newform, -1)
// 优化压缩 // 优化压缩
// 更多参考see http://golang.org/pkg/archive/zip/#pkg-constants // 更多参考see http://golang.org/pkg/archive/zip/#pkg-constants
@ -65,7 +63,7 @@ func ZipFiles(filename string, files []string, oldForm, newForm string) error {
return err return err
} }
return nil return nil
}()
}(file)
if err != nil { if err != nil {
return err return err
} }

Loading…
Cancel
Save