Finish the download archive feature.

This commit is contained in:
zicla
2019-05-01 19:53:20 +08:00
parent adf4b9ea5a
commit 5dd0fec953
26 changed files with 492 additions and 255 deletions

View File

@ -67,7 +67,7 @@ func GetHomePath() string {
exPath = GetDevHomePath() + "/tmp"
}
return exPath
return UniformPath(exPath)
}
//获取前端静态资源的位置。如果你在开发模式下可以将这里直接返回tank/build下面的html路径。
@ -254,3 +254,10 @@ func CopyFile(srcPath string, destPath string) (nBytes int64) {
nBytes, err = io.Copy(destFile, srcFile)
return nBytes
}
//路径归一化处理,把\\替换成/
func UniformPath(path string) string {
return strings.Replace(path, "\\", "/", -1)
}

View File

@ -1,21 +0,0 @@
package util
//带有panic恢复的方法
func RunWithRecovery(f func()) {
defer func() {
if err := recover(); err != nil {
//TODO 全局日志记录
//LOGGER.Error("异步任务错误: %v", err)
}
}()
//执行函数
f()
}
//处理错误的统一方法 可以省去if err!=nil 这段代码
func PanicError(err error) {
if err != nil {
panic(err)
}
}

View File

@ -11,10 +11,10 @@ import (
)
//将srcPath压缩到destPath。
func Zip(srcPath string, destPath string) {
func Zip(srcPath string, destPath string) error {
//统一处理\\成/
srcPath = strings.Replace(srcPath, "\\", "/", -1)
srcPath = UniformPath(srcPath)
if PathExists(destPath) {
panic(result.BadRequest("%s 已经存在了", destPath))
@ -22,10 +22,14 @@ func Zip(srcPath string, destPath string) {
// 创建准备写入的文件
fileWriter, err := os.Create(destPath)
PanicError(err)
if err != nil {
return err
}
defer func() {
err := fileWriter.Close()
PanicError(err)
if err != nil {
panic(err)
}
}()
// 通过 fileWriter 来创建 zip.Write
@ -46,7 +50,7 @@ func Zip(srcPath string, destPath string) {
}
//统一处理\\成/
path = strings.Replace(path, "\\", "/", -1)
path = UniformPath(path)
// 通过文件信息,创建 zip 的文件信息
fileHeader, err := zip.FileInfoHeader(fileInfo)
@ -78,7 +82,9 @@ func Zip(srcPath string, destPath string) {
fileToBeZip, err := os.Open(path)
defer func() {
err = fileToBeZip.Close()
PanicError(err)
if err != nil {
panic(err)
}
}()
if err != nil {
return
@ -92,5 +98,5 @@ func Zip(srcPath string, destPath string) {
return nil
})
PanicError(err)
return err
}