-
Notifications
You must be signed in to change notification settings - Fork 402
/
asset.go
121 lines (110 loc) · 2.68 KB
/
asset.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 Storj Labs, Inc.
// See LICENSE for copying information.
// Package asset implements asset embedding via implementing http.FileSystem interface.
//
// To use the package you would define:
//
// //go:generate go run ../private/asset/generate/main.go -pkg main -dir ../../web/bootstrap -var embeddedAssets -out console.resource.go
// var embeddedAssets http.FileSystem
//
// This will generate a new "console.resource.go" which contains the content of "../../web/bootstrap".
//
// In the program initialization you can select based on whether the embedded resources exist or not:
//
// var assets http.FileSystem
// if *staticAssetDirectory != "" {
// assets = http.Dir(*staticAssetDirectory)
// } else if embeddedAssets == nil {
// assets = embeddedAssets
// } else {
// assets = http.Dir(defaultAssetLocation)
// }
//
// Then write the service in terms of http.FileSystem, which hides the actual thing used for loading.
//
package asset
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"time"
)
// Asset describes a tree of asset files and directories.
type Asset struct {
Name string
Mode os.FileMode
ModTime time.Time
Data []byte
Children []*Asset
}
// ReadDir loads an asset directory from filesystem.
func ReadDir(path string) (*Asset, error) {
abspath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
asset, err := ReadFile(abspath)
if err != nil {
return nil, err
}
asset.Name = ""
return asset, nil
}
// ReadFile loads an asset from filesystem.
func ReadFile(path string) (*Asset, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer func() {
closeErr := file.Close()
if closeErr != nil {
if err == nil {
err = closeErr
} else {
err = fmt.Errorf("%v: %v", err, closeErr)
}
}
}()
stat, err := file.Stat()
if err != nil {
return nil, err
}
asset := &Asset{
Name: stat.Name(),
Mode: stat.Mode(),
ModTime: stat.ModTime(),
}
if stat.IsDir() {
children, err := file.Readdir(-1)
if err != nil {
return nil, err
}
err = asset.readFiles(path, children)
if err != nil {
return nil, err
}
} else {
asset.Data, err = ioutil.ReadAll(file)
if err != nil {
return nil, err
}
}
return asset, nil
}
// readFiles adds all nested files to asset
func (asset *Asset) readFiles(dir string, infos []os.FileInfo) error {
for _, info := range infos {
child, err := ReadFile(filepath.Join(dir, info.Name()))
if err != nil {
return err
}
asset.Children = append(asset.Children, child)
}
sort.Slice(asset.Children, func(i, k int) bool {
return asset.Children[i].Name < asset.Children[k].Name
})
return nil
}