forked from ulule/gostorages
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.go
57 lines (45 loc) · 1017 Bytes
/
base.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
package gostorages
import (
"bytes"
"io/ioutil"
"strings"
"path/filepath"
)
type BaseStorage struct {
BaseURL string
Location string
}
type ContentFile struct {
*bytes.Reader
}
func (f *ContentFile) Close() error {
return nil
}
func (f *ContentFile) Size() int64 {
return int64(f.Len())
}
func (f *ContentFile) ReadAll() ([]byte, error) {
return ioutil.ReadAll(f)
}
func NewContentFile(content []byte) *ContentFile {
return &ContentFile{bytes.NewReader(content)}
}
func NewBaseStorage(location string, baseURL string) *BaseStorage {
return &BaseStorage{
BaseURL: strings.TrimSuffix(baseURL, "/"),
Location: location,
}
}
func (s *BaseStorage) URL(filename string) string {
if s.HasBaseURL() {
return strings.Join([]string{s.BaseURL, s.Path(filename)}, "/")
}
return ""
}
func (s *BaseStorage) HasBaseURL() bool {
return s.BaseURL != ""
}
// Path joins the given file to the storage path
func (s *BaseStorage) Path(fPath string) string {
return filepath.Join(s.Location, fPath)
}