-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.go
More file actions
executable file
·124 lines (110 loc) · 3.23 KB
/
Copy pathstorage.go
File metadata and controls
executable file
·124 lines (110 loc) · 3.23 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
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
package rubik
import (
"io/ioutil"
"os"
"path/filepath"
)
// Storage is the Container Access of your storage/ folder
var Storage = StorageContainer{
path: filepath.Join(".", "storage"),
}
// GetStorageContainers returns the names of containers present in your
// storage/ folder. You can access them by calling `Storage.Access`
// API and use Get or Put to work with your files.
func GetStorageContainers() []string {
containers := []string{}
base := filepath.Join(".", "storage")
files, err := ioutil.ReadDir(base)
if err != nil {
return containers
}
for _, f := range files {
if f.IsDir() {
containers = append(containers, f.Name())
}
}
return containers
}
// StorageContainer is abstracted struct to access your
// storage files. It can access of remove a whole container.
//
// Container corresponds to a single directory in your storage
// folder and will have access to files only inside this
// container/directory
type StorageContainer struct {
path string
}
// FileStore lets you perform CRUD on files of StorageContainer
// FileStore returns the name of container you are accessing
// by Name field
type FileStore struct {
Name string
fullPath string
}
// Access a FileStore from your StorageContainer.
// It can be viewed as accessing a specific folder inside
// your storage/ folder and performing operations inside
// of that folder
func (s StorageContainer) Access(name string) (FileStore, error) {
storeFolder := filepath.Join(s.path, name)
if f, _ := os.Stat(storeFolder); f == nil {
err := os.Mkdir(storeFolder, 0755)
if err != nil {
return FileStore{}, err
}
}
return FileStore{Name: name, fullPath: storeFolder}, nil
}
// Remove a FileStore from your StorageContainer.
// Removing a FileStore will remove all the files inside
// the FileStore
func (s StorageContainer) Remove(name string) error {
storeFolder := filepath.Join(s.path, name)
// remove the directory named 'name'
if f, _ := os.Stat(storeFolder); f != nil {
return os.RemoveAll(storeFolder)
}
return nil
}
// Get a file from this FileStore, returs byte slice
func (fs FileStore) Get(file string) []byte {
outFile := filepath.Join(fs.fullPath, file)
b, err := ioutil.ReadFile(outFile)
if err != nil {
return nil
}
return b
}
// GetFile returns pointer to os.File for passing it into the rubikClient
// using File{}.OSFile = return value of this function
// Other usage may be to obtain file metadata stored
func (fs FileStore) GetFile(file string) *os.File {
outFile := filepath.Join(fs.fullPath, file)
f, err := os.Open(outFile)
if err != nil {
return nil
}
return f
}
// Put a file inside this FileStore given the content
// as parameter
func (fs FileStore) Put(file string, content []byte) error {
inFile := filepath.Join(fs.fullPath, file)
if f, _ := os.Stat(inFile); f != nil {
os.Remove(inFile)
}
return ioutil.WriteFile(inFile, content, 0755)
}
// Delete a file from the FileStore, returns error
func (fs FileStore) Delete(file string) error {
delFile := filepath.Join(fs.fullPath, file)
return os.Remove(delFile)
}
// Has checks if the file by given name is present
// inside this FileStore
func (fs FileStore) Has(file string) bool {
if f, _ := os.Stat(filepath.Join(fs.fullPath, file)); f != nil {
return true
}
return false
}