-
Notifications
You must be signed in to change notification settings - Fork 1
/
filesystem.go
121 lines (102 loc) · 2.62 KB
/
filesystem.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
/*
* Copyright (c) 2019. Octofox.io
*/
package foundation
import (
"bytes"
"fmt"
"github.com/rakyll/statik/fs"
"io/ioutil"
"net/http"
"os"
"path"
"time"
)
type file struct {
*bytes.Reader
mif fileInfo
}
func (mf *file) Close() error { return nil } // Noop, nothing to do
func (mf *file) Readdir(count int) ([]os.FileInfo, error) {
return nil, nil // We are not a directory but a single file
}
func (mf *file) Stat() (os.FileInfo, error) {
return mf.mif, nil
}
type fileInfo struct {
name string
data []byte
}
func (mif fileInfo) Name() string { return mif.name }
func (mif fileInfo) Size() int64 { return int64(len(mif.data)) }
func (mif fileInfo) Mode() os.FileMode { return 0444 } // Read for all
func (mif fileInfo) ModTime() time.Time { return time.Time{} } // Return anything
func (mif fileInfo) IsDir() bool { return false }
func (mif fileInfo) Sys() interface{} { return nil }
// FileSystem
// This utility help you to access local fileInfo by 2 ways
// - Local fileInfo storage
// - Bundled fileInfo system (using statik, please see rakyll/statik documentation)
type FileSystem interface {
GetObject(key string) ([]byte, error)
Open(name string) (http.File, error)
}
type StatikFileSystem struct {
http.FileSystem
}
func (f *StatikFileSystem) GetObject(k string) ([]byte, error) {
r, err := f.Open(path.Join("/", k))
if err != nil {
return nil, err
}
var buf = new(bytes.Buffer)
_, err = buf.ReadFrom(r)
if err != nil {
return nil, err
}
return buf.Bytes(), err
}
type LocalFileSystem struct {
rootDir string
http.Dir
}
func (f *LocalFileSystem) GetObject(key string) ([]byte, error) {
result, err := ioutil.ReadFile(path.Join(f.rootDir, key))
if err != nil {
return nil, err
}
return result, nil
}
type StaticMode string
const (
StaticMode_LOCAL = "LOCAL"
StaticMode_Statik = "STATIK"
)
// NewFileSystem
// Create FileSystem class that can access and return fileInfo
// from system fileInfo
// - rootDirectory is where the root directory of filesystem
// - mode is StaticMode_LOCAL or StaticMode_STATIK
//
// if you want to use StaticMode_STATIK please make sure you are following
// the instruction from rakll/statik documentation
func NewFileSystem(rootDir string, mode StaticMode) FileSystem {
if mode == StaticMode_Statik {
fmt.Println("Use fs with statik mode")
var s, err = fs.New()
if err != nil {
panic(err)
}
return &StatikFileSystem{
FileSystem: s,
}
} else if mode == StaticMode_LOCAL {
fmt.Println("Use fs with local mode")
return &LocalFileSystem{
rootDir: rootDir,
Dir: http.Dir(rootDir),
}
} else {
panic("Static mode invalid")
}
}