-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
94 lines (79 loc) · 1.56 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
package fs
import (
"errors"
"io"
"io/fs"
"mime/multipart"
"net/http"
"strings"
"time"
)
type File struct {
File http.File
Name string
}
type FileInterface interface {
io.Reader
Name() string
}
type FileInfo struct {
name string
modTime time.Time
size int64
mode fs.FileMode
Binary []byte
}
func (f *FileInfo) Name() string {
return f.name
}
func (f *FileInfo) Size() int64 {
return f.size
}
func (f *FileInfo) Mode() fs.FileMode {
return f.mode
}
func (f *FileInfo) ModTime() time.Time {
return f.modTime
}
func (f *FileInfo) IsDir() bool {
return false
}
func (f *FileInfo) Sys() any {
return nil
}
type UploadFile struct {
ID uint64 `gorm:"primary_key" json:"id"`
FileName string `gorm:"type:varchar(100);not null" json:"file_name"`
OriginalName string `gorm:"type:varchar(100);not null" json:"original_name"`
URL string `json:"url"`
MD5 string `gorm:"type:varchar(32)" json:"md5"`
Mime string `json:"mime"`
Size uint64 `json:"size"`
}
func GetExt(file *multipart.FileHeader) (string, error) {
var ext string
var index = strings.LastIndex(file.Filename, ".")
if index == -1 {
return "", nil
} else {
ext = file.Filename[index:]
}
if len(ext) == 1 {
return "", errors.New("无效的扩展名")
}
return ext, nil
}
func CheckSize(f multipart.File, uploadMaxSize int) bool {
size := GetSize(f)
if size == 0 {
return false
}
return size <= uploadMaxSize
}
func GetSize(f multipart.File) int {
content, err := io.ReadAll(f)
if err != nil {
return 0
}
return len(content)
}