Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
csznet committed Nov 6, 2023
1 parent 465744c commit fb5aed7
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 121 deletions.
3 changes: 0 additions & 3 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type File struct {
FileName string
FilePath string
IsZip bool
IsThumb bool
}

type Dir struct {
Expand All @@ -21,5 +20,3 @@ var (
GoFilePort string
GoFile string
)
var GoCacheOption = false
var GoCachePath = "/var/tmp/goFile/"
18 changes: 1 addition & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func web() {
ok := true
file := filepath.Join(conf.GoFile+c.PostForm("path"), c.PostForm("filename"))
//判断文件是否存在
if _, err := os.Stat(file); !os.IsNotExist(err) {
if !utils.Exist(file) {
ok = false
}
f, err := os.Create(file)
Expand Down Expand Up @@ -228,18 +228,6 @@ func web() {
"stat": Stat,
})
})
if conf.GoCacheOption {
//查看图片缩略图 Thumb
r.GET("/thumb/*path", func(c *gin.Context) {
cPath := strings.Replace(c.Param("path"), "/", "", 1)
if utils.GetImgThumb(conf.GoFile+cPath, conf.GoCachePath) {
c.File(utils.RemovePP(conf.GoCachePath + conf.GoFile + cPath))
} else {
c.Status(http.StatusInternalServerError) // 设置HTTP状态码为500
c.String(http.StatusInternalServerError, "Internal Server Error") // 返回错误信息
}
})
}
}
//监听端口默认为8080
r.Run("0.0.0.0:" + conf.GoFilePort)
Expand All @@ -249,14 +237,10 @@ func init() {
flag.StringVar(&conf.GoFile, "path", "./", "goFile path")
flag.StringVar(&conf.GoFilePort, "port", "8089", "goFile web port")
readerPtr := flag.Bool("r", false, "Enable reader")
cachePtr := flag.Bool("t", false, "Enable Thumb")
flag.Parse()
if *readerPtr {
reader = true
}
if *cachePtr {
conf.GoCacheOption = true
}
}
func main() {
// 获取当前工作目录
Expand Down
103 changes: 2 additions & 101 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"archive/zip"
"bytes"
"fmt"
"goFile/conf"
"io"
Expand All @@ -11,8 +10,6 @@ import (
"path/filepath"
"sort"
"strings"

"github.com/disintegration/imaging"
)

// ReadFile read file
Expand Down Expand Up @@ -43,101 +40,10 @@ func ReadFile(path string) ([]byte, bool) {
}
return freeBytes, true
}
func ExistFile(filePath string) bool {
// 获取文件信息
_, err := os.Stat(filePath)
// 判断文件是否存在
if os.IsNotExist(err) {
return false
} else if err != nil {
fmt.Println("Error:", err)
return false
} else {
return true
}
}

// NewImgThumb 生成Thumb
func NewImgThumb(fileContent []byte, thumbnailPath string) bool {
buf := bytes.NewBuffer(fileContent)
image, err := imaging.Decode(buf)
if err != nil {
fmt.Println(err)
return false
}
thumbnail := imaging.Resize(image, 400, 0, imaging.Lanczos)
// 获取文件夹路径
thumbnailDir := filepath.Dir(thumbnailPath)

// 检查文件夹是否存在,如果不存在则创建
if _, err := os.Stat(thumbnailDir); os.IsNotExist(err) {
err := os.MkdirAll(thumbnailDir, os.ModePerm)
if err != nil {
fmt.Println("Error creating directory:", err)
return false
}
}
err = imaging.Save(thumbnail, thumbnailPath)
if err != nil {
fmt.Println(err)
return false
}
return true
}
func RemovePP(path string) string {
return strings.ReplaceAll(path, "//", "/")
}
func GetImgThumb(path, goCachePath string) bool {
fileContent, getImgStatus := ReadFile(path)
if getImgStatus {
// //判断缓存文件夹是否存在(如果是默认文件夹则自动新建
// if goCachePath == "/var/tmp/goFile/" {
// _, err := os.Stat(goCachePath)
// if os.IsNotExist(err) {
// // 文件夹不存在,创建它
// err := os.Mkdir(goCachePath, 0755) // 0755 是文件夹权限
// if err != nil {
// fmt.Println("Failed to create folder:", err)
// return false
// }
// } else if err != nil {
// // 其他错误
// fmt.Println("Error:", err)
// return false
// }
// }
thumbnailPath := RemovePP(goCachePath + path)
//判断thumb文件是否存在
if ExistFile(thumbnailPath) {
// 获取thumb文件信息
fileInfo, err := os.Stat(thumbnailPath)
if err != nil {
fmt.Println("Error:", err)
return false
}
// 获取文件最后修改时间
thumbModTime := fileInfo.ModTime()
fileInfo, err = os.Stat(path)
if err != nil {
fmt.Println("Error:", err)
return false
}
//获取源文件最后修改时间
modTime := fileInfo.ModTime()
if thumbModTime.After(modTime) {
//thumb是在源文件后建立的
fmt.Println("thumbDate:", thumbModTime)
fmt.Println(modTime)
return true
} else {
return NewImgThumb(fileContent, thumbnailPath)
}
}
return NewImgThumb(fileContent, thumbnailPath)
} else {
return false
}
}

// Unzip 解压zip
func Unzip(src string) bool {
Expand Down Expand Up @@ -208,7 +114,7 @@ func GetFile(url, path string) bool {
}

// Exists 判断是否存在
func exists(path string) bool {
func Exist(path string) bool {
_, err := os.Stat(path) //os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
Expand All @@ -235,10 +141,9 @@ func GetFiles(path string) conf.Info {
getFile, _ := filepath.Glob(path)
var info conf.Info
ZipList := []string{"zip", "gz"}
ImgList := []string{"jpg", "png"}
for i := 0; i < len(getFile); i++ {
im := getFile[i]
if exists(im) {
if Exist(im) {
s, _ := os.Stat(im)
if s.IsDir() {
var dir conf.Dir
Expand All @@ -264,10 +169,6 @@ func GetFiles(path string) conf.Info {
if In(strSplit[len(strSplit)-1], ZipList) {
file.IsZip = true
}
file.IsThumb = false
if In(strSplit[len(strSplit)-1], ImgList) && conf.GoCacheOption {
file.IsThumb = true
}
//file.FilePath = NewPath + im
info.Files = append(info.Files, file)
}
Expand Down

0 comments on commit fb5aed7

Please sign in to comment.