-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfilesystem.go
46 lines (39 loc) · 1.08 KB
/
filesystem.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
package media_library
import (
"io"
"os"
"path/filepath"
)
// FileSystem defined a media library storage using file system
type FileSystem struct {
Base
}
// GetFullPath return full file path from a relative file path
func (f FileSystem) GetFullPath(url string, option *Option) (path string, err error) {
if option != nil && option.Get("path") != "" {
path = filepath.Join(option.Get("path"), url)
} else {
path = filepath.Join("./public", url)
}
dir := filepath.Dir(path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, os.ModePerm)
}
return
}
// Store save reader's context with name
func (f FileSystem) Store(name string, option *Option, reader io.Reader) (err error) {
if fullpath, err := f.GetFullPath(name, option); err == nil {
if dst, err := os.Create(fullpath); err == nil {
_, err = io.Copy(dst, reader)
}
}
return err
}
// Retrieve retrieve file content with url
func (f FileSystem) Retrieve(url string) (*os.File, error) {
if fullpath, err := f.GetFullPath(url, nil); err == nil {
return os.Open(fullpath)
}
return nil, os.ErrNotExist
}