diff --git a/server/service/exa_simple_uploader.go b/server/service/exa_simple_uploader.go index 5ce57507..eadfc901 100644 --- a/server/service/exa_simple_uploader.go +++ b/server/service/exa_simple_uploader.go @@ -51,7 +51,12 @@ func MergeFileMd5(md5 string, fileName string) (err error) { rd, err := ioutil.ReadDir(dir) _ = 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 { 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) } } - //关闭文件 - defer fd.Close() if err != nil { return err diff --git a/server/utils/breakpoint_continue.go b/server/utils/breakpoint_continue.go index 7dbe7eb6..342517ef 100644 --- a/server/utils/breakpoint_continue.go +++ b/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) { path := FileDir + fileName + "_" + strconv.Itoa(contentNumber) f, err := os.Create(path) - defer f.Close() if err != nil { return err, path } else { @@ -64,6 +63,7 @@ func makeFileContent(content []byte, fileName string, FileDir string, contentNum return err, path } } + defer f.Close() return nil, path } @@ -79,7 +79,11 @@ func MakeFile(fileName string, FileMd5 string) (error, string) { return err, finishDir + fileName } _ = 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 { content, _ := ioutil.ReadFile(breakpointDir + FileMd5 + "/" + fileName + "_" + strconv.Itoa(k)) _, err = fd.Write(content) @@ -88,7 +92,7 @@ func MakeFile(fileName string, FileMd5 string) (error, string) { return err, finishDir + fileName } } - defer fd.Close() + return nil, finishDir + fileName } diff --git a/server/utils/zipfiles.go b/server/utils/zipfiles.go index a14b0e4c..9798c85e 100644 --- a/server/utils/zipfiles.go +++ b/server/utils/zipfiles.go @@ -31,14 +31,12 @@ func ZipFiles(filename string, files []string, oldForm, newForm string) error { // 把files添加到zip中 for _, file := range files { - zipFile, err := os.Open(file) - if err != nil { - return err - } - err = func() error { - defer func() { - _ = zipFile.Close() - }() + err = func(file string) error { + zipFile, err := os.Open(file) + if err != nil { + return err + } + defer zipFile.Close() // 获取file的基础信息 info, err := zipFile.Stat() if err != nil { @@ -50,8 +48,8 @@ func ZipFiles(filename string, files []string, oldForm, newForm string) error { 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 @@ -65,7 +63,7 @@ func ZipFiles(filename string, files []string, oldForm, newForm string) error { return err } return nil - }() + }(file) if err != nil { return err }