2022-06-03 04:05:36 +00:00
|
|
|
package lfo
|
|
|
|
|
|
|
|
import (
|
2022-06-03 04:56:38 +00:00
|
|
|
"context"
|
2024-11-28 17:39:02 +00:00
|
|
|
core "forge.lightcrystal.systems/nilix/nirvash/archetype"
|
2023-10-21 04:46:37 +00:00
|
|
|
"net/http"
|
2022-06-05 18:09:00 +00:00
|
|
|
"strings"
|
2022-06-03 04:05:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func WithAdapter(next http.Handler, adapter core.Adapter) http.Handler {
|
2022-06-03 04:56:38 +00:00
|
|
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
*req = *req.WithContext(context.WithValue(req.Context(), "adapter", adapter))
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(handlerFunc)
|
2022-06-03 04:05:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 06:38:29 +00:00
|
|
|
func WithFileManager(next http.Handler, fileManager core.FileManager) http.Handler {
|
|
|
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
*req = *req.WithContext(context.WithValue(req.Context(), "file-manager", fileManager))
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(handlerFunc)
|
|
|
|
}
|
|
|
|
|
2022-06-06 05:29:39 +00:00
|
|
|
func EnsurePageData(next http.Handler, adapter core.Adapter) http.Handler {
|
2022-06-05 18:09:00 +00:00
|
|
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
pageTitle := req.FormValue("title")
|
|
|
|
pageContent := req.FormValue("content")
|
2022-06-06 05:29:39 +00:00
|
|
|
newSlug := req.FormValue("slug")
|
2022-06-05 18:09:00 +00:00
|
|
|
|
2022-06-06 05:29:39 +00:00
|
|
|
if pageTitle == "" || pageContent == "" || (adapter.EditableSlugs() && newSlug == "") {
|
2022-06-05 18:09:00 +00:00
|
|
|
newUri := "/edit/"
|
|
|
|
slug := strings.Join(strings.Split(req.URL.Path, "/")[2:], "/")
|
|
|
|
newUri += slug
|
2022-06-06 05:29:39 +00:00
|
|
|
newUri += "?no-empty=1"
|
2022-06-05 18:09:00 +00:00
|
|
|
req.Method = http.MethodGet
|
|
|
|
http.Redirect(w, req, newUri, http.StatusSeeOther)
|
|
|
|
} else {
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(handlerFunc)
|
|
|
|
}
|
2022-06-08 05:44:54 +00:00
|
|
|
|
|
|
|
func SanitizeFormMap(next http.Handler) http.Handler {
|
|
|
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
delete(req.PostForm, "csrfToken")
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(handlerFunc)
|
|
|
|
}
|
|
|
|
|
2022-06-18 06:17:08 +00:00
|
|
|
func FormMapToAdapterConfig(next http.Handler) http.Handler {
|
2022-06-08 05:44:54 +00:00
|
|
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
cfg := make(map[core.ConfigOption]string)
|
|
|
|
for k, arr := range req.PostForm {
|
|
|
|
v := strings.Join(arr, "")
|
|
|
|
optNameAndType := strings.Split(k, ":")
|
|
|
|
optName := optNameAndType[0]
|
|
|
|
optType := optNameAndType[1]
|
|
|
|
cfg[core.ConfigOption{
|
|
|
|
Name: optName,
|
|
|
|
Type: optType,
|
|
|
|
}] = v
|
|
|
|
}
|
|
|
|
*req = *req.WithContext(context.WithValue(req.Context(), "config", cfg))
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(handlerFunc)
|
|
|
|
}
|
2022-06-12 06:00:38 +00:00
|
|
|
|
2022-06-18 06:17:08 +00:00
|
|
|
func FormMapToBuildOptions(next http.Handler) http.Handler {
|
|
|
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
options := make(map[core.BuildOption]string)
|
|
|
|
for k, arr := range req.PostForm {
|
|
|
|
v := strings.Join(arr, "")
|
|
|
|
optNameAndType := strings.Split(k, ":")
|
|
|
|
optName := optNameAndType[0]
|
|
|
|
optType := optNameAndType[1]
|
|
|
|
options[core.BuildOption{
|
|
|
|
Name: optName,
|
|
|
|
Type: optType,
|
|
|
|
}] = v
|
|
|
|
}
|
|
|
|
*req = *req.WithContext(context.WithValue(req.Context(), "build-options", options))
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(handlerFunc)
|
|
|
|
}
|
|
|
|
|
2022-06-15 07:33:56 +00:00
|
|
|
func PrepareForUpload(next http.Handler, fileManager core.FileManager) http.Handler {
|
2022-06-12 06:00:38 +00:00
|
|
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
2022-06-15 07:33:56 +00:00
|
|
|
req.ParseMultipartForm(fileManager.MaxUploadMB() << 20)
|
2022-06-13 06:32:14 +00:00
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(handlerFunc)
|
|
|
|
}
|