Compare commits
53 Commits
095576a234
...
v0.15.3
Author | SHA1 | Date | |
---|---|---|---|
ed614026ec | |||
87f8786c43 | |||
acae07b8f3 | |||
1b100483de | |||
e03fd78ea9 | |||
370ef205a9 | |||
d328ddb749 | |||
5dc5590da9 | |||
364112a0a4 | |||
a38523e933 | |||
200672dae2 | |||
3d3aad88c8 | |||
e4e43d1a83 | |||
737a9ec314 | |||
1ebe0380ee | |||
d62d71b5d1 | |||
b36e0ea503 | |||
bc4d8fa37e | |||
d2b21e7405 | |||
e3c192359f | |||
46532e4c85 | |||
c722135a56 | |||
887fa863bc | |||
74d71cfb6a | |||
ca7e7cddd3 | |||
94431a2aa9 | |||
5b1f20c5bc | |||
d0c566f8df | |||
5e586aa49a | |||
66b2743d3d | |||
3723b2b5e6 | |||
ce788bfd50 | |||
230a6278cc | |||
42d6e0c198 | |||
e1af2979af | |||
f6dedc6f10 | |||
cdf0a49550 | |||
c3c0650210 | |||
d077f700d8 | |||
ec752b1c66 | |||
46aef4f12f | |||
1b29e328cf | |||
e50cb819f3 | |||
c32e38ca10 | |||
d7c8c7a43a | |||
1cd3edc90c | |||
0e768c9f61 | |||
1fcd775cc5 | |||
203a1ed147 | |||
ef1914ee5c | |||
084b101e31 | |||
b2db128aa9 | |||
081e880fb6 |
@ -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",
|
||||||
|
16
Dockerfile
16
Dockerfile
@ -1,16 +0,0 @@
|
|||||||
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"]
|
|
@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@ -335,9 +334,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 {
|
||||||
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(articleUUID, ".md"))
|
articleAbsName := fmt.Sprint(c.ArticleDir, "/", articleUUID, ".md")
|
||||||
|
|
||||||
if err := os.WriteFile(articlePath, content, 0644); err != nil {
|
if err := os.WriteFile(articleAbsName, 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,7 +1,6 @@
|
|||||||
package backend
|
package backend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@ -13,9 +12,6 @@ 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"
|
||||||
@ -38,24 +34,15 @@ 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,24 +43,27 @@ func newConfig() *Config {
|
|||||||
ConfigFile: "/etc/cpolis/config.toml",
|
ConfigFile: "/etc/cpolis/config.toml",
|
||||||
CookieExpiryHours: 24 * 30,
|
CookieExpiryHours: 24 * 30,
|
||||||
DBName: "cpolis",
|
DBName: "cpolis",
|
||||||
FirebaseKey: "/etc/cpolis/serviceAccountKey.json",
|
FirebaseKey: "/var/www/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",
|
||||||
Port: ":1664",
|
PicsDir: "/var/www/cpolis/pics",
|
||||||
Version: "v0.16.0",
|
Port: ":8080",
|
||||||
|
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) {
|
||||||
name := filepath.Base(path)
|
var err error
|
||||||
|
|
||||||
path, err := filepath.Abs(path)
|
stringSlice := strings.Split(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)
|
||||||
}
|
}
|
||||||
@ -79,20 +82,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 := filepath.Dir(path)
|
dir := strings.Join(stringSlice[:len(stringSlice)-1], "/")
|
||||||
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 := filepath.Base(path)
|
fileName := stringSlice[len(stringSlice)-1]
|
||||||
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)
|
||||||
}
|
}
|
||||||
@ -113,10 +116,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")
|
||||||
@ -220,18 +223,6 @@ 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
|
||||||
}
|
}
|
||||||
@ -276,6 +267,18 @@ 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.ImgDir)
|
newImageName, err := SaveImage(image, c.MaxImgHeight, c.MaxImgWidth, c.PicsDir)
|
||||||
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,53 +17,6 @@ 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 {
|
||||||
@ -95,7 +48,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.ImgDir, func(path string, info fs.FileInfo, err error) error {
|
if err := filepath.Walk(c.PicsDir, 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)
|
||||||
}
|
}
|
||||||
@ -103,10 +56,45 @@ 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
|
||||||
|
|
||||||
imageWasFound, err := checkImageUsage(c, db, imageName)
|
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(), 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 checking image usage: %v", err)
|
return fmt.Errorf("error getting all users: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, user := range users {
|
||||||
|
if imageName == user.ProfilePicLink {
|
||||||
|
imageWasFound = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !imageWasFound {
|
if !imageWasFound {
|
||||||
|
@ -5,7 +5,6 @@ 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"
|
||||||
@ -57,8 +56,8 @@ func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))
|
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.UUID, ".md")
|
||||||
contentBytes, err := os.ReadFile(articlePath)
|
contentBytes, err := os.ReadFile(articleAbsName)
|
||||||
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 !f.SessionIsActive(r, s) {
|
if _, err := f.ManageSession(w, r, c, s); err != nil {
|
||||||
if !tokenIsVerified(w, r, c) {
|
if !tokenIsVerified(w, r, c) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
http.ServeFile(w, r, filepath.Join(c.ImgDir, r.PathValue("pic")))
|
http.ServeFile(w, r, filepath.Join(c.PicsDir, r.PathValue("pic")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ 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"
|
||||||
)
|
)
|
||||||
@ -43,6 +42,6 @@ func ServePDF(c *b.Config) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
http.ServeFile(w, r, filepath.Join(c.PDFDir, r.PathValue("id")))
|
http.ServeFile(w, r, 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
|
||||||
|
|
||||||
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(data.Article.UUID, ".md"))
|
articleAbsName := fmt.Sprint(c.ArticleDir, "/", data.Article.UUID, ".md")
|
||||||
content, err := os.ReadFile(articlePath)
|
content, err := os.ReadFile(articleAbsName)
|
||||||
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(filepath.Join(c.ArticleDir, fmt.Sprint(oldArticle.UUID, ".md"))); err != nil {
|
if err = os.Remove(fmt.Sprint(c.ArticleDir, "/", 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
|
||||||
}
|
}
|
||||||
|
|
||||||
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))
|
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.UUID, ".md")
|
||||||
content, err := os.ReadFile(articlePath)
|
content, err := os.ReadFile(articleAbsName)
|
||||||
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(filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))); err != nil {
|
if err = os.Remove(fmt.Sprint(c.ArticleDir, "/", 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 := filepath.Join(c.ArticleDir, fmt.Sprint(oldArticle.UUID, ".md"))
|
src := fmt.Sprint(c.ArticleDir, "/", oldArticle.UUID, ".md")
|
||||||
dst := filepath.Join(c.ArticleDir, fmt.Sprint(newArticle.UUID, ".md"))
|
dst := fmt.Sprint(c.ArticleDir, "/", 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(filepath.Join(c.ArticleDir, fmt.Sprint(data.Article.UUID, ".md")))
|
content, err := os.ReadFile(fmt.Sprint(c.ArticleDir, "/", 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.ImgDir)
|
filename, err := b.SaveImage(file, c.MaxImgHeight, c.MaxImgWidth, c.PicsDir+"/")
|
||||||
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.ImgDir)
|
filename, err := b.SaveImage(file, c.MaxBannerHeight, c.MaxBannerWidth, c.PicsDir+"/")
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))
|
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.UUID, ".md")
|
||||||
if err = os.WriteFile(articlePath, content, 0644); err != nil {
|
if err = os.WriteFile(articleAbsName, 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,18 +64,6 @@ 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.
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
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_ROOT_PASSWORD: example
|
|
||||||
MYSQL_DATABASE: cpolis
|
|
||||||
MYSQL_USER: exampleuser
|
|
||||||
MYSQL_PASSWORD: examplepass
|
|
||||||
ports:
|
|
||||||
- "3306:3306"
|
|
||||||
volumes:
|
|
||||||
- ./create_db.sql:/docker-entrypoint-initdb.d/create_db.sql
|
|
Reference in New Issue
Block a user