Remove source query, simplify code
parent
7e819f4017
commit
4997f1092e
72
entry.go
72
entry.go
|
@ -6,10 +6,8 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -44,15 +42,11 @@ type Image struct {
|
|||
|
||||
func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) {
|
||||
if contentType == WwwForm {
|
||||
bodyString, err := parseRequestBody(r)
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.New("failed to parse Form")
|
||||
}
|
||||
bodyValues, err := url.ParseQuery(bodyString)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to parse query")
|
||||
}
|
||||
return createEntryFromValueMap(bodyValues)
|
||||
return createEntryFromValueMap(r.Form)
|
||||
} else if contentType == Multipart {
|
||||
err := r.ParseMultipartForm(1024 * 1024 * 16)
|
||||
if err != nil {
|
||||
|
@ -60,12 +54,9 @@ func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) {
|
|||
}
|
||||
return createEntryFromValueMap(r.MultipartForm.Value)
|
||||
} else if contentType == Json {
|
||||
bodyString, err := parseRequestBody(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
parsedMfItem := &MicroformatItem{}
|
||||
err = json.Unmarshal([]byte(bodyString), &parsedMfItem)
|
||||
err := decoder.Decode(&parsedMfItem)
|
||||
if err != nil {
|
||||
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) {
|
||||
if h, ok := values["h"]; ok && (len(h) != 1 || h[0] != "entry") {
|
||||
return nil, errors.New("only entry type is supported so far")
|
||||
|
@ -320,46 +302,4 @@ func WriteEntry(entry *Entry) (location string, err error) {
|
|||
}
|
||||
location = entry.location
|
||||
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
|
||||
}
|
||||
}
|
14
micropub.go
14
micropub.go
|
@ -3,7 +3,6 @@ package main
|
|||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -29,19 +28,6 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
_, _ = w.Write(jsonBytes)
|
||||
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 {
|
||||
w.Header().Add("Content-type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
|
65
post.go
65
post.go
|
@ -4,7 +4,6 @@ import (
|
|||
"bytes"
|
||||
"errors"
|
||||
"gopkg.in/yaml.v2"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type HugoFrontmatter struct {
|
||||
|
@ -85,67 +84,3 @@ func WriteHugoPost(entry *Entry) (string, error) {
|
|||
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
|
||||
}
|
||||
|
|
94
query.go
94
query.go
|
@ -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
|
||||
}
|
23
storage.go
23
storage.go
|
@ -14,7 +14,6 @@ import (
|
|||
type Storage interface {
|
||||
CreateFile(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 {
|
||||
|
@ -135,26 +134,4 @@ func (g Git) UpdateFile(filepath string, file string, message string) error {
|
|||
return errors.New("failed to commit file")
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue