Skip to content

Commit fb5aed7

Browse files
committed
optimize
1 parent 465744c commit fb5aed7

File tree

3 files changed

+3
-121
lines changed

3 files changed

+3
-121
lines changed

conf/conf.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ type File struct {
99
FileName string
1010
FilePath string
1111
IsZip bool
12-
IsThumb bool
1312
}
1413

1514
type Dir struct {
@@ -21,5 +20,3 @@ var (
2120
GoFilePort string
2221
GoFile string
2322
)
24-
var GoCacheOption = false
25-
var GoCachePath = "/var/tmp/goFile/"

main.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func web() {
142142
ok := true
143143
file := filepath.Join(conf.GoFile+c.PostForm("path"), c.PostForm("filename"))
144144
//判断文件是否存在
145-
if _, err := os.Stat(file); !os.IsNotExist(err) {
145+
if !utils.Exist(file) {
146146
ok = false
147147
}
148148
f, err := os.Create(file)
@@ -228,18 +228,6 @@ func web() {
228228
"stat": Stat,
229229
})
230230
})
231-
if conf.GoCacheOption {
232-
//查看图片缩略图 Thumb
233-
r.GET("/thumb/*path", func(c *gin.Context) {
234-
cPath := strings.Replace(c.Param("path"), "/", "", 1)
235-
if utils.GetImgThumb(conf.GoFile+cPath, conf.GoCachePath) {
236-
c.File(utils.RemovePP(conf.GoCachePath + conf.GoFile + cPath))
237-
} else {
238-
c.Status(http.StatusInternalServerError) // 设置HTTP状态码为500
239-
c.String(http.StatusInternalServerError, "Internal Server Error") // 返回错误信息
240-
}
241-
})
242-
}
243231
}
244232
//监听端口默认为8080
245233
r.Run("0.0.0.0:" + conf.GoFilePort)
@@ -249,14 +237,10 @@ func init() {
249237
flag.StringVar(&conf.GoFile, "path", "./", "goFile path")
250238
flag.StringVar(&conf.GoFilePort, "port", "8089", "goFile web port")
251239
readerPtr := flag.Bool("r", false, "Enable reader")
252-
cachePtr := flag.Bool("t", false, "Enable Thumb")
253240
flag.Parse()
254241
if *readerPtr {
255242
reader = true
256243
}
257-
if *cachePtr {
258-
conf.GoCacheOption = true
259-
}
260244
}
261245
func main() {
262246
// 获取当前工作目录

utils/utils.go

Lines changed: 2 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package utils
22

33
import (
44
"archive/zip"
5-
"bytes"
65
"fmt"
76
"goFile/conf"
87
"io"
@@ -11,8 +10,6 @@ import (
1110
"path/filepath"
1211
"sort"
1312
"strings"
14-
15-
"github.com/disintegration/imaging"
1613
)
1714

1815
// ReadFile read file
@@ -43,101 +40,10 @@ func ReadFile(path string) ([]byte, bool) {
4340
}
4441
return freeBytes, true
4542
}
46-
func ExistFile(filePath string) bool {
47-
// 获取文件信息
48-
_, err := os.Stat(filePath)
49-
// 判断文件是否存在
50-
if os.IsNotExist(err) {
51-
return false
52-
} else if err != nil {
53-
fmt.Println("Error:", err)
54-
return false
55-
} else {
56-
return true
57-
}
58-
}
5943

60-
// NewImgThumb 生成Thumb
61-
func NewImgThumb(fileContent []byte, thumbnailPath string) bool {
62-
buf := bytes.NewBuffer(fileContent)
63-
image, err := imaging.Decode(buf)
64-
if err != nil {
65-
fmt.Println(err)
66-
return false
67-
}
68-
thumbnail := imaging.Resize(image, 400, 0, imaging.Lanczos)
69-
// 获取文件夹路径
70-
thumbnailDir := filepath.Dir(thumbnailPath)
71-
72-
// 检查文件夹是否存在,如果不存在则创建
73-
if _, err := os.Stat(thumbnailDir); os.IsNotExist(err) {
74-
err := os.MkdirAll(thumbnailDir, os.ModePerm)
75-
if err != nil {
76-
fmt.Println("Error creating directory:", err)
77-
return false
78-
}
79-
}
80-
err = imaging.Save(thumbnail, thumbnailPath)
81-
if err != nil {
82-
fmt.Println(err)
83-
return false
84-
}
85-
return true
86-
}
8744
func RemovePP(path string) string {
8845
return strings.ReplaceAll(path, "//", "/")
8946
}
90-
func GetImgThumb(path, goCachePath string) bool {
91-
fileContent, getImgStatus := ReadFile(path)
92-
if getImgStatus {
93-
// //判断缓存文件夹是否存在(如果是默认文件夹则自动新建
94-
// if goCachePath == "/var/tmp/goFile/" {
95-
// _, err := os.Stat(goCachePath)
96-
// if os.IsNotExist(err) {
97-
// // 文件夹不存在,创建它
98-
// err := os.Mkdir(goCachePath, 0755) // 0755 是文件夹权限
99-
// if err != nil {
100-
// fmt.Println("Failed to create folder:", err)
101-
// return false
102-
// }
103-
// } else if err != nil {
104-
// // 其他错误
105-
// fmt.Println("Error:", err)
106-
// return false
107-
// }
108-
// }
109-
thumbnailPath := RemovePP(goCachePath + path)
110-
//判断thumb文件是否存在
111-
if ExistFile(thumbnailPath) {
112-
// 获取thumb文件信息
113-
fileInfo, err := os.Stat(thumbnailPath)
114-
if err != nil {
115-
fmt.Println("Error:", err)
116-
return false
117-
}
118-
// 获取文件最后修改时间
119-
thumbModTime := fileInfo.ModTime()
120-
fileInfo, err = os.Stat(path)
121-
if err != nil {
122-
fmt.Println("Error:", err)
123-
return false
124-
}
125-
//获取源文件最后修改时间
126-
modTime := fileInfo.ModTime()
127-
if thumbModTime.After(modTime) {
128-
//thumb是在源文件后建立的
129-
fmt.Println("thumbDate:", thumbModTime)
130-
fmt.Println(modTime)
131-
return true
132-
} else {
133-
return NewImgThumb(fileContent, thumbnailPath)
134-
}
135-
}
136-
return NewImgThumb(fileContent, thumbnailPath)
137-
} else {
138-
return false
139-
}
140-
}
14147

14248
// Unzip 解压zip
14349
func Unzip(src string) bool {
@@ -208,7 +114,7 @@ func GetFile(url, path string) bool {
208114
}
209115

210116
// Exists 判断是否存在
211-
func exists(path string) bool {
117+
func Exist(path string) bool {
212118
_, err := os.Stat(path) //os.Stat获取文件信息
213119
if err != nil {
214120
if os.IsExist(err) {
@@ -235,10 +141,9 @@ func GetFiles(path string) conf.Info {
235141
getFile, _ := filepath.Glob(path)
236142
var info conf.Info
237143
ZipList := []string{"zip", "gz"}
238-
ImgList := []string{"jpg", "png"}
239144
for i := 0; i < len(getFile); i++ {
240145
im := getFile[i]
241-
if exists(im) {
146+
if Exist(im) {
242147
s, _ := os.Stat(im)
243148
if s.IsDir() {
244149
var dir conf.Dir
@@ -264,10 +169,6 @@ func GetFiles(path string) conf.Info {
264169
if In(strSplit[len(strSplit)-1], ZipList) {
265170
file.IsZip = true
266171
}
267-
file.IsThumb = false
268-
if In(strSplit[len(strSplit)-1], ImgList) && conf.GoCacheOption {
269-
file.IsThumb = true
270-
}
271172
//file.FilePath = NewPath + im
272173
info.Files = append(info.Files, file)
273174
}

0 commit comments

Comments
 (0)