@@ -2,7 +2,6 @@ package utils
2
2
3
3
import (
4
4
"archive/zip"
5
- "bytes"
6
5
"fmt"
7
6
"goFile/conf"
8
7
"io"
@@ -11,8 +10,6 @@ import (
11
10
"path/filepath"
12
11
"sort"
13
12
"strings"
14
-
15
- "github.com/disintegration/imaging"
16
13
)
17
14
18
15
// ReadFile read file
@@ -43,101 +40,10 @@ func ReadFile(path string) ([]byte, bool) {
43
40
}
44
41
return freeBytes , true
45
42
}
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
- }
59
43
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
- }
87
44
func RemovePP (path string ) string {
88
45
return strings .ReplaceAll (path , "//" , "/" )
89
46
}
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
- }
141
47
142
48
// Unzip 解压zip
143
49
func Unzip (src string ) bool {
@@ -208,7 +114,7 @@ func GetFile(url, path string) bool {
208
114
}
209
115
210
116
// Exists 判断是否存在
211
- func exists (path string ) bool {
117
+ func Exist (path string ) bool {
212
118
_ , err := os .Stat (path ) //os.Stat获取文件信息
213
119
if err != nil {
214
120
if os .IsExist (err ) {
@@ -235,10 +141,9 @@ func GetFiles(path string) conf.Info {
235
141
getFile , _ := filepath .Glob (path )
236
142
var info conf.Info
237
143
ZipList := []string {"zip" , "gz" }
238
- ImgList := []string {"jpg" , "png" }
239
144
for i := 0 ; i < len (getFile ); i ++ {
240
145
im := getFile [i ]
241
- if exists (im ) {
146
+ if Exist (im ) {
242
147
s , _ := os .Stat (im )
243
148
if s .IsDir () {
244
149
var dir conf.Dir
@@ -264,10 +169,6 @@ func GetFiles(path string) conf.Info {
264
169
if In (strSplit [len (strSplit )- 1 ], ZipList ) {
265
170
file .IsZip = true
266
171
}
267
- file .IsThumb = false
268
- if In (strSplit [len (strSplit )- 1 ], ImgList ) && conf .GoCacheOption {
269
- file .IsThumb = true
270
- }
271
172
//file.FilePath = NewPath + im
272
173
info .Files = append (info .Files , file )
273
174
}
0 commit comments