-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
executable file
·72 lines (61 loc) · 1.39 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
/**
* @Author: TheLife
* @Date: 2020-2-25 9:00 下午
*/
package upload
import (
"fmt"
"github.com/lifegit/go-gulu/crypto"
"github.com/lifegit/go-gulu/file"
"github.com/lifegit/go-gulu/rand"
"os"
"path"
"strconv"
"strings"
"time"
)
type File struct {
Error error
Url string
Save string
}
type FileAttribute struct {
Key string
Exts []string
DirPath string
MaxSize int64
}
// 使用md5随机生成一个文件名
func RandFileName(name string) string {
ext := path.Ext(name)
fileName := strings.TrimSuffix(name, ext)
fileName = fileName + "_" + strconv.FormatInt(time.Now().Unix(), 10) + "_" + strconv.Itoa(rand.Int(10000, 99999))
fileName = crypto.EncodeMD5(fileName)
return fileName + ext
}
// 判断一个文件的拓展名是否在切片内
func CheckFileExt(fileName string, exts *[]string) bool {
ext := file.GetExt(fileName)
for _, allowExt := range *exts {
if strings.ToUpper(string(allowExt)) == strings.ToUpper(ext) {
return true
}
}
return false
}
// 就绪一个文件夹
func ReadyDir(src string) error {
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("os.Getwd err: %v", err)
}
err = file.IsNotExistMkDir(dir + "/" + src)
if err != nil {
return fmt.Errorf("file.IsNotExistMkDir err: %v", err)
}
perm := file.CheckPermission(src)
if perm == true {
return fmt.Errorf("file.CheckPermission Permission denied src: %s", src)
}
return nil
}