2024-07-13 13:58:36 +02:00
|
|
|
package frontend
|
2024-02-27 14:10:27 +01:00
|
|
|
|
|
|
|
import (
|
2024-03-29 09:07:17 +01:00
|
|
|
"fmt"
|
2024-02-27 14:10:27 +01:00
|
|
|
"html/template"
|
2024-03-29 09:07:17 +01:00
|
|
|
"io"
|
2024-02-27 14:10:27 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-03-29 09:07:17 +01:00
|
|
|
"os"
|
2024-04-07 10:58:07 +02:00
|
|
|
"path/filepath"
|
2024-03-06 20:53:17 +01:00
|
|
|
"strconv"
|
2024-04-07 18:43:09 +02:00
|
|
|
"strings"
|
2024-03-17 09:41:09 +01:00
|
|
|
"time"
|
2024-02-27 14:10:27 +01:00
|
|
|
|
2024-04-07 18:43:09 +02:00
|
|
|
"github.com/google/uuid"
|
2024-07-13 13:58:36 +02:00
|
|
|
"streifling.com/jason/cpolis/cmd/backend"
|
2024-02-27 14:10:27 +01:00
|
|
|
)
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
func ShowHub(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
2024-03-01 11:30:31 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-03 09:16:49 +01:00
|
|
|
session, err := s.Get(r, "cookie")
|
|
|
|
if err != nil {
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
2024-03-03 09:16:49 +01:00
|
|
|
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
|
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
|
|
}
|
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
2024-03-10 15:03:46 +01:00
|
|
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", session.Values["role"].(int))
|
2024-03-01 11:30:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
func WriteArticle(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
2024-03-03 13:56:49 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-06 20:53:17 +01:00
|
|
|
tags, err := db.GetTagList()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html")
|
2024-03-06 20:53:17 +01:00
|
|
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", tags)
|
2024-03-03 13:56:49 +01:00
|
|
|
}
|
2024-02-27 14:10:27 +01:00
|
|
|
}
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
func SubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
2024-02-27 14:10:27 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-03 09:16:49 +01:00
|
|
|
session, err := s.Get(r, "cookie")
|
|
|
|
if err != nil {
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
2024-03-03 09:16:49 +01:00
|
|
|
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
|
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
|
|
}
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
article := &backend.Article{
|
2024-03-10 15:03:46 +01:00
|
|
|
Title: r.PostFormValue("article-title"),
|
|
|
|
Description: r.PostFormValue("article-description"),
|
|
|
|
Content: r.PostFormValue("article-content"),
|
|
|
|
Published: false,
|
|
|
|
Rejected: false,
|
|
|
|
AuthorID: session.Values["id"].(int64),
|
|
|
|
}
|
|
|
|
|
2024-03-07 15:31:00 +01:00
|
|
|
article.ID, err = db.AddArticle(article)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.ParseForm()
|
|
|
|
tags := make([]int64, 0)
|
|
|
|
for _, tag := range r.Form["tags"] {
|
|
|
|
tagID, err := strconv.ParseInt(tag, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tags = append(tags, tagID)
|
|
|
|
}
|
|
|
|
if err = db.WriteArticleTags(article.ID, tags); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-02-27 14:10:27 +01:00
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
2024-03-03 09:16:49 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
|
|
|
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
2024-02-27 14:10:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
func ResubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
2024-03-10 15:03:46 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-04-01 15:30:24 +02:00
|
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
2024-03-10 15:03:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
title := r.PostFormValue("article-title")
|
|
|
|
description := r.PostFormValue("article-description")
|
|
|
|
content := r.PostFormValue("article-content")
|
|
|
|
|
2024-03-28 07:29:49 +01:00
|
|
|
if err = db.UpdateAttributes(
|
2024-07-13 13:58:36 +02:00
|
|
|
&backend.Attribute{Table: "articles", ID: id, AttName: "title", Value: title},
|
|
|
|
&backend.Attribute{Table: "articles", ID: id, AttName: "description", Value: description},
|
|
|
|
&backend.Attribute{Table: "articles", ID: id, AttName: "content", Value: content},
|
|
|
|
&backend.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false},
|
2024-03-10 15:03:46 +01:00
|
|
|
); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-28 07:29:49 +01:00
|
|
|
r.ParseForm()
|
|
|
|
tags := make([]int64, 0)
|
|
|
|
for _, tag := range r.Form["tags"] {
|
|
|
|
tagID, err := strconv.ParseInt(tag, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tags = append(tags, tagID)
|
|
|
|
}
|
|
|
|
if err = db.UpdateArticleTags(id, tags); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-10 15:03:46 +01:00
|
|
|
session, err := s.Get(r, "cookie")
|
|
|
|
if err != nil {
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
2024-03-10 15:03:46 +01:00
|
|
|
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
|
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
|
|
}
|
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
2024-03-10 15:03:46 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
|
|
|
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
func ShowUnpublishedArticles(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
2024-03-01 11:30:31 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-10 15:03:46 +01:00
|
|
|
unpublishedArticles, err := db.GetCertainArticles(false, false)
|
2024-03-06 20:53:17 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-10 15:03:46 +01:00
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/unpublished-articles.html")
|
2024-03-10 15:03:46 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
|
|
|
tmpl.ExecuteTemplate(w, "page-content", unpublishedArticles)
|
2024-03-01 11:30:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
func ShowRejectedArticles(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
2024-03-10 15:03:46 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-17 09:15:37 +01:00
|
|
|
type htmlData struct {
|
|
|
|
MyIDs map[int64]bool
|
2024-07-13 13:58:36 +02:00
|
|
|
RejectedArticles []*backend.Article
|
2024-03-17 09:15:37 +01:00
|
|
|
}
|
|
|
|
data := new(htmlData)
|
|
|
|
|
|
|
|
session, err := s.Get(r, "cookie")
|
2024-03-10 15:03:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-17 09:15:37 +01:00
|
|
|
data.RejectedArticles, err = db.GetCertainArticles(false, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data.MyIDs = make(map[int64]bool)
|
|
|
|
for _, article := range data.RejectedArticles {
|
|
|
|
if article.AuthorID == session.Values["id"].(int64) {
|
|
|
|
data.MyIDs[article.ID] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/rejected-articles.html")
|
2024-03-10 15:03:46 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-03-17 09:15:37 +01:00
|
|
|
tmpl.ExecuteTemplate(w, "page-content", data)
|
2024-03-10 15:03:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
func ReviewUnpublishedArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
2024-03-01 11:30:31 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-17 08:46:49 +01:00
|
|
|
type htmlData struct {
|
2024-04-03 18:08:56 +02:00
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
Content template.HTML
|
2024-07-13 13:58:36 +02:00
|
|
|
Tags []*backend.Tag
|
2024-04-03 18:08:56 +02:00
|
|
|
ID int64
|
2024-03-17 08:46:49 +01:00
|
|
|
}
|
2024-04-03 18:08:56 +02:00
|
|
|
|
|
|
|
var err error
|
2024-03-17 08:46:49 +01:00
|
|
|
data := new(htmlData)
|
|
|
|
|
2024-04-03 18:08:56 +02:00
|
|
|
data.ID, err = strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
article, err := db.GetArticle(data.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
data.Title, err = backend.ConvertToPlain(article.Title)
|
2024-04-03 18:08:56 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
data.Description, err = backend.ConvertToPlain(article.Description)
|
2024-03-01 11:30:31 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
content, err := backend.ConvertToHTML(article.Content)
|
2024-03-17 08:46:49 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-03 18:08:56 +02:00
|
|
|
data.Content = template.HTML(content)
|
2024-03-17 08:46:49 +01:00
|
|
|
|
2024-04-03 18:08:56 +02:00
|
|
|
data.Tags, err = db.GetArticleTags(data.ID)
|
2024-03-06 20:53:17 +01:00
|
|
|
if err != nil {
|
2024-03-17 08:46:49 +01:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-03-06 20:53:17 +01:00
|
|
|
return
|
2024-03-01 11:30:31 +01:00
|
|
|
}
|
2024-03-03 09:16:49 +01:00
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/to-be-published.html")
|
2024-03-03 09:16:49 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-03-17 08:46:49 +01:00
|
|
|
tmpl.ExecuteTemplate(w, "page-content", data)
|
2024-03-01 11:30:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
func ReviewRejectedArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
2024-03-10 15:03:46 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-17 08:46:49 +01:00
|
|
|
type htmlData struct {
|
|
|
|
Selected map[int64]bool
|
2024-07-13 13:58:36 +02:00
|
|
|
Article *backend.Article
|
|
|
|
Tags []*backend.Tag
|
2024-03-17 08:46:49 +01:00
|
|
|
}
|
|
|
|
data := new(htmlData)
|
|
|
|
|
2024-04-01 15:42:51 +02:00
|
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
2024-03-10 15:03:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-17 08:46:49 +01:00
|
|
|
data.Article, err = db.GetArticle(id)
|
2024-03-10 15:03:46 +01:00
|
|
|
if err != nil {
|
2024-03-17 08:46:49 +01:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-03-10 15:03:46 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-17 08:46:49 +01:00
|
|
|
data.Tags, err = db.GetTagList()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
selectedTags, err := db.GetArticleTags(id)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data.Selected = make(map[int64]bool)
|
|
|
|
for _, tag := range selectedTags {
|
|
|
|
data.Selected[tag.ID] = true
|
|
|
|
}
|
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/rework-article.html")
|
2024-03-10 15:03:46 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-03-17 08:46:49 +01:00
|
|
|
tmpl.ExecuteTemplate(w, "page-content", data)
|
2024-03-10 15:03:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
func PublishArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
2024-02-27 14:10:27 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-04-01 15:30:24 +02:00
|
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
2024-03-01 11:30:31 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-03 13:56:49 +01:00
|
|
|
session, err := s.Get(r, "cookie")
|
|
|
|
if err != nil {
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
2024-03-03 13:56:49 +01:00
|
|
|
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
|
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
|
|
}
|
|
|
|
|
2024-03-28 07:00:37 +01:00
|
|
|
if err = db.AddArticleToCurrentIssue(id); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-11 21:08:27 +01:00
|
|
|
if err = db.UpdateAttributes(
|
2024-07-13 13:58:36 +02:00
|
|
|
&backend.Attribute{Table: "articles", ID: id, AttName: "published", Value: true},
|
|
|
|
&backend.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false},
|
|
|
|
&backend.Attribute{Table: "articles", ID: id, AttName: "created", Value: time.Now().Format("2006-01-02 15:04:05")},
|
2024-03-11 21:08:27 +01:00
|
|
|
); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-10 15:03:46 +01:00
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
feed, err := backend.GenerateRSS(db, c.Title, c.Link, c.Description)
|
2024-03-28 12:51:33 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-07-13 13:58:36 +02:00
|
|
|
if err = backend.SaveRSS(c.RSSFile, feed); err != nil {
|
2024-03-28 12:51:33 +01:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
2024-03-10 15:03:46 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
|
|
|
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
func RejectArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
|
2024-03-10 15:03:46 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-04-01 15:30:24 +02:00
|
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
2024-03-10 15:03:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
session, err := s.Get(r, "cookie")
|
|
|
|
if err != nil {
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
2024-03-10 15:03:46 +01:00
|
|
|
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
|
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
|
|
}
|
|
|
|
|
2024-03-11 21:08:27 +01:00
|
|
|
if err = db.UpdateAttributes(
|
2024-07-13 13:58:36 +02:00
|
|
|
&backend.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: true},
|
2024-03-11 21:08:27 +01:00
|
|
|
); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-02-27 14:10:27 +01:00
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
2024-03-03 09:16:49 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
|
|
|
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
2024-02-27 14:10:27 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-28 07:00:37 +01:00
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
func ShowCurrentArticles(c *backend.Config, db *backend.DB) http.HandlerFunc {
|
2024-03-28 07:00:37 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
articles, err := db.GetCurrentIssueArticles()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/current-articles.html")
|
2024-03-28 07:00:37 +01:00
|
|
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", articles)
|
|
|
|
}
|
|
|
|
}
|
2024-03-29 09:07:17 +01:00
|
|
|
|
2024-07-13 13:58:36 +02:00
|
|
|
func UploadImage(c *backend.Config) http.HandlerFunc {
|
2024-03-29 09:07:17 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
file, header, err := r.FormFile("article-image")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2024-04-07 18:43:09 +02:00
|
|
|
nameStrings := strings.Split(header.Filename, ".")
|
|
|
|
extension := "." + nameStrings[len(nameStrings)-1]
|
|
|
|
filename := fmt.Sprint(uuid.New(), extension)
|
2024-04-07 11:12:07 +02:00
|
|
|
absFilepath, err := filepath.Abs(fmt.Sprint(c.PicsDir, "/", filename))
|
2024-04-07 10:58:07 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
img, err := os.Create(absFilepath)
|
2024-03-29 09:07:17 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer img.Close()
|
|
|
|
|
|
|
|
if _, err = io.Copy(img, file); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-07 18:43:09 +02:00
|
|
|
alt := strings.Join(nameStrings[0:len(nameStrings)-1], " ")
|
|
|
|
imgMD := fmt.Sprint("![", alt, "](", c.Domain, "/pics/", filename, ")")
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html")
|
2024-04-07 11:12:07 +02:00
|
|
|
template.Must(tmpl, err).ExecuteTemplate(w, "editor-images", imgMD)
|
2024-03-29 09:07:17 +01:00
|
|
|
}
|
|
|
|
}
|
2024-07-13 13:58:36 +02:00
|
|
|
|
|
|
|
func PreviewArticle(c *backend.Config, s *backend.CookieStore) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
type htmlData struct {
|
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
Content template.HTML
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
data := new(htmlData)
|
|
|
|
|
|
|
|
data.Title, err = backend.ConvertToPlain(r.PostFormValue("article-title"))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data.Description, err = backend.ConvertToPlain(r.PostFormValue("article-description"))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := backend.ConvertToHTML(r.PostFormValue("article-content"))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data.Content = template.HTML(content)
|
|
|
|
|
|
|
|
session, err := s.Get(r, "cookie")
|
|
|
|
if err != nil {
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
|
|
|
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
|
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
session.Values["article"] = data
|
|
|
|
if err = session.Save(r, w); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html")
|
|
|
|
tmpl = template.Must(tmpl, err)
|
|
|
|
tmpl.ExecuteTemplate(w, "preview", data)
|
|
|
|
}
|
|
|
|
}
|