/
file.go
147 lines (134 loc) · 3.28 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright 2021 liyiligang.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package Jtool
import (
"crypto/md5"
"encoding/hex"
"io"
"io/ioutil"
"mime/multipart"
"os"
"path"
"strings"
)
// CreateSysTmpFile 创建系统临时文件
func CreateSysTmpFile(fileName string, data []byte) (string, error) {
tmpFile, err := ioutil.TempFile(os.TempDir(), fileName)
if err != nil {
return "", err
}
_, err = tmpFile.Write(data)
if err != nil {
return "", err
}
return tmpFile.Name(), err
}
// IsFileExist 判断文件是否存在
func IsFileExist(filePath string) bool {
s, err := os.Lstat(filePath)
return !os.IsNotExist(err) && !s.IsDir()
}
// IsDirExist 判断文件夹是否存在
func IsDirExist(dirPath string) (bool, error) {
s, err := os.Stat(dirPath)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return s.IsDir(), nil
}
// MakeDir 创建文件夹
func MakeDir(dirPath string) error {
err := os.MkdirAll(dirPath, 755)
if err != nil {
return err
}
return nil
}
// MakeDirIfNoExist 判断文件夹是否存在, 不存在则创建
func MakeDirIfNoExist(dirPath string) (bool, error) {
isExist, err := IsDirExist(dirPath)
if err != nil {
return false, err
}
if isExist {
return true, nil
} else {
err := MakeDir(dirPath)
if err != nil {
return false, err
}
return false, nil
}
}
// 按文件名查找指定目录下的一个文件 (与后缀无关)
func FindDirFileWithFileName(dirPath string, fileName string) (string, error) {
fileList, err := ioutil.ReadDir(dirPath)
if err != nil {
return "", err
}
for _, file := range fileList {
if !file.IsDir() {
name := file.Name()
n := strings.LastIndex(file.Name(), ".")
if n >= 0 {
name = name[:n]
}
if name == fileName {
return file.Name(), nil
}
}
}
return "", nil
}
// ReadFileWithSize 按字节读取文件
func ReadFileWithSize(fileName string, size uint64, call func([]byte) error) error {
file, err := os.Open(fileName)
if err != nil {
return err
}
defer file.Close()
return ReadIOWithSize(file, size, call)
}
// GetFileMd5 获取文件Md5值(file)
func GetFileMd5(file multipart.File) (string, error) {
hash := md5.New()
_, err := io.Copy(hash, file)
if err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
// GetFileMd5WithPath 获取文件Md5值(path)
func GetFileMd5WithPath(filePath string) (string, error) {
body, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
m := md5.New()
m.Write(body)
return hex.EncodeToString(m.Sum(nil)), nil
}
//获取文件名后缀
func GetFileNameExt(fileName string) string {
return path.Ext(fileName)
}
//获取文件名不带后缀
func GetFileNameOnly(fileName string) string {
return strings.TrimSuffix(fileName, path.Ext(fileName))
}