From d1ce3ed49c7cc1ae23a6cb994a09b8cad59b722f Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Tue, 6 Oct 2020 20:04:07 +0200 Subject: [PATCH] Fixes --- api.go | 26 +++++++++++++++++++++----- hugo.go | 28 ++++++---------------------- postsDb.go | 7 +++++-- redirects.go | 4 ++++ 4 files changed, 36 insertions(+), 29 deletions(-) diff --git a/api.go b/api.go index 4fef4b4..aa9af52 100644 --- a/api.go +++ b/api.go @@ -4,6 +4,7 @@ import ( "encoding/json" "io/ioutil" "net/http" + "strings" ) func apiPostCreate(w http.ResponseWriter, r *http.Request) { @@ -26,11 +27,11 @@ func apiPostCreate(w http.ResponseWriter, r *http.Request) { } func apiPostCreateHugo(w http.ResponseWriter, r *http.Request) { + blog := r.URL.Query().Get("blog") path := r.URL.Query().Get("path") - if path == "" { - http.Error(w, "No path defined", http.StatusBadRequest) - return - } + section := r.URL.Query().Get("section") + slug := r.URL.Query().Get("slug") + alias := r.URL.Query().Get("alias") defer func() { _ = r.Body.Close() }() @@ -39,16 +40,31 @@ func apiPostCreateHugo(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } - post, err := parseHugoFile(string(bodyContent), path) + post, aliases, err := parseHugoFile(string(bodyContent)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } + post.Blog = blog + post.Path = path + post.Section = section + post.Slug = slug + aliases = append(aliases, alias) err = post.createOrReplace(false) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } + for _, alias := range aliases { + // Fix relativ paths + if !strings.HasPrefix(alias, "/") { + splittedPostPath := strings.Split(post.Path, "/") + alias = strings.TrimSuffix(post.Path, splittedPostPath[len(splittedPostPath)-1]) + alias + } + if alias != "" { + _ = createOrReplaceRedirect(alias, post.Path) + } + } w.Header().Set("Location", appConfig.Server.PublicAddress+post.Path) w.WriteHeader(http.StatusCreated) } diff --git a/hugo.go b/hugo.go index 7317dbd..e7fd953 100644 --- a/hugo.go +++ b/hugo.go @@ -1,7 +1,6 @@ package main import ( - "errors" "strconv" "strings" @@ -10,18 +9,13 @@ import ( "gopkg.in/yaml.v3" ) -func parseHugoFile(fileContent string, path string) (*Post, error) { - // TODO: Add option to set blog, slug - if path == "" { - return nil, errors.New("empty path") - } +func parseHugoFile(fileContent string) (post *Post, aliases []string, e error) { frontmatterSep := "---\n" frontmatter := "" if split := strings.Split(fileContent, frontmatterSep); len(split) > 2 { frontmatter = split[1] } - post := &Post{ - Path: path, + post = &Post{ Content: strings.TrimPrefix(fileContent, frontmatterSep+frontmatter+frontmatterSep), Parameters: map[string][]string{}, } @@ -29,11 +23,11 @@ func parseHugoFile(fileContent string, path string) (*Post, error) { meta := map[string]interface{}{} err := yaml.Unmarshal([]byte(frontmatter), &meta) if err != nil { - return nil, err + return nil, nil, err } flat, err := flatten.Flatten(meta, "", flatten.DotStyle) if err != nil { - return nil, err + return nil, nil, err } // Read dates post.Published = cast.ToString(flat["date"]) @@ -58,9 +52,7 @@ func parseHugoFile(fileContent string, path string) (*Post, error) { post.Parameters[fm.Parameter] = values } } - // Create redirects - // TODO: Move redirect creation after post creation - var aliases []string + // Parse redirects for fk, value := range flat { if strings.HasPrefix(fk, "aliases") { trimmed := strings.TrimPrefix(fk, "aliases") @@ -74,14 +66,6 @@ func parseHugoFile(fileContent string, path string) (*Post, error) { } } } - for _, alias := range aliases { - // Fix relativ paths - if !strings.HasPrefix(alias, "/") { - splittedPostPath := strings.Split(post.Path, "/") - alias = strings.TrimSuffix(post.Path, splittedPostPath[len(splittedPostPath)-1]) + alias - } - _ = createOrReplaceRedirect(alias, post.Path) - } // Return post - return post, nil + return post, aliases, nil } diff --git a/postsDb.go b/postsDb.go index a58ad29..559b09f 100644 --- a/postsDb.go +++ b/postsDb.go @@ -51,16 +51,19 @@ func (p *Post) checkPost(new bool) error { random := generateRandomString(now, 5) p.Slug = fmt.Sprintf("%v-%02d-%02d-%v", now.Year(), int(now.Month()), now.Day(), random) } + published, _ := dateparse.ParseIn(p.Published, time.Local) pathVars := struct { BlogPath string Year int Month int + Day int Slug string Section string }{ BlogPath: appConfig.Blogs[p.Blog].Path, - Year: now.Year(), - Month: int(now.Month()), + Year: published.Year(), + Month: int(published.Month()), + Day: published.Day(), Slug: p.Slug, Section: p.Section, } diff --git a/redirects.go b/redirects.go index 68dcd1e..2908382 100644 --- a/redirects.go +++ b/redirects.go @@ -61,6 +61,10 @@ func createOrReplaceRedirect(from, to string) error { if from == "" || to == "" { return errors.New("empty path") } + if from == to { + // Don't need a redirect + return nil + } from = strings.TrimSuffix(from, "/") startWritingToDb() tx, err := appDb.Begin()