2016-12-05 07:21:17 +00:00
|
|
|
package main
|
2017-02-10 13:39:13 +00:00
|
|
|
|
2017-01-07 06:31:04 +00:00
|
|
|
import "log"
|
2017-02-10 13:39:13 +00:00
|
|
|
import "bytes"
|
2017-01-07 06:31:04 +00:00
|
|
|
import "strings"
|
|
|
|
import "mime"
|
2017-02-15 10:49:30 +00:00
|
|
|
//import "errors"
|
2017-01-07 06:31:04 +00:00
|
|
|
import "os"
|
2017-02-15 10:49:30 +00:00
|
|
|
//import "io"
|
2017-01-07 06:31:04 +00:00
|
|
|
import "io/ioutil"
|
|
|
|
import "path/filepath"
|
|
|
|
import "net/http"
|
2017-02-10 13:39:13 +00:00
|
|
|
import "compress/gzip"
|
2016-12-05 07:21:17 +00:00
|
|
|
|
|
|
|
type SFile struct
|
|
|
|
{
|
|
|
|
Data []byte
|
2017-02-10 13:39:13 +00:00
|
|
|
GzipData []byte
|
2016-12-05 07:21:17 +00:00
|
|
|
Pos int64
|
|
|
|
Length int64
|
2017-02-16 06:47:55 +00:00
|
|
|
GzipLength int64
|
2016-12-05 07:21:17 +00:00
|
|
|
Mimetype string
|
|
|
|
Info os.FileInfo
|
|
|
|
FormattedModTime string
|
|
|
|
}
|
|
|
|
|
2017-02-15 10:49:30 +00:00
|
|
|
/*func (r SFile) Read(b []byte) (n int, err error) {
|
2016-12-05 07:21:17 +00:00
|
|
|
n = 0
|
|
|
|
if r.Pos > r.Length {
|
|
|
|
return n, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
size := cap(b)
|
|
|
|
if size > 0 {
|
|
|
|
for n < size {
|
|
|
|
b[n] = r.Data[r.Pos]
|
|
|
|
n++
|
|
|
|
if r.Pos == r.Length {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
r.Pos++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r SFile) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
if offset < 0 {
|
|
|
|
return 0, errors.New("negative position")
|
|
|
|
}
|
|
|
|
switch whence {
|
|
|
|
case 0:
|
|
|
|
r.Pos = offset
|
|
|
|
case 1:
|
|
|
|
r.Pos += offset
|
|
|
|
case 2:
|
|
|
|
r.Pos = r.Length + offset
|
|
|
|
default:
|
|
|
|
return 0, errors.New("invalid whence")
|
|
|
|
}
|
|
|
|
return r.Pos, nil
|
2017-02-15 10:49:30 +00:00
|
|
|
}*/
|
2017-01-07 06:31:04 +00:00
|
|
|
|
|
|
|
func add_static_file(path string, prefix string) error {
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fi, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := fi.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path = strings.TrimPrefix(path, prefix)
|
2017-05-07 08:31:41 +00:00
|
|
|
if debug {
|
|
|
|
log.Print("Added the '" + path + "' static file")
|
|
|
|
}
|
2017-02-16 06:47:55 +00:00
|
|
|
gzip_data := compress_bytes_gzip(data)
|
2017-01-07 06:31:04 +00:00
|
|
|
|
2017-02-16 06:47:55 +00:00
|
|
|
static_files["/static" + path] = SFile{data,gzip_data,0,int64(len(data)),int64(len(gzip_data)),mime.TypeByExtension(filepath.Ext(prefix + path)),f,f.ModTime().UTC().Format(http.TimeFormat)}
|
2017-01-07 06:31:04 +00:00
|
|
|
return nil
|
2017-02-10 13:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func compress_bytes_gzip(in []byte) []byte {
|
|
|
|
var buff bytes.Buffer
|
|
|
|
gz := gzip.NewWriter(&buff)
|
|
|
|
gz.Write(in)
|
|
|
|
gz.Close()
|
|
|
|
return buff.Bytes()
|
|
|
|
}
|