Compare commits

...

17 Commits

Author SHA1 Message Date
40fbb93732 Add docker-start.sh 2025-01-24 18:28:29 +01:00
b120341d78 Correct docker-compose.yml 2025-01-24 18:28:18 +01:00
095576a234 Add tailwindcss to Dockerfile 2025-01-24 18:13:13 +01:00
9199f202be Check atom feed for nil returns 2025-01-24 17:42:05 +01:00
3d08cc7612 Make background image check way more efficient 2025-01-20 20:25:50 +01:00
a7b6fb9705 Correct probably last occurance of manually set path delimeter 2025-01-19 20:41:48 +01:00
2f4d5d4c7c Correct comment 2025-01-19 20:35:35 +01:00
5b417ef87d Change ValidateSession() to SessionIsActive() which only returns a bool 2025-01-19 20:34:12 +01:00
04283d5917 Restore old way of managing session while keeping slimmed down version 2025-01-19 20:29:10 +01:00
d882daeb01 Correct command line flag for ImgDir 2025-01-19 20:15:56 +01:00
f99358729c Create ValidateSession() to not have unwanted side effects when validating session 2025-01-19 20:10:51 +01:00
7b04149a28 Rename PicsDir to ImgDir 2025-01-19 20:10:06 +01:00
43c1cb6d9a Use proper filepaths for docker-compose 2025-01-19 20:08:58 +01:00
9feb16a8d8 Change filepaths to use filepath.Join() where possible 2025-01-19 20:07:32 +01:00
6885dfbb38 Add the ability to run cpolis from docker 2025-01-19 15:17:38 +01:00
86a16629fd Change htmx version to @latest 2025-01-19 10:16:07 +01:00
6d402aca32 Update copyright notice 2025-01-19 10:13:18 +01:00
17 changed files with 171 additions and 89 deletions

View File

@ -12,11 +12,11 @@ args_bin = [
"-domain localhost",
"-feed tmp/cpolis.atom",
"-firebase tmp/firebase.json",
"-images tmp/pics",
"-img-width 256",
"-link https://distrikt-ni-st.de",
"-log tmp/cpolis.log",
"-pdfs tmp/pdfs",
"-pics tmp/pics",
"-port 8080",
"-title 'Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt'",
"-web web",

16
Dockerfile Normal file
View File

@ -0,0 +1,16 @@
FROM golang:latest
RUN apk add --no-cache pandoc
WORKDIR /var/www/cpolis
COPY . .
RUN curl -sLO https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-linux-x64
RUN chmod +x tailwindcss-linux-x64
RUN mv tailwindcss-linux-x64 tailwindcss
RUN ./web/static/css/input.css -o ./web/static/css/style.css
RUN go build -o cpolis cmd/main.go
CMD ["./cpolis"]

View File

@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"time"
"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 {
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)
}

View File

@ -1,6 +1,7 @@
package backend
import (
"errors"
"fmt"
"io"
"os"
@ -12,6 +13,9 @@ func GenerateAtomFeed(c *Config, db *DB) (*string, error) {
feed := atom.NewFeed(c.Title)
feed.ID = atom.NewID("urn:feed:1")
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))
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.Published = atom.NewDate(article.Created)
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 {
entry.Summary = atom.NewText("text", "automatically generated")
if entry.Summary == nil {
return nil, errors.New("entry summary was not created")
}
} else {
articleSummary, err := ConvertToPlain(article.Summary)
if err != nil {
return nil, fmt.Errorf("error converting description to plain text for Atom feed: %v", err)
}
entry.Summary = atom.NewText("text", articleSummary)
if entry.Summary == nil {
return nil, errors.New("entry summary was not created")
}
}
if len(article.BannerLink) > 0 {

View File

@ -20,10 +20,10 @@ type Config struct {
Description string
Domain string
FirebaseKey string
ImgDir string
Link string
LogFile string
PDFDir string
PicsDir string
Port string
Title string
Version string
@ -43,27 +43,24 @@ func newConfig() *Config {
ConfigFile: "/etc/cpolis/config.toml",
CookieExpiryHours: 24 * 30,
DBName: "cpolis",
FirebaseKey: "/var/www/cpolis/serviceAccountKey.json",
FirebaseKey: "/etc/cpolis/serviceAccountKey.json",
ImgDir: "/var/www/cpolis/images",
LogFile: "/var/log/cpolis.log",
MaxBannerHeight: 1080,
MaxBannerWidth: 1920,
MaxImgHeight: 1080,
MaxImgWidth: 1920,
PDFDir: "/var/www/cpolis/pdfs",
PicsDir: "/var/www/cpolis/pics",
Port: ":8080",
Version: "v0.15.2",
Port: ":1664",
Version: "v0.16.0",
WebDir: "/var/www/cpolis/web",
}
}
func mkDir(path string, perm fs.FileMode) (string, error) {
var err error
name := filepath.Base(path)
stringSlice := strings.Split(path, "/")
name := stringSlice[len(stringSlice)-1]
path, err = filepath.Abs(path)
path, err := filepath.Abs(path)
if err != nil {
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)
}
stringSlice := strings.Split(path, "/")
_, err = os.Stat(path)
if os.IsNotExist(err) {
dir := strings.Join(stringSlice[:len(stringSlice)-1], "/")
dir := filepath.Dir(path)
if err = os.MkdirAll(dir, dirPerm); err != nil {
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))
if err != nil {
return "", fmt.Errorf("error creating %v: %v", fileName, err)
}
defer file.Close()
if err = file.Chmod(filePerm); err != nil {
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.Domain, "domain", c.Domain, "domain name")
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.LogFile, "log", c.LogFile, "log file")
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.WebDir, "web", c.WebDir, "web directory")
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)
}
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 {
c.Link = cliConfig.Link
}
@ -267,18 +276,6 @@ func (c *Config) setupConfig(cliConfig *Config) error {
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 {
c.Port = cliConfig.Port
}

View File

@ -33,7 +33,7 @@ func ConvertToMarkdown(c *Config, filename string) ([]byte, error) {
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 {
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()
newImageName, err := SaveImage(image, c.MaxImgHeight, c.MaxImgWidth, c.PicsDir)
newImageName, err := SaveImage(image, c.MaxImgHeight, c.MaxImgWidth, c.ImgDir)
if err != nil {
return nil, fmt.Errorf("error saving image %v: %v", name, err)
}

View File

@ -17,6 +17,53 @@ import (
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) {
img, err := imaging.Decode(src, imaging.AutoOrientation(true))
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 {
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 {
return fmt.Errorf("error walking images filepath: %v", err)
}
@ -56,45 +103,10 @@ func CleanUpImages(c *Config, db *DB) error {
if !info.IsDir() {
imageName := info.Name()
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 {
return fmt.Errorf("error getting all users: %v", err)
}
for _, user := range users {
if imageName == user.ProfilePicLink {
imageWasFound = true
}
return fmt.Errorf("error checking image usage: %v", err)
}
if !imageWasFound {

View File

@ -5,6 +5,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"github.com/google/uuid"
b "streifling.com/jason/cpolis/cmd/backend"
@ -56,8 +57,8 @@ func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc {
return
}
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.UUID, ".md")
contentBytes, err := os.ReadFile(articleAbsName)
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))
contentBytes, err := os.ReadFile(articlePath)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)

View File

@ -10,12 +10,12 @@ import (
func ServeImage(c *b.Config, s map[string]*f.Session) http.HandlerFunc {
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) {
return
}
}
http.ServeFile(w, r, filepath.Join(c.PicsDir, r.PathValue("pic")))
http.ServeFile(w, r, filepath.Join(c.ImgDir, r.PathValue("pic")))
}
}

View File

@ -5,6 +5,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
b "streifling.com/jason/cpolis/cmd/backend"
)
@ -42,6 +43,6 @@ func ServePDF(c *b.Config) http.HandlerFunc {
return
}
http.ServeFile(w, r, c.PDFDir+"/"+r.PathValue("id"))
http.ServeFile(w, r, filepath.Join(c.PDFDir, r.PathValue("id")))
}
}

View File

@ -456,8 +456,8 @@ func ReviewRejectedArticle(c *b.Config, db *b.DB, s map[string]*Session) http.Ha
data.Image = data.Article.BannerLink
articleAbsName := fmt.Sprint(c.ArticleDir, "/", data.Article.UUID, ".md")
content, err := os.ReadFile(articleAbsName)
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(data.Article.UUID, ".md"))
content, err := os.ReadFile(articlePath)
if err != nil {
log.Println(err)
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
}
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)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@ -765,8 +765,8 @@ func ReviewArticle(c *b.Config, db *b.DB, s map[string]*Session, action, title,
return
}
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.UUID, ".md")
content, err := os.ReadFile(articleAbsName)
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))
content, err := os.ReadFile(articlePath)
if err != nil {
log.Println(err)
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
}
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)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@ -918,8 +918,8 @@ func AllowEditArticle(c *b.Config, db *b.DB, s map[string]*Session) http.Handler
return
}
src := fmt.Sprint(c.ArticleDir, "/", oldArticle.UUID, ".md")
dst := fmt.Sprint(c.ArticleDir, "/", newArticle.UUID, ".md")
src := filepath.Join(c.ArticleDir, fmt.Sprint(oldArticle.UUID, ".md"))
dst := filepath.Join(c.ArticleDir, fmt.Sprint(newArticle.UUID, ".md"))
if err = b.CopyFile(src, dst); err != nil {
log.Println(err)
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
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 {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)

View File

@ -25,7 +25,7 @@ func UploadEasyMDEImage(c *b.Config, s map[string]*Session) http.HandlerFunc {
}
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 == b.ErrUnsupportedFormat {
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()
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 == b.ErrUnsupportedFormat {
http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest)

View File

@ -58,8 +58,8 @@ func PublishLatestIssue(c *b.Config, db *b.DB, s map[string]*Session) http.Handl
return
}
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.UUID, ".md")
if err = os.WriteFile(articleAbsName, content, 0644); err != nil {
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))
if err = os.WriteFile(articlePath, content, 0644); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return

View File

@ -64,6 +64,18 @@ func StartSessions() (map[string]*Session, chan string) {
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
// their session and an error. It also handles cases where the user is not
// logged in.

22
docker-compose.yml Normal file
View 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 Normal file
View File

@ -0,0 +1,7 @@
#! /bin/bash -
read -sp "Enter DB password: " DB_PASS
echo
export DB_PASS
docker-compose up -d

View File

@ -38,11 +38,11 @@
</main>
<footer class="text-center text-gray-500 my-8">
<p>&copy; 2024 Jason Streifling. Alle Rechte vorbehalten.</p>
<p>&copy; 2025 Jason Streifling. Alle Rechte vorbehalten.</p>
<p>{{.Version}} - <strong>Alpha: Drastische Änderungen und Fehler vorbehalten.</strong></p>
</footer>
<script src="https://unpkg.com/htmx.org@2.0.3"></script>
<script src="https://unpkg.com/htmx.org@latest"></script>
<script src="https://unpkg.com/easymde/dist/easymde.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {