Remove source query, simplify code

master
Jan-Lukas Else 2020-03-26 10:49:13 +01:00
parent 7e819f4017
commit 4997f1092e
5 changed files with 6 additions and 262 deletions

View File

@ -6,10 +6,8 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"net/http" "net/http"
"net/url"
"strings" "strings"
"time" "time"
) )
@ -44,15 +42,11 @@ type Image struct {
func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) { func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) {
if contentType == WwwForm { if contentType == WwwForm {
bodyString, err := parseRequestBody(r) err := r.ParseForm()
if err != nil { if err != nil {
return nil, err return nil, errors.New("failed to parse Form")
} }
bodyValues, err := url.ParseQuery(bodyString) return createEntryFromValueMap(r.Form)
if err != nil {
return nil, errors.New("failed to parse query")
}
return createEntryFromValueMap(bodyValues)
} else if contentType == Multipart { } else if contentType == Multipart {
err := r.ParseMultipartForm(1024 * 1024 * 16) err := r.ParseMultipartForm(1024 * 1024 * 16)
if err != nil { if err != nil {
@ -60,12 +54,9 @@ func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) {
} }
return createEntryFromValueMap(r.MultipartForm.Value) return createEntryFromValueMap(r.MultipartForm.Value)
} else if contentType == Json { } else if contentType == Json {
bodyString, err := parseRequestBody(r) decoder := json.NewDecoder(r.Body)
if err != nil {
return nil, err
}
parsedMfItem := &MicroformatItem{} parsedMfItem := &MicroformatItem{}
err = json.Unmarshal([]byte(bodyString), &parsedMfItem) err := decoder.Decode(&parsedMfItem)
if err != nil { if err != nil {
return nil, errors.New("failed to parse Json") return nil, errors.New("failed to parse Json")
} }
@ -75,15 +66,6 @@ func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) {
} }
} }
func parseRequestBody(r *http.Request) (string, error) {
defer r.Body.Close()
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", errors.New("failed to read body")
}
return string(bodyBytes), nil
}
func createEntryFromValueMap(values map[string][]string) (*Entry, error) { func createEntryFromValueMap(values map[string][]string) (*Entry, error) {
if h, ok := values["h"]; ok && (len(h) != 1 || h[0] != "entry") { if h, ok := values["h"]; ok && (len(h) != 1 || h[0] != "entry") {
return nil, errors.New("only entry type is supported so far") return nil, errors.New("only entry type is supported so far")
@ -321,45 +303,3 @@ func WriteEntry(entry *Entry) (location string, err error) {
location = entry.location location = entry.location
return return
} }
func analyzeURL(url string) (filePath string, section string, slug string, lang string, err error) {
if !strings.HasPrefix(url, BlogUrl) {
return
}
contentFolder := "content"
path := ""
if strings.HasPrefix(url, BlogUrl+"de/") {
lang = "de"
// German content folder
contentFolder += "-" + lang
path = strings.TrimSuffix(strings.TrimPrefix(url, BlogUrl+lang+"/"), "/")
} else {
path = strings.TrimSuffix(strings.TrimPrefix(url, BlogUrl), "/")
}
pathParts := strings.Split(path, "/")
filePath = contentFolder + "/" + path + ".md"
section = pathParts[0]
slug = pathParts[len(pathParts)-1]
return
}
func ReadEntry(url string) (entry *Entry, err error) {
filePath, section, slug, lang, err := analyzeURL(url)
if err != nil {
return
}
fileContent, exists, err := SelectedStorage.ReadFile(filePath)
if err != nil || !exists {
err = errors.New("failed to read file or entry doesn't exist")
return
}
entry, err = ReadHugoPost(fileContent)
if entry != nil {
entry.location = url
entry.filename = filePath
entry.section = section
entry.slug = slug
entry.language = lang
}
return
}

View File

@ -3,7 +3,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"strconv"
"time" "time"
) )
@ -29,19 +28,6 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) {
} }
_, _ = w.Write(jsonBytes) _, _ = w.Write(jsonBytes)
return return
} else if url := r.URL.Query().Get("url"); q == "source" {
limit := r.URL.Query().Get("limit")
limitInt, err := strconv.Atoi(limit)
jsonBytes, err := QueryURL(url, limitInt)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(err.Error()))
return
}
w.Header().Add("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(jsonBytes)
return
} else { } else {
w.Header().Add("Content-type", "application/json") w.Header().Add("Content-type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)

65
post.go
View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"strings"
) )
type HugoFrontmatter struct { type HugoFrontmatter struct {
@ -85,67 +84,3 @@ func WriteHugoPost(entry *Entry) (string, error) {
return buff.String(), nil return buff.String(), nil
} }
func readFrontMatter(frontmatter string, entry *Entry) (err error) {
parsedFrontmatter := &HugoFrontmatter{}
err = yaml.Unmarshal([]byte(frontmatter), &parsedFrontmatter)
if err != nil {
err = errors.New("failed parsing frontmatter")
}
if len(parsedFrontmatter.Title) > 0 {
entry.title = parsedFrontmatter.Title
}
if len(parsedFrontmatter.Date) > 0 {
entry.date = parsedFrontmatter.Date
}
if len(parsedFrontmatter.Lastmod) > 0 {
entry.lastmod = parsedFrontmatter.Lastmod
}
if len(parsedFrontmatter.Tags) > 0 {
entry.tags = parsedFrontmatter.Tags
}
if len(parsedFrontmatter.ExternalURL) > 0 {
entry.link = parsedFrontmatter.ExternalURL
}
if len(parsedFrontmatter.Indieweb.Reply.Link) > 0 {
entry.replyLink = parsedFrontmatter.Indieweb.Reply.Link
}
if len(parsedFrontmatter.Indieweb.Reply.Title) > 0 {
entry.replyTitle = parsedFrontmatter.Indieweb.Reply.Title
}
if len(parsedFrontmatter.Indieweb.Like.Link) > 0 {
entry.replyLink = parsedFrontmatter.Indieweb.Like.Link
}
if len(parsedFrontmatter.Indieweb.Like.Title) > 0 {
entry.replyTitle = parsedFrontmatter.Indieweb.Like.Title
}
if len(parsedFrontmatter.Syndicate) > 0 {
entry.syndicate = parsedFrontmatter.Syndicate
}
if len(parsedFrontmatter.TranslationKey) > 0 {
entry.translationKey = parsedFrontmatter.TranslationKey
}
if len(parsedFrontmatter.Images) > 0 {
for _, image := range parsedFrontmatter.Images {
entry.images = append(entry.images, Image{url: image})
}
}
if len(parsedFrontmatter.Audio) > 0 {
entry.audio = parsedFrontmatter.Audio
}
return
}
func ReadHugoPost(fileContent string) (entry *Entry, err error) {
parts := strings.Split(fileContent, "---\n")
if len(parts) != 3 {
err = errors.New("empty frontmatter or content")
return
}
entry = new(Entry)
err = readFrontMatter(parts[1], entry)
if err != nil {
return
}
entry.content = strings.TrimSuffix(parts[2], "\n")
return
}

View File

@ -1,94 +0,0 @@
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
type ItemList struct {
Items []*MicroformatItem `json:"items"`
}
func QueryURL(url string, limit int) ([]byte, error) {
if len(url) == 0 {
url = BlogUrl
}
if url == BlogUrl {
allPosts, err := allPosts(url)
if err != nil {
return nil, err
}
itemList := &ItemList{}
for i, postURL := range allPosts {
if limit != 0 && i == limit {
break
}
item, _ := getItem(postURL)
itemList.Items = append(itemList.Items, item)
}
jsonBytes, err := json.Marshal(itemList)
if err != nil {
err = errors.New("failed to marshal json")
return nil, err
}
return jsonBytes, err
} else {
item, err := getItem(url)
if err != nil {
return nil, err
}
jsonBytes, err := json.Marshal(item)
if err != nil {
err = errors.New("failed to marshal json")
return nil, err
}
return jsonBytes, err
}
}
func getItem(url string) (item *MicroformatItem, err error) {
entry, err := ReadEntry(url)
if err != nil {
return
}
item = &MicroformatItem{
Type: []string{"h-entry"},
Properties: &MicroformatProperties{
Name: []string{entry.title},
Published: []string{entry.date},
Updated: []string{entry.lastmod},
Category: entry.tags,
Content: []string{entry.content},
Url: []string{entry.location},
},
}
return
}
func allPosts(url string) ([]string, error) {
jsonFeed := &struct {
Items []struct {
Url string `json:"url"`
} `json:"items"`
}{}
resp, err := http.Get(url + "feed.json")
if err != nil {
return nil, errors.New("failed to get json feed")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("failed to read json feed")
}
err = json.Unmarshal(body, &jsonFeed)
if err != nil {
return nil, errors.New("failed to parse json feed")
}
var allUrls []string
for _, item := range jsonFeed.Items {
allUrls = append(allUrls, item.Url)
}
return allUrls, nil
}

View File

@ -14,7 +14,6 @@ import (
type Storage interface { type Storage interface {
CreateFile(path string, file string, message string) (err error) CreateFile(path string, file string, message string) (err error)
UpdateFile(path string, file string, message string) (err error) UpdateFile(path string, file string, message string) (err error)
ReadFile(path string) (content string, exists bool, err error)
} }
type Git struct { type Git struct {
@ -136,25 +135,3 @@ func (g Git) UpdateFile(filepath string, file string, message string) error {
} }
return g.push(repo) return g.push(repo)
} }
func (g Git) ReadFile(filepath string) (content string, exists bool, err error) {
_, _, err = g.init()
if err != nil {
err = errors.New("failed to initialize repo")
return
}
joinedPath := path.Join(g.filepath, filepath)
if _, e := os.Stat(joinedPath); e == nil {
exists = true
if b, e := ioutil.ReadFile(joinedPath); e == nil {
content = string(b)
return
} else {
err = errors.New("failed to read file")
return
}
} else {
exists = false
return
}
}