-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
64 lines (52 loc) · 1.18 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
package jumper
import (
"errors"
"io/ioutil"
"mime/multipart"
"os"
"path/filepath"
)
type File struct {
f multipart.File
fh *multipart.FileHeader
name string
}
func (f *File) GetFile() multipart.File {
return f.f
}
func (f *File) GetFileHeader() *multipart.FileHeader {
return f.fh
}
func (f *File) Name() string {
return f.name
}
func (f *File) Store(path string, pattern string, perm os.FileMode) (string, error) {
os.MkdirAll(path, perm)
file, err := ioutil.TempFile(path, pattern)
if err != nil {
return "", errors.New("failed to store file")
}
defer file.Close()
fBytes, err := ioutil.ReadAll(f.GetFile())
if err != nil {
return "", errors.New("failed to read file")
}
file.Write(fBytes)
os.Chmod(file.Name(), perm)
f.name = file.Name()
//here we save our file to our path
return filepath.Base(f.name), nil
}
func (f *File) StoreAs(path string, name string, perm os.FileMode) error {
os.MkdirAll(path, perm)
fBytes, err := ioutil.ReadAll(f.GetFile())
if err != nil {
return errors.New("failed to read file")
}
err = ioutil.WriteFile(path+"/"+name, fBytes, perm)
if err != nil {
return errors.New("failed to store file")
}
f.name = name
return nil
}