GoBlog/api.go

62 lines
1.4 KiB
Go
Raw Normal View History

2020-07-31 19:44:16 +00:00
package main
import (
"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
)
2021-01-15 20:56:46 +00:00
// Not tested anymore
func apiPostCreateHugo(w http.ResponseWriter, r *http.Request) {
2020-10-06 18:04:07 +00:00
blog := r.URL.Query().Get("blog")
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")
defer func() {
_ = r.Body.Close()
}()
bodyContent, err := ioutil.ReadAll(r.Body)
if err != nil {
2020-12-24 09:09:34 +00:00
serveError(w, r, err.Error(), http.StatusBadRequest)
return
}
2020-10-15 15:32:46 +00:00
p, aliases, err := parseHugoFile(string(bodyContent))
if err != nil {
2020-12-24 09:09:34 +00:00
serveError(w, r, 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
2021-01-15 20:56:46 +00:00
err = p.create()
if err != nil {
2020-12-24 09:09:34 +00:00
serveError(w, r, 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
2021-01-15 20:56:46 +00:00
err = p.replace(p.Path, p.Status)
2020-11-09 17:33:56 +00:00
if err != nil {
2020-12-24 09:09:34 +00:00
serveError(w, r, err.Error(), http.StatusBadRequest)
2020-11-09 17:33:56 +00:00
return
2020-10-06 18:04:07 +00:00
}
}
2021-01-15 20:56:46 +00:00
http.Redirect(w, r, p.fullURL(), http.StatusCreated)
}