gosora/files.go
Azareal d976a192fb Added the new theme, Shadow. It still isn't complete yet.
Revamped the configuration system.
If you have trouble installing this, try installing my fork of gopsutil with `go get` and delete the users_penalties table from the schema folder. That stuff will be cleaned up in the next commit.

Fixed an issue with the links for the Uncategorised forum being broken.
Swapped out NOW() for UTC_TIMESTAMP() in MySQL queries.
We now get an error message when the server goes down for whatever reason.
The template compiler now supports pointers.
Swapped out shirou/gopsutil for a fork locked on an older and more stable commit of the same library.
Fixed a bug where the preprocessor didn't play nice with empty CSS files.

Added the site name to the navbar.
Added more things to .gitignore
Laid the foundations for the next commit.
2017-07-17 11:23:42 +01:00

95 lines
1.8 KiB
Go

package main
import (
"log"
"bytes"
"strings"
"mime"
//"errors"
"os"
"io/ioutil"
"path/filepath"
"net/http"
"compress/gzip"
)
type SFile struct
{
Data []byte
GzipData []byte
Pos int64
Length int64
GzipLength int64
Mimetype string
Info os.FileInfo
FormattedModTime string
}
type CssData struct
{
ComingSoon string
}
func init_static_files() {
log.Print("Loading the static files.")
err := filepath.Walk("./public", func(path string, f os.FileInfo, err error) error {
if f.IsDir() {
return nil
}
path = strings.Replace(path,"\\","/",-1)
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
path = strings.TrimPrefix(path,"public/")
var ext string = filepath.Ext("/public/" + path)
gzip_data := compress_bytes_gzip(data)
static_files["/static/" + path] = SFile{data,gzip_data,0,int64(len(data)),int64(len(gzip_data)),mime.TypeByExtension(ext),f,f.ModTime().UTC().Format(http.TimeFormat)}
if dev.DebugMode {
log.Print("Added the '" + path + "' static file.")
}
return nil
})
if err != nil {
log.Fatal(err)
}
}
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
}
var ext string = filepath.Ext(path)
path = strings.TrimPrefix(path, prefix)
gzip_data := compress_bytes_gzip(data)
static_files["/static" + path] = SFile{data,gzip_data,0,int64(len(data)),int64(len(gzip_data)),mime.TypeByExtension(ext),f,f.ModTime().UTC().Format(http.TimeFormat)}
if dev.DebugMode {
log.Print("Added the '" + path + "' static file")
}
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()
}