183 lines
3.1 KiB
Go
183 lines
3.1 KiB
Go
// Code generated by fileb0x at "2018-05-01 14:28:30.814103749 +0500 +05 m=+0.033880643" from config file "fileb0x.yml" DO NOT EDIT.
|
|
// modification hash(173f89ae699d558787ec5f8317fe49c2.5c3b355fe5a9e5324e826569a62de332)
|
|
|
|
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
|
|
}
|
|
|
|
|