This repository has been archived by the owner on Sep 27, 2021. It is now read-only.
forked from ulule/gostorages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
177 lines (136 loc) · 3.55 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package gostorages
import (
"io/ioutil"
"os"
"reflect"
"runtime"
"strings"
"syscall"
"time"
)
// Storage is a file system storage handler
type FileSystemStorage struct {
*BaseStorage
}
type FileSystemFile struct {
*os.File
Storage Storage
FileInfo os.FileInfo
}
// NewStorage returns a file system storage engine
func NewFileSystemStorage(location string, baseURL string) Storage {
return &FileSystemStorage{
&BaseStorage{
Location: location,
BaseURL: baseURL,
},
}
}
func NewFileSystemFile(storage Storage, file *os.File) (*FileSystemFile, error) {
fileInfo, err := file.Stat()
if err != nil {
return nil, err
}
return &FileSystemFile{
file,
storage,
fileInfo,
}, nil
}
func (s *FileSystemStorage) URL(filename string) string {
if s.HasBaseURL() {
return strings.Join([]string{s.BaseURL, filename}, "/")
}
return ""
}
func (f *FileSystemFile) Size() int64 {
return f.FileInfo.Size()
}
func (f *FileSystemFile) ReadAll() ([]byte, error) {
return ioutil.ReadAll(f)
}
// Save saves a file at the given path
func (s *FileSystemStorage) Save(filepath string, file File) error {
return s.SaveWithPermissions(filepath, file, DefaultFilePermissions)
}
// SaveWithPermissions saves a file with the given permissions to the storage
func (s *FileSystemStorage) SaveWithPermissions(filepath string, file File, perm os.FileMode) error {
_, err := os.Stat(s.Location)
if err != nil {
return err
}
location := s.Path(filepath)
basename := location[:strings.LastIndex(location, "/")+1]
err = os.MkdirAll(basename, perm)
if err != nil {
return err
}
content, err := file.ReadAll()
if err != nil {
return err
}
err = ioutil.WriteFile(location, content, perm)
return err
}
// Open returns the file content
func (s *FileSystemStorage) Open(filepath string) (File, error) {
file, err := os.Open(s.Path(filepath))
if err != nil {
return nil, err
}
return NewFileSystemFile(s, file)
}
// Delete the file from storage
func (s *FileSystemStorage) Delete(filepath string) error {
return os.Remove(s.Path(filepath))
}
// ModifiedTime returns the last update time
func (s *FileSystemStorage) ModifiedTime(filepath string) (time.Time, error) {
fi, err := os.Stat(s.Path(filepath))
if err != nil {
return time.Time{}, err
}
return fi.ModTime(), nil
}
// Exists checks if the given file is in the storage
func (s *FileSystemStorage) Exists(filepath string) bool {
_, err := os.Stat(s.Path(filepath))
return err == nil
}
func (s *FileSystemStorage) AccessedTime(filepath string) (time.Time, error) {
fi, err := os.Stat(s.Path(filepath))
if err != nil {
return time.Time{}, err
}
if runtime.GOOS == "windows" {
return s.ModifiedTime(filepath)
}
stat := fi.Sys().(*syscall.Stat_t)
atim := reflect.ValueOf(stat).FieldByName("Atim")
sec := atim.FieldByName("Sec").Int()
nsec := atim.FieldByName("Nsec").Int()
return time.Unix(int64(sec), int64(nsec)), nil
}
// CreatedTime returns the last access time.
func (s *FileSystemStorage) CreatedTime(filepath string) (time.Time, error) {
fi, err := os.Stat(s.Path(filepath))
if err != nil {
return time.Time{}, err
}
if runtime.GOOS == "windows" {
return s.ModifiedTime(filepath)
}
stat := fi.Sys().(*syscall.Stat_t)
atim := reflect.ValueOf(stat).FieldByName("Ctim")
sec := atim.FieldByName("Sec").Int()
nsec := atim.FieldByName("Nsec").Int()
return time.Unix(int64(sec), int64(nsec)), nil
}
// Size returns the size of the given file
func (s *FileSystemStorage) Size(filepath string) int64 {
fi, err := os.Stat(s.Path(filepath))
if err != nil {
return 0
}
return fi.Size()
}