-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsaver.go
More file actions
68 lines (54 loc) · 1.18 KB
/
saver.go
File metadata and controls
68 lines (54 loc) · 1.18 KB
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
package baraka
import (
"os"
"path/filepath"
)
// Saver is a interface that wraps the Save function
type Saver interface {
Save(path string, filename string, part *Part) error
}
// FilesystemStorage is the default store to save parts
type FilesystemStorage struct {
Path string
}
// NewFilesystemStorage creates a new FilesystemStorage
func NewFilesystemStorage(path string) FilesystemStorage {
return FilesystemStorage{
Path: path,
}
}
// Save is a method for saving parts into disk.
func (s FilesystemStorage) Save(path string, filename string, part *Part) error {
if !isDir(path) {
err := os.MkdirAll(path, os.ModeSticky|os.ModePerm)
if err != nil {
return err
}
}
extension := part.Extension
if part.Extension == "" {
extension = filepath.Ext(part.Name)
}
out, err := os.Create(filepath.Join(path, filename+extension))
if err != nil {
return err
}
_, err = out.Write(part.Content)
if err != nil {
return err
}
err = out.Close()
if err != nil {
return err
}
return nil
}
// helper functions
// isDir checks if the path is a directory and exists
func isDir(path string) bool {
f, e := os.Stat(path)
if e != nil {
return false
}
return f.IsDir()
}