/
fs.go
98 lines (79 loc) · 2.51 KB
/
fs.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
package upload
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/chadweimer/gomp/modules/conf"
)
// FileSystemDriver is an implementation of Driver that uses the local file system.
type FileSystemDriver struct {
cfg *conf.Config
}
// NewFileSystemDriver constucts a FileSystemDriver.
func NewFileSystemDriver(cfg *conf.Config) FileSystemDriver {
return FileSystemDriver{cfg: cfg}
}
// Save creates or overrites a file with the provided binary data.
func (u FileSystemDriver) Save(filePath string, data []byte) error {
// First prepend the base UploadPath
filePath = filepath.Join(u.cfg.UploadPath, filePath)
dir := filepath.Dir(filePath)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(data)
return err
}
// Delete deletes the file at the specified path, if it exists.
func (u FileSystemDriver) Delete(filePath string) error {
// First prepend the base UploadPath
filePath = filepath.Join(u.cfg.UploadPath, filePath)
return os.Remove(filePath)
}
// DeleteAll deletes all files at or under the specified directory path.
func (u FileSystemDriver) DeleteAll(dirPath string) error {
// First prepend the base UploadPath
dirPath = filepath.Join(u.cfg.UploadPath, dirPath)
return os.RemoveAll(dirPath)
}
// List retrieves information about all uploaded files under the specified directory.
func (u FileSystemDriver) List(dirPath string) ([]FileInfo, error) {
var fileInfos []FileInfo
// First prepend the base UploadPath
origDirPath := filepath.Join(u.cfg.UploadPath, dirPath, "images")
if _, err := os.Stat(origDirPath); os.IsNotExist(err) {
return fileInfos, nil
}
files, err := ioutil.ReadDir(origDirPath)
if err != nil {
return fileInfos, err
}
// TODO: Restrict based on file extension?
for _, file := range files {
if !file.IsDir() {
name := file.Name()
origPath := filepath.Join(origDirPath, file.Name())
thumbPath := filepath.Join(u.cfg.UploadPath, dirPath, "thumbs", file.Name())
fileInfo := FileInfo{
Name: name,
URL: u.convertPathToURL(origPath),
}
if _, err := os.Stat(thumbPath); err == nil {
fileInfo.ThumbnailURL = u.convertPathToURL(thumbPath)
}
fileInfos = append(fileInfos, fileInfo)
}
}
return fileInfos, nil
}
func (u FileSystemDriver) convertPathToURL(path string) string {
fullFilePath := filepath.Join("/uploads", strings.TrimPrefix(path, u.cfg.UploadPath))
return u.cfg.RootURLPath + filepath.ToSlash(fullFilePath)
}