@@ -2,7 +2,6 @@ package utils
22
33import (
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- }
8744func 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
14349func 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