2020-07-31 19:44:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-18 10:34:52 +00:00
|
|
|
"io/ioutil"
|
2020-07-31 19:44:16 +00:00
|
|
|
"net/http"
|
2020-10-06 18:04:07 +00:00
|
|
|
"strings"
|
2020-07-31 19:44:16 +00:00
|
|
|
)
|
|
|
|
|
2020-09-18 10:34:52 +00:00
|
|
|
func apiPostCreateHugo(w http.ResponseWriter, r *http.Request) {
|
2020-10-06 18:04:07 +00:00
|
|
|
blog := r.URL.Query().Get("blog")
|
2020-09-18 10:34:52 +00:00
|
|
|
path := r.URL.Query().Get("path")
|
2020-10-06 18:04:07 +00:00
|
|
|
section := r.URL.Query().Get("section")
|
|
|
|
slug := r.URL.Query().Get("slug")
|
|
|
|
alias := r.URL.Query().Get("alias")
|
2020-09-18 10:34:52 +00:00
|
|
|
defer func() {
|
|
|
|
_ = r.Body.Close()
|
|
|
|
}()
|
|
|
|
bodyContent, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-10-15 15:32:46 +00:00
|
|
|
p, aliases, err := parseHugoFile(string(bodyContent))
|
2020-09-18 10:34:52 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2020-10-15 15:32:46 +00:00
|
|
|
p.Blog = blog
|
|
|
|
p.Path = path
|
|
|
|
p.Section = section
|
|
|
|
p.Slug = slug
|
|
|
|
err = p.replace()
|
2020-09-18 10:34:52 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-11-09 17:33:56 +00:00
|
|
|
aliases = append(aliases, alias)
|
|
|
|
for i, alias := range aliases {
|
2020-10-06 18:04:07 +00:00
|
|
|
// Fix relativ paths
|
|
|
|
if !strings.HasPrefix(alias, "/") {
|
2020-10-15 15:32:46 +00:00
|
|
|
splittedPostPath := strings.Split(p.Path, "/")
|
|
|
|
alias = strings.TrimSuffix(p.Path, splittedPostPath[len(splittedPostPath)-1]) + alias
|
2020-10-06 18:04:07 +00:00
|
|
|
}
|
2020-11-09 17:33:56 +00:00
|
|
|
alias = strings.TrimSuffix(alias, "/")
|
|
|
|
if alias == p.Path {
|
|
|
|
alias = ""
|
|
|
|
}
|
|
|
|
aliases[i] = alias
|
|
|
|
}
|
|
|
|
if len(aliases) > 0 {
|
|
|
|
p.Parameters["aliases"] = aliases
|
|
|
|
err = p.replace()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
2020-10-06 18:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-16 17:34:29 +00:00
|
|
|
w.Header().Set("Location", p.fullURL())
|
2020-09-18 10:34:52 +00:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
}
|