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"
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2018-08-22 01:32:07 +00:00
|
|
|
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && file.GzipLength > 0 {
|
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 {
|
|
|
|
h.Set("Content-Length", strconv.FormatInt(file.Length, 10)) // Avoid doing a type conversion every time?
|
|
|
|
io.Copy(w, bytes.NewReader(file.Data))
|
|
|
|
}
|
|
|
|
// Other options instead of io.Copy: io.CopyN(), w.Write(), http.ServeContent()
|
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
func Overview(w http.ResponseWriter, r *http.Request, user c.User, header *c.Header) c.RouteError {
|
2018-11-01 06:43:56 +00:00
|
|
|
header.Title = phrases.GetTitlePhrase("overview")
|
2018-04-22 12:33:56 +00:00
|
|
|
header.Zone = "overview"
|
2019-04-19 09:34:16 +00:00
|
|
|
return renderTemplate("overview", w, r, header, c.Page{header, tList, nil})
|
2018-02-03 05:47:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
func CustomPage(w http.ResponseWriter, r *http.Request, user c.User, header *c.Header, name string) c.RouteError {
|
2018-04-22 12:33:56 +00:00
|
|
|
header.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 {
|
|
|
|
header.Title = page.Title
|
2019-04-19 09:34:16 +00:00
|
|
|
return renderTemplate("custom_page", w, r, header, c.CustomPagePage{header, 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-06-01 12:31:48 +00:00
|
|
|
header.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?
|
|
|
|
err = renderTemplate3("page_"+name, "tmpl_page", w, r, header, c.Page{header, tList, nil})
|
|
|
|
if err == c.ErrBadDefaultTemplate {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.NotFound(w, r, header)
|
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)
|
|
|
|
// TODO: Rename isJs to something else, just in case we rewrite the JS side in WebAssembly?
|
|
|
|
isJs := (r.PostFormValue("isJs") == "1")
|
2019-04-19 06:36:26 +00:00
|
|
|
newTheme := c.SanitiseSingleLine(r.PostFormValue("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-04-19 06:36:26 +00:00
|
|
|
return c.LocalErrorJSQ("That theme doesn't exist", w, r, user, isJs)
|
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)
|
|
|
|
|
|
|
|
if !isJs {
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
} else {
|
|
|
|
_, _ = w.Write(successJSONBytes)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|