2026-05-25 15:36:49 +03:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2026-06-02 11:30:33 +03:00
|
|
|
"os"
|
2026-05-25 15:36:49 +03:00
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (a *App) Static() http.Handler {
|
|
|
|
|
fileServer := http.StripPrefix("/static/", http.FileServer(http.Dir(a.cfg.StaticDir)))
|
|
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
setStaticCacheHeaders(w, r.URL.Path)
|
|
|
|
|
fileServer.ServeHTTP(w, r)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 11:30:33 +03:00
|
|
|
func (a *App) EmojiAsset(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
pack := strings.TrimSpace(r.PathValue("pack"))
|
|
|
|
|
file := strings.TrimSpace(r.PathValue("file"))
|
|
|
|
|
if pack == "" || file == "" || strings.Contains(pack, "/") || strings.Contains(pack, "\\") || strings.Contains(pack, "..") || strings.Contains(file, "/") || strings.Contains(file, "\\") || strings.Contains(file, "..") || !isEmojiFile(file) {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
path := filepath.Join(a.emojiRoot(), pack, file)
|
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
|
if err != nil || info.IsDir() {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
setStaticCacheHeaders(w, r.URL.Path)
|
|
|
|
|
http.ServeFile(w, r, path)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 15:36:49 +03:00
|
|
|
func setStaticCacheHeaders(w http.ResponseWriter, path string) {
|
|
|
|
|
ext := strings.ToLower(filepath.Ext(path))
|
|
|
|
|
|
|
|
|
|
switch ext {
|
|
|
|
|
case ".avif", ".gif", ".ico", ".jpg", ".jpeg", ".png", ".svg", ".webp",
|
|
|
|
|
".mp4", ".m4v", ".mov", ".webm", ".mp3", ".ogg",
|
|
|
|
|
".eot", ".otf", ".ttf", ".woff", ".woff2":
|
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
|
|
|
|
case ".css", ".js":
|
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=86400")
|
|
|
|
|
default:
|
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=3600")
|
|
|
|
|
}
|
|
|
|
|
}
|