2018-05-26 13:50:13 +05:00
|
|
|
// Code generated by fileb0x at "2018-05-26 13:49:35.047444828 +0500 +05 m=+0.010624405" from config file "fileb0x.yml" DO NOT EDIT.
|
|
|
|
// modification hash(1e92ae0f57fceda0c71705d184cfd4d6.522a9cc525fa984df98098d4c3992752)
|
2018-04-30 18:42:17 +05:00
|
|
|
|
|
|
|
package static
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
|
|
|
|
"golang.org/x/net/webdav"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// CTX is a context for webdav vfs
|
|
|
|
CTX = context.Background()
|
|
|
|
|
|
|
|
|
|
|
|
// FS is a virtual memory file system
|
|
|
|
FS = webdav.NewMemFS()
|
|
|
|
|
|
|
|
|
|
|
|
// Handler is used to server files through a http handler
|
|
|
|
Handler *webdav.Handler
|
|
|
|
|
|
|
|
// HTTP is the http file system
|
|
|
|
HTTP http.FileSystem = new(HTTPFS)
|
|
|
|
)
|
|
|
|
|
|
|
|
// HTTPFS implements http.FileSystem
|
|
|
|
type HTTPFS struct {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if CTX.Err() != nil {
|
|
|
|
panic(CTX.Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = FS.Mkdir(CTX, "static/", 0777)
|
|
|
|
if err != nil && err != os.ErrExist {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = FS.Mkdir(CTX, "static/css/", 0777)
|
|
|
|
if err != nil && err != os.ErrExist {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = FS.Mkdir(CTX, "static/js/", 0777)
|
|
|
|
if err != nil && err != os.ErrExist {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Handler = &webdav.Handler{
|
|
|
|
FileSystem: FS,
|
|
|
|
LockSystem: webdav.NewMemLS(),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Open a file
|
|
|
|
func (hfs *HTTPFS) Open(path string) (http.File, error) {
|
|
|
|
|
|
|
|
|
|
|
|
f, err := FS.OpenFile(CTX, path, os.O_RDONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFile is adapTed from ioutil
|
|
|
|
func ReadFile(path string) ([]byte, error) {
|
|
|
|
f, err := FS.OpenFile(CTX, path, os.O_RDONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(make([]byte, 0, bytes.MinRead))
|
|
|
|
|
|
|
|
// If the buffer overflows, we will get bytes.ErrTooLarge.
|
|
|
|
// Return that as an error. Any other panic remains.
|
|
|
|
defer func() {
|
|
|
|
e := recover()
|
|
|
|
if e == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if panicErr, ok := e.(error); ok && panicErr == bytes.ErrTooLarge {
|
|
|
|
err = panicErr
|
|
|
|
} else {
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
_, err = buf.ReadFrom(f)
|
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteFile is adapTed from ioutil
|
|
|
|
func WriteFile(filename string, data []byte, perm os.FileMode) error {
|
|
|
|
f, err := FS.OpenFile(CTX, filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n, err := f.Write(data)
|
|
|
|
if err == nil && n < len(data) {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
}
|
|
|
|
if err1 := f.Close(); err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WalkDirs looks for files in the given dir and returns a list of files in it
|
|
|
|
// usage for all files in the b0x: WalkDirs("", false)
|
|
|
|
func WalkDirs(name string, includeDirsInList bool, files ...string) ([]string, error) {
|
|
|
|
f, err := FS.OpenFile(CTX, name, os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileInfos, err := f.Readdir(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range fileInfos {
|
|
|
|
filename := path.Join(name, info.Name())
|
|
|
|
|
|
|
|
if includeDirsInList || !info.IsDir() {
|
|
|
|
files = append(files, filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
files, err = WalkDirs(filename, includeDirsInList, files...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|