forked from rakyll/statik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
146 lines (128 loc) · 3.69 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
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package fs contains an HTTP file system that works with zip contents.
package fs
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
var zipData string
// file holds unzipped read-only file contents and file metadata.
type file struct {
os.FileInfo
data []byte
}
type statikFS struct {
files map[string]file
}
// Register registers zip contents data, later used to initialize
// the statik file system.
func Register(data string) {
zipData = data
}
// New creates a new file system with the registered zip contents data.
// It unzips all files and stores them in an in-memory map.
func New() (http.FileSystem, error) {
if zipData == "" {
return nil, errors.New("statik/fs: no zip data registered")
}
zipReader, err := zip.NewReader(strings.NewReader(zipData), int64(len(zipData)))
if err != nil {
return nil, err
}
files := make(map[string]file)
for _, zipFile := range zipReader.File {
unzipped, err := unzip(zipFile)
if err != nil {
return nil, fmt.Errorf("statik/fs: error unzipping file %q: %s", zipFile.Name, err)
}
files["/"+zipFile.Name] = file{
FileInfo: zipFile.FileInfo(),
data: unzipped,
}
}
return &statikFS{files: files}, nil
}
func unzip(zf *zip.File) ([]byte, error) {
rc, err := zf.Open()
if err != nil {
return nil, err
}
defer rc.Close()
return ioutil.ReadAll(rc)
}
// Open returns a file matching the given file name, or os.ErrNotExists if
// no file matching the given file name is found in the archive.
// If a directory is requested, Open returns the file named "index.html"
// in the requested directory, if that file exists.
func (fs *statikFS) Open(name string) (http.File, error) {
name = strings.Replace(name, "//", "/", -1)
f, ok := fs.files[name]
if ok {
return newHTTPFile(f, false), nil
}
// The file doesn't match, but maybe it's a directory,
// thus we should look for index.html
indexName := strings.Replace(name+"/index.html", "//", "/", -1)
f, ok = fs.files[indexName]
if !ok {
return nil, os.ErrNotExist
}
return newHTTPFile(f, true), nil
}
func newHTTPFile(file file, isDir bool) *httpFile {
return &httpFile{
file: file,
reader: bytes.NewReader(file.data),
isDir: isDir,
}
}
// httpFile represents an HTTP file and acts as a bridge
// between file and http.File.
type httpFile struct {
file
reader *bytes.Reader
isDir bool
}
// Read reads bytes into p, returns the number of read bytes.
func (f *httpFile) Read(p []byte) (n int, err error) {
return f.reader.Read(p)
}
// Seek seeks to the offset.
func (f *httpFile) Seek(offset int64, whence int) (ret int64, err error) {
return f.reader.Seek(offset, whence)
}
// Stat stats the file.
func (f *httpFile) Stat() (os.FileInfo, error) {
return f, nil
}
// IsDir returns true if the file location represents a directory.
func (f *httpFile) IsDir() bool {
return f.isDir
}
// Readdir returns an empty slice of files, directory
// listing is disabled.
func (f *httpFile) Readdir(count int) ([]os.FileInfo, error) {
// directory listing is disabled.
return make([]os.FileInfo, 0), nil
}
func (f *httpFile) Close() error {
return nil
}