vinegar/vinegarUtil/files.go

66 lines
1.1 KiB
Go

package vinegarUtil
import (
"bytes"
gzip2 "compress/gzip"
"html/template"
"io/ioutil"
"net/http"
"os"
)
func RenderTemplate(w http.ResponseWriter, pathlike string, data any) {
templateHelper := template.New(pathlike)
f, err := os.OpenFile(pathlike, os.O_RDONLY, 0777)
if err != nil {
panic(err)
}
defer f.Close()
content, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
templ, err := templateHelper.Parse(string(content))
err = templ.Execute(w, data)
if err != nil {
panic(err)
}
}
func GetDiskContent(filePath string) (*[]byte, bool) {
_, ferr := os.Stat(filePath)
if os.IsNotExist(ferr) {
return nil, false
} else {
f, err := os.OpenFile(filePath, os.O_RDONLY, 0755)
if err != nil {
panic(err)
}
defer f.Close()
content, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
return &content, true
}
}
func GZipBytes(uncompressed *[]byte) *[]byte {
buff := bytes.Buffer{}
gzip := gzip2.NewWriter(&buff)
_, err := gzip.Write(*uncompressed)
if err != nil {
panic(err)
}
err = gzip.Flush()
if err != nil {
panic(err)
}
compressed := buff.Bytes()
return &compressed
}