gosora/files.go
Azareal 534ef10194 You can now upvote topics and replies. The number of upvotes is visible on the posts.
The static resources now support Gzip Compression. Software-wide support is coming with the upcoming router rewrite!
Revamped Tempra Simple's topic view. We now use emojis instead of text for the fields. Experimental.
Simplified the json permissions in the installer.
Fixed some places where the wrong error handler is used.
Fixed a bug in the word counter where it was off by one.
The word count is now tracked by the topics and replies.
2017-02-10 13:39:13 +00:00

92 lines
1.6 KiB
Go

package main
import "log"
import "bytes"
import "strings"
import "mime"
import "errors"
import "os"
import "io"
import "io/ioutil"
import "path/filepath"
import "net/http"
import "compress/gzip"
type SFile struct
{
Data []byte
GzipData []byte
Pos int64
Length int64
Mimetype string
Info os.FileInfo
FormattedModTime string
}
func (r SFile) Read(b []byte) (n int, err error) {
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
}
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
}
log.Print("Adding the '" + path + "' static file")
path = strings.TrimPrefix(path, prefix)
log.Print("Added the '" + path + "' static file")
static_files["/static" + path] = SFile{data,compress_bytes_gzip(data),0,int64(len(data)),mime.TypeByExtension(filepath.Ext(prefix + path)),f,f.ModTime().UTC().Format(http.TimeFormat)}
return nil
}
func compress_bytes_gzip(in []byte) []byte {
var buff bytes.Buffer
gz := gzip.NewWriter(&buff)
gz.Write(in)
gz.Close()
return buff.Bytes()
}