/
file.go
131 lines (108 loc) · 2.73 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
package filesystem
import (
"errors"
"fmt"
"io"
"mime/multipart"
"os"
"path/filepath"
"strings"
"time"
"github.com/chenyuIT/framework/contracts/config"
"github.com/chenyuIT/framework/contracts/filesystem"
supportfile "github.com/chenyuIT/framework/support/file"
"github.com/chenyuIT/framework/support/str"
)
type File struct {
config config.Config
disk string
path string
name string
storage filesystem.Storage
}
func NewFile(file string) (*File, error) {
if !supportfile.Exists(file) {
return nil, errors.New("file doesn't exist")
}
return &File{
config: ConfigFacade,
disk: ConfigFacade.GetString("filesystems.default"),
path: file,
name: filepath.Base(file),
storage: StorageFacade,
}, nil
}
func NewFileFromRequest(fileHeader *multipart.FileHeader) (*File, error) {
src, err := fileHeader.Open()
if err != nil {
return nil, err
}
defer func(src multipart.File) {
if err = src.Close(); err != nil {
panic(err)
}
}(src)
tempFileName := fmt.Sprintf("%s_*%s", ConfigFacade.GetString("app.name"), filepath.Ext(fileHeader.Filename))
tempFile, err := os.CreateTemp(os.TempDir(), tempFileName)
if err != nil {
return nil, err
}
defer func(tempFile *os.File) {
if err = tempFile.Close(); err != nil {
panic(err)
}
}(tempFile)
_, err = io.Copy(tempFile, src)
if err != nil {
return nil, err
}
return &File{
config: ConfigFacade,
disk: ConfigFacade.GetString("filesystems.default"),
path: tempFile.Name(),
name: fileHeader.Filename,
storage: StorageFacade,
}, nil
}
func (f *File) Disk(disk string) filesystem.File {
f.disk = disk
return f
}
func (f *File) Extension() (string, error) {
return supportfile.Extension(f.path)
}
func (f *File) File() string {
return f.path
}
func (f *File) GetClientOriginalName() string {
return f.name
}
func (f *File) GetClientOriginalExtension() string {
return supportfile.ClientOriginalExtension(f.name)
}
func (f *File) HashName(path ...string) string {
var realPath string
if len(path) > 0 {
realPath = strings.TrimRight(path[0], "/") + "/"
}
extension, _ := supportfile.Extension(f.path, true)
if extension == "" {
return realPath + str.Random(40)
}
return realPath + str.Random(40) + "." + extension
}
func (f *File) LastModified() (time.Time, error) {
return supportfile.LastModified(f.path, f.config.GetString("app.timezone"))
}
func (f *File) MimeType() (string, error) {
return supportfile.MimeType(f.path)
}
func (f *File) Size() (int64, error) {
return supportfile.Size(f.path)
}
func (f *File) Store(path string) (string, error) {
return f.storage.Disk(f.disk).PutFile(path, f)
}
func (f *File) StoreAs(path string, name string) (string, error) {
return f.storage.Disk(f.disk).PutFileAs(path, f, name)
}