forked from extrame/goblet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
143 lines (122 loc) · 3.29 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
package goblet
import (
"io"
"mime/multipart"
"net/textproto"
"os"
"path/filepath"
)
// File the input file type, if you want to response a file, just response(*os.File)
type File struct {
//Name the filename uploaded with file
Name string
//Path the filepath after saved in server
Path string
Header textproto.MIMEHeader `json:"-"`
rc multipart.File `xorm:"-"`
}
func (f *File) Read(p []byte) (n int, err error) {
return f.rc.Read(p)
}
func (f *File) ReadAt(p []byte, off int64) (n int, err error) {
return f.rc.ReadAt(p, off)
}
func (f *File) Close() error {
return f.rc.Close()
}
func (f *File) Seek(offset int64, whence int) (int64, error) {
return f.rc.Seek(offset, whence)
}
func (f *File) GetName() string {
return f.Name
}
func (f *File) GetSize() int64 {
if fi, ok := f.rc.(*os.File); ok {
stat, err := fi.Stat()
if err == nil {
return stat.Size()
}
}
return 0
}
func (f *File) SaveInPublic(dir string, s *Server) error {
f.Path = filepath.Join(s.PublicDir(), dir, f.Name)
return s.saver.Save(filepath.Join(s.Basic.WwwRoot, f.Path), f)
}
func (f *File) SaveInTemp(dir string, s *Server) error {
f.Path = filepath.Join(os.TempDir(), dir, f.Name)
return s.saver.Save(f.Path, f)
}
func (f *File) SaveInPrivate(dir string, s *Server) error {
f.Path = filepath.Join(dir, f.Name)
return s.saver.Save(filepath.Join(s.Basic.WwwRoot, f.Path), f)
}
func (f *File) OpenInPrivate(s *Server) error {
fi, err := os.Open(filepath.Join(s.Basic.WwwRoot, f.Path))
if err == nil {
f.rc = fi
}
return err
}
// Open open file in any location of server, if want to open file relate to www dir, please use OpenInPrivate
func Open(path string) (f *File, err error) {
var fi *os.File
fi, err = os.Open(path)
if err == nil {
f = new(File)
f.rc = fi
f.Path = path
//TODO limit open file out of current dir
}
return
}
// 将文件保存在公开目录,可以使用http访问到
func (s *Server) SaveInPublic(path string, f io.Reader) error {
fullPath := filepath.Join(s.Basic.WwwRoot, s.PublicDir(), path)
return s.saver.Save(fullPath, f)
}
func (s *Server) DelFileInPrivate(path string, force ...bool) error {
fullPath := filepath.Join(s.Basic.WwwRoot, path)
var _force = false
if len(force) > 0 {
_force = force[0]
}
return s.saver.Delete(fullPath, _force)
}
func (s *Server) DelFileInPublic(path string, force ...bool) error {
fullPath := filepath.Join(s.Basic.WwwRoot, s.PublicDir(), path)
var _force = false
if len(force) > 0 {
_force = force[0]
}
return s.saver.Delete(fullPath, _force)
}
// 将文件保存在私有目录,不可以使用http访问到
func (s *Server) SaveInPrivate(path string, f io.Reader) error {
fullPath := filepath.Join(s.Basic.WwwRoot, path)
return s.saver.Save(fullPath, f)
}
type Saver interface {
Save(fullpath string, f io.Reader) error
Delete(fullpath string, force bool) error
}
type LocalSaver struct {
}
func (l *LocalSaver) Save(path string, f io.Reader) error {
var file *os.File
var err error
err = os.MkdirAll(filepath.Dir(path), 0777)
if err == nil {
if file, err = os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666); err == nil {
io.Copy(file, f)
file.Close()
}
}
return err
}
func (l *LocalSaver) Delete(path string, force bool) error {
if force {
return os.RemoveAll(path)
}
return os.Remove(path)
}