2018-02-03 05:47:14 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-05-15 05:59:52 +00:00
|
|
|
"database/sql"
|
2018-02-03 05:47:14 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-03-06 21:52:58 +00:00
|
|
|
|
2019-10-27 08:58:35 +00:00
|
|
|
//"fmt"
|
2018-02-03 05:47:14 +00:00
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2018-11-01 06:43:56 +00:00
|
|
|
"github.com/Azareal/Gosora/common/phrases"
|
2018-02-03 05:47:14 +00:00
|
|
|
)
|
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
var cacheControlMaxAge = "max-age=" + strconv.Itoa(int(c.Day)) // TODO: Make this a c.Config value
|
2019-05-03 08:34:18 +00:00
|
|
|
var cacheControlMaxAgeWeek = "max-age=" + strconv.Itoa(int(c.Week)) // TODO: Make this a c.Config value
|
2018-02-03 05:47:14 +00:00
|
|
|
|
|
|
|
// GET functions
|
|
|
|
func StaticFile(w http.ResponseWriter, r *http.Request) {
|
2019-04-19 06:36:26 +00:00
|
|
|
file, ok := c.StaticFiles.Get(r.URL.Path)
|
2018-02-03 05:47:14 +00:00
|
|
|
if !ok {
|
2019-07-23 10:34:44 +00:00
|
|
|
//c.DebugLogf("Failed to find '%s'", r.URL.Path) // TODO: Use MicroNotFound? Might be better than the unneccessary overhead of sprintf
|
2018-02-03 05:47:14 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h := w.Header()
|
|
|
|
|
2020-03-06 21:52:58 +00:00
|
|
|
if file.Length > 300 {
|
2020-03-06 23:00:35 +00:00
|
|
|
if h.Get("Range") != "" {
|
2020-03-06 21:52:58 +00:00
|
|
|
if file.GzipLength > 300 && strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
|
|
|
if len(file.Sha256) != 0 {
|
|
|
|
h.Set("Cache-Control", cacheControlMaxAgeWeek)
|
|
|
|
} else {
|
|
|
|
h.Set("Cache-Control", cacheControlMaxAge) //Cache-Control: max-age=31536000
|
|
|
|
}
|
|
|
|
h.Set("Content-Encoding", "gzip")
|
|
|
|
h.Set("Content-Length", file.StrGzipLength)
|
|
|
|
http.ServeContent(w, r, r.URL.Path, file.Info.ModTime(), bytes.NewReader(file.GzipData))
|
2020-03-06 22:11:13 +00:00
|
|
|
return
|
2020-03-06 21:52:58 +00:00
|
|
|
} else if file.GzipLength == 0 {
|
|
|
|
if len(file.Sha256) != 0 {
|
|
|
|
h.Set("Cache-Control", cacheControlMaxAgeWeek)
|
|
|
|
} else {
|
|
|
|
h.Set("Cache-Control", cacheControlMaxAge) //Cache-Control: max-age=31536000
|
|
|
|
}
|
|
|
|
h.Set("Content-Length", file.StrLength)
|
|
|
|
http.ServeContent(w, r, r.URL.Path, file.Info.ModTime(), bytes.NewReader(file.Data))
|
2020-03-06 22:11:13 +00:00
|
|
|
return
|
2020-03-06 21:52:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-03 05:47:14 +00:00
|
|
|
// Surely, there's a more efficient way of doing this?
|
2018-08-13 12:01:27 +00:00
|
|
|
t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since"))
|
2018-02-03 05:47:14 +00:00
|
|
|
if err == nil && file.Info.ModTime().Before(t.Add(1*time.Second)) {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.Set("Last-Modified", file.FormattedModTime)
|
|
|
|
h.Set("Content-Type", file.Mimetype)
|
2019-05-03 08:34:18 +00:00
|
|
|
if len(file.Sha256) != 0 {
|
|
|
|
h.Set("Cache-Control", cacheControlMaxAgeWeek)
|
|
|
|
} else {
|
|
|
|
h.Set("Cache-Control", cacheControlMaxAge) //Cache-Control: max-age=31536000
|
|
|
|
}
|
2018-02-03 05:47:14 +00:00
|
|
|
h.Set("Vary", "Accept-Encoding")
|
2018-08-13 12:01:27 +00:00
|
|
|
|
2020-03-06 21:52:58 +00:00
|
|
|
if file.GzipLength > 0 && strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
2018-02-03 05:47:14 +00:00
|
|
|
h.Set("Content-Encoding", "gzip")
|
2019-07-23 10:34:44 +00:00
|
|
|
h.Set("Content-Length", file.StrGzipLength)
|
2018-02-03 05:47:14 +00:00
|
|
|
io.Copy(w, bytes.NewReader(file.GzipData)) // Use w.Write instead?
|
|
|
|
} else {
|
2020-03-06 21:52:58 +00:00
|
|
|
h.Set("Content-Length", file.StrLength)
|
2018-02-03 05:47:14 +00:00
|
|
|
io.Copy(w, bytes.NewReader(file.Data))
|
|
|
|
}
|
|
|
|
// Other options instead of io.Copy: io.CopyN(), w.Write(), http.ServeContent()
|
|
|
|
}
|
|
|
|
|
2019-10-06 00:34:09 +00:00
|
|
|
func Overview(w http.ResponseWriter, r *http.Request, user c.User, h *c.Header) c.RouteError {
|
|
|
|
h.Title = phrases.GetTitlePhrase("overview")
|
|
|
|
h.Zone = "overview"
|
|
|
|
return renderTemplate("overview", w, r, h, c.Page{h, tList, nil})
|
2018-02-03 05:47:14 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 00:34:09 +00:00
|
|
|
func CustomPage(w http.ResponseWriter, r *http.Request, user c.User, h *c.Header, name string) c.RouteError {
|
|
|
|
h.Zone = "custom_page"
|
2019-04-19 06:36:26 +00:00
|
|
|
name = c.SanitiseSingleLine(name)
|
|
|
|
page, err := c.Pages.GetByName(name)
|
2018-06-06 00:21:22 +00:00
|
|
|
if err == nil {
|
2019-10-06 00:34:09 +00:00
|
|
|
h.Title = page.Title
|
|
|
|
return renderTemplate("custom_page", w, r, h, c.CustomPagePage{h, page})
|
2018-06-06 00:21:22 +00:00
|
|
|
} else if err != sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-06-06 00:21:22 +00:00
|
|
|
}
|
2019-10-06 00:34:09 +00:00
|
|
|
h.Title = phrases.GetTitlePhrase("page")
|
2018-06-06 00:21:22 +00:00
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
// TODO: Pass the page name to the pre-render hook?
|
2019-10-06 00:34:09 +00:00
|
|
|
err = renderTemplate3("page_"+name, "tmpl_page", w, r, h, c.Page{h, tList, nil})
|
2019-06-01 12:31:48 +00:00
|
|
|
if err == c.ErrBadDefaultTemplate {
|
2019-10-06 00:34:09 +00:00
|
|
|
return c.NotFound(w, r, h)
|
2019-06-01 12:31:48 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
2018-02-03 05:47:14 +00:00
|
|
|
}
|
2019-06-01 12:31:48 +00:00
|
|
|
return nil
|
2018-02-03 05:47:14 +00:00
|
|
|
}
|
2018-05-15 05:59:52 +00:00
|
|
|
|
|
|
|
// TODO: Set the cookie domain
|
2019-04-19 06:36:26 +00:00
|
|
|
func ChangeTheme(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError {
|
2018-05-15 05:59:52 +00:00
|
|
|
//headerLite, _ := SimpleUserCheck(w, r, &user)
|
2019-09-30 10:15:50 +00:00
|
|
|
// TODO: Rename js to something else, just in case we rewrite the JS side in WebAssembly?
|
2019-10-06 00:34:09 +00:00
|
|
|
js := r.PostFormValue("js") == "1"
|
2019-10-27 08:58:35 +00:00
|
|
|
newTheme := c.SanitiseSingleLine(r.PostFormValue("theme"))
|
|
|
|
//fmt.Printf("newTheme: %+v\n", newTheme)
|
2018-05-15 05:59:52 +00:00
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
theme, ok := c.Themes[newTheme]
|
2018-05-15 05:59:52 +00:00
|
|
|
if !ok || theme.HideFromThemes {
|
2019-09-30 10:15:50 +00:00
|
|
|
return c.LocalErrorJSQ("That theme doesn't exist", w, r, user, js)
|
2018-05-15 05:59:52 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
cookie := http.Cookie{Name: "current_theme", Value: newTheme, Path: "/", MaxAge: int(c.Year)}
|
2018-05-15 05:59:52 +00:00
|
|
|
http.SetCookie(w, &cookie)
|
|
|
|
|
2019-09-30 10:15:50 +00:00
|
|
|
if !js {
|
2018-05-15 05:59:52 +00:00
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
} else {
|
|
|
|
_, _ = w.Write(successJSONBytes)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|