2016-08-11 13:47:26 +00:00
|
|
|
// 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.
|
|
|
|
|
2018-05-08 16:57:55 +00:00
|
|
|
// Package fs contains an HTTP file system that works with zip contents.
|
2016-08-11 13:47:26 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2018-05-08 16:57:55 +00:00
|
|
|
"fmt"
|
2016-08-11 13:47:26 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var zipData string
|
|
|
|
|
2018-05-08 16:57:55 +00:00
|
|
|
// file holds unzipped read-only file contents and file metadata.
|
|
|
|
type file struct {
|
|
|
|
os.FileInfo
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
2016-08-11 13:47:26 +00:00
|
|
|
type statikFS struct {
|
2018-05-08 16:57:55 +00:00
|
|
|
files map[string]file
|
2016-08-11 13:47:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 16:57:55 +00:00
|
|
|
// Register registers zip contents data, later used to initialize
|
2016-08-11 13:47:26 +00:00
|
|
|
// the statik file system.
|
|
|
|
func Register(data string) {
|
|
|
|
zipData = data
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:57:55 +00:00
|
|
|
// New creates a new file system with the registered zip contents data.
|
|
|
|
// It unzips all files and stores them in an in-memory map.
|
2016-08-11 13:47:26 +00:00
|
|
|
func New() (http.FileSystem, error) {
|
|
|
|
if zipData == "" {
|
2018-05-08 16:57:55 +00:00
|
|
|
return nil, errors.New("statik/fs: no zip data registered")
|
2016-08-11 13:47:26 +00:00
|
|
|
}
|
|
|
|
zipReader, err := zip.NewReader(strings.NewReader(zipData), int64(len(zipData)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-08 16:57:55 +00:00
|
|
|
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,
|
|
|
|
}
|
2016-08-11 13:47:26 +00:00
|
|
|
}
|
|
|
|
return &statikFS{files: files}, nil
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:57:55 +00:00
|
|
|
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.
|
2016-08-11 13:47:26 +00:00
|
|
|
func (fs *statikFS) Open(name string) (http.File, error) {
|
|
|
|
name = strings.Replace(name, "//", "/", -1)
|
|
|
|
f, ok := fs.files[name]
|
2018-05-08 16:57:55 +00:00
|
|
|
if ok {
|
|
|
|
return newHTTPFile(f, false), nil
|
|
|
|
}
|
2016-08-11 13:47:26 +00:00
|
|
|
// The file doesn't match, but maybe it's a directory,
|
|
|
|
// thus we should look for index.html
|
2018-05-08 16:57:55 +00:00
|
|
|
indexName := strings.Replace(name+"/index.html", "//", "/", -1)
|
|
|
|
f, ok = fs.files[indexName]
|
2016-08-11 13:47:26 +00:00
|
|
|
if !ok {
|
2018-05-08 16:57:55 +00:00
|
|
|
return nil, os.ErrNotExist
|
2016-08-11 13:47:26 +00:00
|
|
|
}
|
2018-05-08 16:57:55 +00:00
|
|
|
return newHTTPFile(f, true), nil
|
2016-08-11 13:47:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 16:57:55 +00:00
|
|
|
func newHTTPFile(file file, isDir bool) *httpFile {
|
|
|
|
return &httpFile{
|
|
|
|
file: file,
|
|
|
|
reader: bytes.NewReader(file.data),
|
|
|
|
isDir: isDir,
|
2016-08-11 13:47:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:57:55 +00:00
|
|
|
// httpFile represents an HTTP file and acts as a bridge
|
|
|
|
// between file and http.File.
|
|
|
|
type httpFile struct {
|
|
|
|
file
|
2016-08-11 13:47:26 +00:00
|
|
|
|
2018-05-08 16:57:55 +00:00
|
|
|
reader *bytes.Reader
|
|
|
|
isDir bool
|
2016-08-11 13:47:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 16:57:55 +00:00
|
|
|
// Read reads bytes into p, returns the number of read bytes.
|
|
|
|
func (f *httpFile) Read(p []byte) (n int, err error) {
|
2016-08-11 13:47:26 +00:00
|
|
|
return f.reader.Read(p)
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:57:55 +00:00
|
|
|
// Seek seeks to the offset.
|
|
|
|
func (f *httpFile) Seek(offset int64, whence int) (ret int64, err error) {
|
2016-08-11 13:47:26 +00:00
|
|
|
return f.reader.Seek(offset, whence)
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:57:55 +00:00
|
|
|
// Stat stats the file.
|
|
|
|
func (f *httpFile) Stat() (os.FileInfo, error) {
|
2016-08-11 13:47:26 +00:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDir returns true if the file location represents a directory.
|
2018-05-08 16:57:55 +00:00
|
|
|
func (f *httpFile) IsDir() bool {
|
2016-08-11 13:47:26 +00:00
|
|
|
return f.isDir
|
|
|
|
}
|
|
|
|
|
2018-05-08 16:57:55 +00:00
|
|
|
// Readdir returns an empty slice of files, directory
|
2016-08-11 13:47:26 +00:00
|
|
|
// listing is disabled.
|
2018-05-08 16:57:55 +00:00
|
|
|
func (f *httpFile) Readdir(count int) ([]os.FileInfo, error) {
|
2016-08-11 13:47:26 +00:00
|
|
|
// directory listing is disabled.
|
|
|
|
return make([]os.FileInfo, 0), nil
|
|
|
|
}
|
2018-05-08 16:57:55 +00:00
|
|
|
|
|
|
|
func (f *httpFile) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|