Compare commits
20 Commits
v0.15.3
...
57953b2cfd
Author | SHA1 | Date | |
---|---|---|---|
57953b2cfd | |||
a93603eac0 | |||
1b72f05add | |||
2c6b15bc6d | |||
cafb158323 | |||
40fbb93732 | |||
b120341d78 | |||
095576a234 | |||
9199f202be | |||
3d08cc7612 | |||
a7b6fb9705 | |||
2f4d5d4c7c | |||
5b417ef87d | |||
04283d5917 | |||
d882daeb01 | |||
f99358729c | |||
7b04149a28 | |||
43c1cb6d9a | |||
9feb16a8d8 | |||
6885dfbb38 |
@ -12,11 +12,11 @@ args_bin = [
|
|||||||
"-domain localhost",
|
"-domain localhost",
|
||||||
"-feed tmp/cpolis.atom",
|
"-feed tmp/cpolis.atom",
|
||||||
"-firebase tmp/firebase.json",
|
"-firebase tmp/firebase.json",
|
||||||
|
"-images tmp/pics",
|
||||||
"-img-width 256",
|
"-img-width 256",
|
||||||
"-link https://distrikt-ni-st.de",
|
"-link https://distrikt-ni-st.de",
|
||||||
"-log tmp/cpolis.log",
|
"-log tmp/cpolis.log",
|
||||||
"-pdfs tmp/pdfs",
|
"-pdfs tmp/pdfs",
|
||||||
"-pics tmp/pics",
|
|
||||||
"-port 8080",
|
"-port 8080",
|
||||||
"-title 'Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt'",
|
"-title 'Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt'",
|
||||||
"-web web",
|
"-web web",
|
||||||
|
17
Dockerfile
Normal file
17
Dockerfile
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
FROM golang:1.23
|
||||||
|
|
||||||
|
RUN apt update
|
||||||
|
RUN apt install -y pandoc
|
||||||
|
|
||||||
|
WORKDIR /var/www/cpolis
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN curl -sLO https://github.com/tailwindlabs/tailwindcss/releases/download/v3.4.15/tailwindcss-linux-x64
|
||||||
|
RUN chmod +x tailwindcss-linux-x64
|
||||||
|
RUN mv tailwindcss-linux-x64 tailwindcss
|
||||||
|
RUN ./tailwindcss -i ./web/static/css/input.css -o ./web/static/css/style.css
|
||||||
|
|
||||||
|
RUN go build -o cpolis cmd/main.go
|
||||||
|
|
||||||
|
CMD ["./cpolis"]
|
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@ -334,9 +335,9 @@ func (db *DB) DeleteArticle(id int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func WriteArticleToFile(c *Config, articleUUID uuid.UUID, content []byte) error {
|
func WriteArticleToFile(c *Config, articleUUID uuid.UUID, content []byte) error {
|
||||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", articleUUID, ".md")
|
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(articleUUID, ".md"))
|
||||||
|
|
||||||
if err := os.WriteFile(articleAbsName, content, 0644); err != nil {
|
if err := os.WriteFile(articlePath, content, 0644); err != nil {
|
||||||
return fmt.Errorf("error writing article %v to file: %v", articleUUID, err)
|
return fmt.Errorf("error writing article %v to file: %v", articleUUID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package backend
|
package backend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@ -12,6 +13,9 @@ func GenerateAtomFeed(c *Config, db *DB) (*string, error) {
|
|||||||
feed := atom.NewFeed(c.Title)
|
feed := atom.NewFeed(c.Title)
|
||||||
feed.ID = atom.NewID("urn:feed:1")
|
feed.ID = atom.NewID("urn:feed:1")
|
||||||
feed.Subtitle = atom.NewText("text", c.Description)
|
feed.Subtitle = atom.NewText("text", c.Description)
|
||||||
|
if feed.Subtitle == nil {
|
||||||
|
return nil, errors.New("feed subtitle was not created")
|
||||||
|
}
|
||||||
|
|
||||||
linkID := feed.AddLink(atom.NewLink(c.Link))
|
linkID := feed.AddLink(atom.NewLink(c.Link))
|
||||||
feed.Links[linkID].Rel = "self"
|
feed.Links[linkID].Rel = "self"
|
||||||
@ -34,15 +38,24 @@ func GenerateAtomFeed(c *Config, db *DB) (*string, error) {
|
|||||||
entry.ID = atom.NewID(fmt.Sprint("urn:entry:", article.ID))
|
entry.ID = atom.NewID(fmt.Sprint("urn:entry:", article.ID))
|
||||||
entry.Published = atom.NewDate(article.Created)
|
entry.Published = atom.NewDate(article.Created)
|
||||||
entry.Content = atom.NewContent(atom.OutOfLine, "text/html", fmt.Sprint(c.Domain, "/article/serve/", article.UUID))
|
entry.Content = atom.NewContent(atom.OutOfLine, "text/html", fmt.Sprint(c.Domain, "/article/serve/", article.UUID))
|
||||||
|
if entry.Content == nil {
|
||||||
|
return nil, errors.New("entry content was not created")
|
||||||
|
}
|
||||||
|
|
||||||
if article.AutoGenerated {
|
if article.AutoGenerated {
|
||||||
entry.Summary = atom.NewText("text", "automatically generated")
|
entry.Summary = atom.NewText("text", "automatically generated")
|
||||||
|
if entry.Summary == nil {
|
||||||
|
return nil, errors.New("entry summary was not created")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
articleSummary, err := ConvertToPlain(article.Summary)
|
articleSummary, err := ConvertToPlain(article.Summary)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error converting description to plain text for Atom feed: %v", err)
|
return nil, fmt.Errorf("error converting description to plain text for Atom feed: %v", err)
|
||||||
}
|
}
|
||||||
entry.Summary = atom.NewText("text", articleSummary)
|
entry.Summary = atom.NewText("text", articleSummary)
|
||||||
|
if entry.Summary == nil {
|
||||||
|
return nil, errors.New("entry summary was not created")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(article.BannerLink) > 0 {
|
if len(article.BannerLink) > 0 {
|
||||||
|
@ -20,10 +20,10 @@ type Config struct {
|
|||||||
Description string
|
Description string
|
||||||
Domain string
|
Domain string
|
||||||
FirebaseKey string
|
FirebaseKey string
|
||||||
|
ImgDir string
|
||||||
Link string
|
Link string
|
||||||
LogFile string
|
LogFile string
|
||||||
PDFDir string
|
PDFDir string
|
||||||
PicsDir string
|
|
||||||
Port string
|
Port string
|
||||||
Title string
|
Title string
|
||||||
Version string
|
Version string
|
||||||
@ -43,27 +43,24 @@ func newConfig() *Config {
|
|||||||
ConfigFile: "/etc/cpolis/config.toml",
|
ConfigFile: "/etc/cpolis/config.toml",
|
||||||
CookieExpiryHours: 24 * 30,
|
CookieExpiryHours: 24 * 30,
|
||||||
DBName: "cpolis",
|
DBName: "cpolis",
|
||||||
FirebaseKey: "/var/www/cpolis/serviceAccountKey.json",
|
FirebaseKey: "/etc/cpolis/serviceAccountKey.json",
|
||||||
|
ImgDir: "/var/www/cpolis/images",
|
||||||
LogFile: "/var/log/cpolis.log",
|
LogFile: "/var/log/cpolis.log",
|
||||||
MaxBannerHeight: 1080,
|
MaxBannerHeight: 1080,
|
||||||
MaxBannerWidth: 1920,
|
MaxBannerWidth: 1920,
|
||||||
MaxImgHeight: 1080,
|
MaxImgHeight: 1080,
|
||||||
MaxImgWidth: 1920,
|
MaxImgWidth: 1920,
|
||||||
PDFDir: "/var/www/cpolis/pdfs",
|
PDFDir: "/var/www/cpolis/pdfs",
|
||||||
PicsDir: "/var/www/cpolis/pics",
|
Port: ":1664",
|
||||||
Port: ":8080",
|
Version: "v0.16.0",
|
||||||
Version: "v0.15.3",
|
|
||||||
WebDir: "/var/www/cpolis/web",
|
WebDir: "/var/www/cpolis/web",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mkDir(path string, perm fs.FileMode) (string, error) {
|
func mkDir(path string, perm fs.FileMode) (string, error) {
|
||||||
var err error
|
name := filepath.Base(path)
|
||||||
|
|
||||||
stringSlice := strings.Split(path, "/")
|
path, err := filepath.Abs(path)
|
||||||
name := stringSlice[len(stringSlice)-1]
|
|
||||||
|
|
||||||
path, err = filepath.Abs(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("error finding absolute path for %v directory: %v", name, err)
|
return "", fmt.Errorf("error finding absolute path for %v directory: %v", name, err)
|
||||||
}
|
}
|
||||||
@ -82,20 +79,20 @@ func mkFile(path string, filePerm, dirPerm fs.FileMode) (string, error) {
|
|||||||
return "", fmt.Errorf("error finding absolute path for %v: %v", path, err)
|
return "", fmt.Errorf("error finding absolute path for %v: %v", path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
stringSlice := strings.Split(path, "/")
|
|
||||||
_, err = os.Stat(path)
|
_, err = os.Stat(path)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
dir := strings.Join(stringSlice[:len(stringSlice)-1], "/")
|
dir := filepath.Dir(path)
|
||||||
if err = os.MkdirAll(dir, dirPerm); err != nil {
|
if err = os.MkdirAll(dir, dirPerm); err != nil {
|
||||||
return "", fmt.Errorf("error creating %v: %v", dir, err)
|
return "", fmt.Errorf("error creating %v: %v", dir, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fileName := stringSlice[len(stringSlice)-1]
|
fileName := filepath.Base(path)
|
||||||
file, err := os.Create(filepath.Join(dir, fileName))
|
file, err := os.Create(filepath.Join(dir, fileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("error creating %v: %v", fileName, err)
|
return "", fmt.Errorf("error creating %v: %v", fileName, err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
if err = file.Chmod(filePerm); err != nil {
|
if err = file.Chmod(filePerm); err != nil {
|
||||||
return "", fmt.Errorf("error setting permissions for %v: %v", fileName, err)
|
return "", fmt.Errorf("error setting permissions for %v: %v", fileName, err)
|
||||||
}
|
}
|
||||||
@ -116,10 +113,10 @@ func (c *Config) handleCliArgs() error {
|
|||||||
flag.StringVar(&c.Description, "desc", c.Description, "channel description")
|
flag.StringVar(&c.Description, "desc", c.Description, "channel description")
|
||||||
flag.StringVar(&c.Domain, "domain", c.Domain, "domain name")
|
flag.StringVar(&c.Domain, "domain", c.Domain, "domain name")
|
||||||
flag.StringVar(&c.FirebaseKey, "firebase", c.FirebaseKey, "Firebase service account key file")
|
flag.StringVar(&c.FirebaseKey, "firebase", c.FirebaseKey, "Firebase service account key file")
|
||||||
|
flag.StringVar(&c.ImgDir, "images", c.ImgDir, "images directory")
|
||||||
flag.StringVar(&c.Link, "link", c.Link, "channel Link")
|
flag.StringVar(&c.Link, "link", c.Link, "channel Link")
|
||||||
flag.StringVar(&c.LogFile, "log", c.LogFile, "log file")
|
flag.StringVar(&c.LogFile, "log", c.LogFile, "log file")
|
||||||
flag.StringVar(&c.PDFDir, "pdfs", c.PDFDir, "pdf directory")
|
flag.StringVar(&c.PDFDir, "pdfs", c.PDFDir, "pdf directory")
|
||||||
flag.StringVar(&c.PicsDir, "pics", c.PicsDir, "pictures directory")
|
|
||||||
flag.StringVar(&c.Title, "title", c.Title, "channel title")
|
flag.StringVar(&c.Title, "title", c.Title, "channel title")
|
||||||
flag.StringVar(&c.WebDir, "web", c.WebDir, "web directory")
|
flag.StringVar(&c.WebDir, "web", c.WebDir, "web directory")
|
||||||
flag.IntVar(&c.CookieExpiryHours, "cookie-expiry-hours", c.CookieExpiryHours, "cookies expire after this amount of hours")
|
flag.IntVar(&c.CookieExpiryHours, "cookie-expiry-hours", c.CookieExpiryHours, "cookies expire after this amount of hours")
|
||||||
@ -223,6 +220,18 @@ func (c *Config) setupConfig(cliConfig *Config) error {
|
|||||||
return fmt.Errorf("error setting up file: %v", err)
|
return fmt.Errorf("error setting up file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cliConfig.ImgDir != defaultConfig.ImgDir {
|
||||||
|
c.ImgDir = cliConfig.ImgDir
|
||||||
|
}
|
||||||
|
c.ImgDir, err = filepath.Abs(c.ImgDir)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error setting absolute filepath for PicsDir: %v", err)
|
||||||
|
}
|
||||||
|
c.ImgDir, err = mkDir(c.ImgDir, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error setting up directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
if cliConfig.Link != defaultConfig.Link {
|
if cliConfig.Link != defaultConfig.Link {
|
||||||
c.Link = cliConfig.Link
|
c.Link = cliConfig.Link
|
||||||
}
|
}
|
||||||
@ -267,18 +276,6 @@ func (c *Config) setupConfig(cliConfig *Config) error {
|
|||||||
return fmt.Errorf("error setting up directory: %v", err)
|
return fmt.Errorf("error setting up directory: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cliConfig.PicsDir != defaultConfig.PicsDir {
|
|
||||||
c.PicsDir = cliConfig.PicsDir
|
|
||||||
}
|
|
||||||
c.PicsDir, err = filepath.Abs(c.PicsDir)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error setting absolute filepath for PicsDir: %v", err)
|
|
||||||
}
|
|
||||||
c.PicsDir, err = mkDir(c.PicsDir, 0700)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error setting up directory: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if cliConfig.Port != defaultConfig.Port {
|
if cliConfig.Port != defaultConfig.Port {
|
||||||
c.Port = cliConfig.Port
|
c.Port = cliConfig.Port
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ func ConvertToMarkdown(c *Config, filename string) ([]byte, error) {
|
|||||||
return nil, fmt.Errorf("error reading markdown file: %v", err)
|
return nil, fmt.Errorf("error reading markdown file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
imageNames, err := filepath.Glob(filepath.Join(tmpDir, "/media/*"))
|
imageNames, err := filepath.Glob(filepath.Join(tmpDir, "media", "*"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting docx images from temporary directory: %v", err)
|
return nil, fmt.Errorf("error getting docx images from temporary directory: %v", err)
|
||||||
}
|
}
|
||||||
@ -45,7 +45,7 @@ func ConvertToMarkdown(c *Config, filename string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
defer image.Close()
|
defer image.Close()
|
||||||
|
|
||||||
newImageName, err := SaveImage(image, c.MaxImgHeight, c.MaxImgWidth, c.PicsDir)
|
newImageName, err := SaveImage(image, c.MaxImgHeight, c.MaxImgWidth, c.ImgDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error saving image %v: %v", name, err)
|
return nil, fmt.Errorf("error saving image %v: %v", name, err)
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,53 @@ import (
|
|||||||
|
|
||||||
var ErrUnsupportedFormat error = image.ErrFormat // used internally by imaging
|
var ErrUnsupportedFormat error = image.ErrFormat // used internally by imaging
|
||||||
|
|
||||||
|
func checkImageUsage(c *Config, db *DB, name string) (bool, error) {
|
||||||
|
imageWasFound := false
|
||||||
|
|
||||||
|
if err := filepath.Walk(c.ArticleDir, func(path string, info fs.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error walking articles filepath: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !info.IsDir() {
|
||||||
|
mdFile, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error opening article %v: %v", info.Name(), err)
|
||||||
|
}
|
||||||
|
defer mdFile.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(mdFile)
|
||||||
|
for scanner.Scan() {
|
||||||
|
if strings.Contains(scanner.Text(), name) {
|
||||||
|
imageWasFound = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return scanner.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return false, fmt.Errorf("error walking articles filepath: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !imageWasFound {
|
||||||
|
users, err := db.GetAllUsers(c)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("error getting all users: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, user := range users {
|
||||||
|
if name == user.ProfilePicLink {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return imageWasFound, nil
|
||||||
|
}
|
||||||
|
|
||||||
func SaveImage(src io.Reader, maxHeight, maxWidth int, path string) (string, error) {
|
func SaveImage(src io.Reader, maxHeight, maxWidth int, path string) (string, error) {
|
||||||
img, err := imaging.Decode(src, imaging.AutoOrientation(true))
|
img, err := imaging.Decode(src, imaging.AutoOrientation(true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -48,7 +95,7 @@ func SaveImage(src io.Reader, maxHeight, maxWidth int, path string) (string, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CleanUpImages(c *Config, db *DB) error {
|
func CleanUpImages(c *Config, db *DB) error {
|
||||||
if err := filepath.Walk(c.PicsDir, func(path string, info fs.FileInfo, err error) error {
|
if err := filepath.Walk(c.ImgDir, func(path string, info fs.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error walking images filepath: %v", err)
|
return fmt.Errorf("error walking images filepath: %v", err)
|
||||||
}
|
}
|
||||||
@ -56,45 +103,10 @@ func CleanUpImages(c *Config, db *DB) error {
|
|||||||
if !info.IsDir() {
|
if !info.IsDir() {
|
||||||
imageName := info.Name()
|
imageName := info.Name()
|
||||||
imagePath := path
|
imagePath := path
|
||||||
imageWasFound := false
|
|
||||||
|
|
||||||
if err = filepath.Walk(c.ArticleDir, func(path string, info fs.FileInfo, err error) error {
|
imageWasFound, err := checkImageUsage(c, db, imageName)
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error walking articles filepath: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !info.IsDir() {
|
|
||||||
mdFile, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error opening article %v: %v", info.Name(), err)
|
|
||||||
}
|
|
||||||
defer mdFile.Close()
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(mdFile)
|
|
||||||
|
|
||||||
for scanner.Scan() {
|
|
||||||
if strings.Contains(scanner.Text(), imageName) {
|
|
||||||
imageWasFound = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return scanner.Err()
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
return fmt.Errorf("error walking articles filepath: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
users, err := db.GetAllUsers(c)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error getting all users: %v", err)
|
return fmt.Errorf("error checking image usage: %v", err)
|
||||||
}
|
|
||||||
|
|
||||||
for _, user := range users {
|
|
||||||
if imageName == user.ProfilePicLink {
|
|
||||||
imageWasFound = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !imageWasFound {
|
if !imageWasFound {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
b "streifling.com/jason/cpolis/cmd/backend"
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
@ -56,8 +57,8 @@ func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.UUID, ".md")
|
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))
|
||||||
contentBytes, err := os.ReadFile(articleAbsName)
|
contentBytes, err := os.ReadFile(articlePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -10,12 +10,12 @@ import (
|
|||||||
|
|
||||||
func ServeImage(c *b.Config, s map[string]*f.Session) http.HandlerFunc {
|
func ServeImage(c *b.Config, s map[string]*f.Session) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if _, err := f.ManageSession(w, r, c, s); err != nil {
|
if !f.SessionIsActive(r, s) {
|
||||||
if !tokenIsVerified(w, r, c) {
|
if !tokenIsVerified(w, r, c) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
http.ServeFile(w, r, filepath.Join(c.PicsDir, r.PathValue("pic")))
|
http.ServeFile(w, r, filepath.Join(c.ImgDir, r.PathValue("pic")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
b "streifling.com/jason/cpolis/cmd/backend"
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
)
|
)
|
||||||
@ -42,6 +43,6 @@ func ServePDF(c *b.Config) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
http.ServeFile(w, r, c.PDFDir+"/"+r.PathValue("id"))
|
http.ServeFile(w, r, filepath.Join(c.PDFDir, r.PathValue("id")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -456,8 +456,8 @@ func ReviewRejectedArticle(c *b.Config, db *b.DB, s map[string]*Session) http.Ha
|
|||||||
|
|
||||||
data.Image = data.Article.BannerLink
|
data.Image = data.Article.BannerLink
|
||||||
|
|
||||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", data.Article.UUID, ".md")
|
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(data.Article.UUID, ".md"))
|
||||||
content, err := os.ReadFile(articleAbsName)
|
content, err := os.ReadFile(articlePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
@ -587,7 +587,7 @@ func PublishArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFu
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = os.Remove(fmt.Sprint(c.ArticleDir, "/", oldArticle.UUID, ".md")); err != nil {
|
if err = os.Remove(filepath.Join(c.ArticleDir, fmt.Sprint(oldArticle.UUID, ".md"))); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@ -765,8 +765,8 @@ func ReviewArticle(c *b.Config, db *b.DB, s map[string]*Session, action, title,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.UUID, ".md")
|
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))
|
||||||
content, err := os.ReadFile(articleAbsName)
|
content, err := os.ReadFile(articlePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
@ -840,7 +840,7 @@ func DeleteArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFun
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = os.Remove(fmt.Sprint(c.ArticleDir, "/", article.UUID, ".md")); err != nil {
|
if err = os.Remove(filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@ -918,8 +918,8 @@ func AllowEditArticle(c *b.Config, db *b.DB, s map[string]*Session) http.Handler
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
src := fmt.Sprint(c.ArticleDir, "/", oldArticle.UUID, ".md")
|
src := filepath.Join(c.ArticleDir, fmt.Sprint(oldArticle.UUID, ".md"))
|
||||||
dst := fmt.Sprint(c.ArticleDir, "/", newArticle.UUID, ".md")
|
dst := filepath.Join(c.ArticleDir, fmt.Sprint(newArticle.UUID, ".md"))
|
||||||
if err = b.CopyFile(src, dst); err != nil {
|
if err = b.CopyFile(src, dst); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
@ -995,7 +995,7 @@ func EditArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc
|
|||||||
|
|
||||||
data.Image = data.Article.BannerLink
|
data.Image = data.Article.BannerLink
|
||||||
|
|
||||||
content, err := os.ReadFile(fmt.Sprint(c.ArticleDir, "/", data.Article.UUID, ".md"))
|
content, err := os.ReadFile(filepath.Join(c.ArticleDir, fmt.Sprint(data.Article.UUID, ".md")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -25,7 +25,7 @@ func UploadEasyMDEImage(c *b.Config, s map[string]*Session) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
filename, err := b.SaveImage(file, c.MaxImgHeight, c.MaxImgWidth, c.PicsDir+"/")
|
filename, err := b.SaveImage(file, c.MaxImgHeight, c.MaxImgWidth, c.ImgDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == b.ErrUnsupportedFormat {
|
if err == b.ErrUnsupportedFormat {
|
||||||
http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest)
|
http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest)
|
||||||
@ -57,7 +57,7 @@ func UploadImage(c *b.Config, s map[string]*Session, fileKey, htmlFile, htmlTemp
|
|||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
filename, err := b.SaveImage(file, c.MaxBannerHeight, c.MaxBannerWidth, c.PicsDir+"/")
|
filename, err := b.SaveImage(file, c.MaxBannerHeight, c.MaxBannerWidth, c.ImgDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == b.ErrUnsupportedFormat {
|
if err == b.ErrUnsupportedFormat {
|
||||||
http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest)
|
http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest)
|
||||||
|
@ -58,8 +58,8 @@ func PublishLatestIssue(c *b.Config, db *b.DB, s map[string]*Session) http.Handl
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.UUID, ".md")
|
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))
|
||||||
if err = os.WriteFile(articleAbsName, content, 0644); err != nil {
|
if err = os.WriteFile(articlePath, content, 0644); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -64,6 +64,18 @@ func StartSessions() (map[string]*Session, chan string) {
|
|||||||
return sessions, sessionExpiryChan
|
return sessions, sessionExpiryChan
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SessionIsActive is used for verifying that the user is logged in and returns
|
||||||
|
// a bool.
|
||||||
|
func SessionIsActive(r *http.Request, s map[string]*Session) bool {
|
||||||
|
cookie, err := r.Cookie("cpolis_session")
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
_, ok := s[cookie.Value]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
// ManageSession is used for verifying that the user is logged in and returns
|
// ManageSession is used for verifying that the user is logged in and returns
|
||||||
// their session and an error. It also handles cases where the user is not
|
// their session and an error. It also handles cases where the user is not
|
||||||
// logged in.
|
// logged in.
|
||||||
|
22
docker-compose.yml
Normal file
22
docker-compose.yml
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
services:
|
||||||
|
app:
|
||||||
|
build: .
|
||||||
|
ports:
|
||||||
|
- "1664:1664"
|
||||||
|
volumes:
|
||||||
|
- /etc/cpolis/config.toml:/etc/cpolis/config.toml
|
||||||
|
- /etc/cpolis/serviceAccountKey.json:/etc/cpolis/serviceAccountKey.json
|
||||||
|
- /var/log/cpolis.log:/var/log/cpolis.log
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
|
||||||
|
db:
|
||||||
|
image: mariadb:latest
|
||||||
|
environment:
|
||||||
|
MYSQL_DATABASE: cpolis
|
||||||
|
MYSQL_USER: cpolis
|
||||||
|
MYSQL_PASSWORD: ${DB_PASS}
|
||||||
|
ports:
|
||||||
|
- "3306:3306"
|
||||||
|
volumes:
|
||||||
|
- ./create_db.sql:/docker-entrypoint-initdb.d/create_db.sql
|
7
docker-start.sh
Executable file
7
docker-start.sh
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#! /bin/sh -
|
||||||
|
|
||||||
|
read -sp "Enter DB password: " DB_PASS
|
||||||
|
echo
|
||||||
|
|
||||||
|
export DB_PASS
|
||||||
|
docker-compose up -d
|
@ -42,7 +42,7 @@
|
|||||||
<p>{{.Version}} - <strong>Alpha: Drastische Änderungen und Fehler vorbehalten.</strong></p>
|
<p>{{.Version}} - <strong>Alpha: Drastische Änderungen und Fehler vorbehalten.</strong></p>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
<script src="https://unpkg.com/htmx.org@latest"></script>
|
<script src="https://unpkg.com/htmx.org@2.0.4"></script>
|
||||||
<script src="https://unpkg.com/easymde/dist/easymde.min.js"></script>
|
<script src="https://unpkg.com/easymde/dist/easymde.min.js"></script>
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
Reference in New Issue
Block a user