Compare commits
50 Commits
095576a234
...
v0.15.0
Author | SHA1 | Date | |
---|---|---|---|
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",
|
||||
"-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
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"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@ -116,31 +115,6 @@ func (db *DB) GetArticle(id int64) (*Article, error) {
|
||||
return article, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetArticleByUUID(u uuid.UUID) (*Article, error) {
|
||||
query := `
|
||||
SELECT id, title, created, banner_link, summary, published, creator_id, issue_id, edited_id, clicks, is_in_issue, auto_generated
|
||||
FROM articles
|
||||
WHERE uuid = ?
|
||||
`
|
||||
row := db.QueryRow(query, u.String())
|
||||
|
||||
article := new(Article)
|
||||
var created []byte
|
||||
var err error
|
||||
|
||||
if err := row.Scan(&article.ID, &article.Title, &created, &article.BannerLink, &article.Summary, &article.Published, &article.CreatorID, &article.IssueID, &article.EditedID, &article.Clicks, &article.IsInIssue, &article.AutoGenerated); err != nil {
|
||||
return nil, fmt.Errorf("error scanning article row: %v", err)
|
||||
}
|
||||
|
||||
article.UUID = u
|
||||
article.Created, err = time.Parse("2006-01-02 15:04:05", string(created))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing created: %v", err)
|
||||
}
|
||||
|
||||
return article, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetCertainArticles(attribute string, value bool) ([]*Article, error) {
|
||||
query := fmt.Sprintf(`
|
||||
SELECT id, title, created, banner_link, summary, creator_id, issue_id, clicks, published, rejected, is_in_issue, auto_generated, uuid
|
||||
@ -335,9 +309,9 @@ func (db *DB) DeleteArticle(id int64) 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)
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@ -13,9 +12,6 @@ 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"
|
||||
@ -38,24 +34,15 @@ 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 {
|
||||
|
@ -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,24 +43,27 @@ func newConfig() *Config {
|
||||
ConfigFile: "/etc/cpolis/config.toml",
|
||||
CookieExpiryHours: 24 * 30,
|
||||
DBName: "cpolis",
|
||||
FirebaseKey: "/etc/cpolis/serviceAccountKey.json",
|
||||
ImgDir: "/var/www/cpolis/images",
|
||||
FirebaseKey: "/var/www/cpolis/serviceAccountKey.json",
|
||||
LogFile: "/var/log/cpolis.log",
|
||||
MaxBannerHeight: 1080,
|
||||
MaxBannerWidth: 1920,
|
||||
MaxImgHeight: 1080,
|
||||
MaxImgWidth: 1920,
|
||||
PDFDir: "/var/www/cpolis/pdfs",
|
||||
Port: ":1664",
|
||||
Version: "v0.16.0",
|
||||
PicsDir: "/var/www/cpolis/pics",
|
||||
Port: ":8080",
|
||||
Version: "v0.15.0",
|
||||
WebDir: "/var/www/cpolis/web",
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
|
||||
stringSlice := strings.Split(path, "/")
|
||||
_, err = os.Stat(path)
|
||||
if os.IsNotExist(err) {
|
||||
dir := filepath.Dir(path)
|
||||
dir := strings.Join(stringSlice[:len(stringSlice)-1], "/")
|
||||
if err = os.MkdirAll(dir, dirPerm); err != nil {
|
||||
return "", fmt.Errorf("error creating %v: %v", dir, err)
|
||||
}
|
||||
|
||||
fileName := filepath.Base(path)
|
||||
file, err := os.Create(filepath.Join(dir, fileName))
|
||||
fileName := stringSlice[len(stringSlice)-1]
|
||||
file, err := os.Create(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)
|
||||
}
|
||||
@ -113,10 +116,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")
|
||||
@ -155,10 +158,6 @@ func (c *Config) setupConfig(cliConfig *Config) error {
|
||||
if cliConfig.AESKeyFile != defaultConfig.AESKeyFile {
|
||||
c.AESKeyFile = cliConfig.AESKeyFile
|
||||
}
|
||||
c.AESKeyFile, err = filepath.Abs(c.AESKeyFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting absolute filepath for AESKeyFile: %v", err)
|
||||
}
|
||||
c.AESKeyFile, err = mkFile(c.AESKeyFile, 0600, 0700)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting up file: %v", err)
|
||||
@ -167,10 +166,6 @@ func (c *Config) setupConfig(cliConfig *Config) error {
|
||||
if cliConfig.ArticleDir != defaultConfig.ArticleDir {
|
||||
c.ArticleDir = cliConfig.ArticleDir
|
||||
}
|
||||
c.ArticleDir, err = filepath.Abs(c.ArticleDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting absolute filepath for ArticleDir: %v", err)
|
||||
}
|
||||
c.ArticleDir, err = mkDir(c.ArticleDir, 0700)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting up directory: %v", err)
|
||||
@ -179,10 +174,6 @@ func (c *Config) setupConfig(cliConfig *Config) error {
|
||||
if cliConfig.AtomFile != defaultConfig.AtomFile {
|
||||
c.AtomFile = cliConfig.AtomFile
|
||||
}
|
||||
c.AtomFile, err = filepath.Abs(c.AtomFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting absolute filepath for AtomFile: %v", err)
|
||||
}
|
||||
c.AtomFile, err = mkFile(c.AtomFile, 0644, 0744)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting up file: %v", err)
|
||||
@ -211,27 +202,11 @@ func (c *Config) setupConfig(cliConfig *Config) error {
|
||||
if cliConfig.FirebaseKey != defaultConfig.FirebaseKey {
|
||||
c.FirebaseKey = cliConfig.FirebaseKey
|
||||
}
|
||||
c.FirebaseKey, err = filepath.Abs(c.FirebaseKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting absolute filepath for FirebaseKey: %v", err)
|
||||
}
|
||||
c.FirebaseKey, err = mkFile(c.FirebaseKey, 0600, 0700)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
@ -239,10 +214,6 @@ func (c *Config) setupConfig(cliConfig *Config) error {
|
||||
if cliConfig.LogFile != defaultConfig.LogFile {
|
||||
c.LogFile = cliConfig.LogFile
|
||||
}
|
||||
c.LogFile, err = filepath.Abs(c.LogFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting absolute filepath for LogFile: %v", err)
|
||||
}
|
||||
c.LogFile, err = mkFile(c.LogFile, 0600, 0700)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting up file: %v", err)
|
||||
@ -267,15 +238,19 @@ func (c *Config) setupConfig(cliConfig *Config) error {
|
||||
if cliConfig.PDFDir != defaultConfig.PDFDir {
|
||||
c.PDFDir = cliConfig.PDFDir
|
||||
}
|
||||
c.PDFDir, err = filepath.Abs(c.PDFDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting absolute filepath for PDFDir: %v", err)
|
||||
}
|
||||
c.PDFDir, err = mkDir(c.PDFDir, 0700)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting up directory: %v", err)
|
||||
}
|
||||
|
||||
if cliConfig.PicsDir != defaultConfig.PicsDir {
|
||||
c.PicsDir = cliConfig.PicsDir
|
||||
}
|
||||
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
|
||||
}
|
||||
@ -287,10 +262,6 @@ func (c *Config) setupConfig(cliConfig *Config) error {
|
||||
if cliConfig.WebDir != defaultConfig.WebDir {
|
||||
c.WebDir = cliConfig.WebDir
|
||||
}
|
||||
c.WebDir, err = filepath.Abs(c.WebDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting absolute filepath for WebDir: %v", err)
|
||||
}
|
||||
c.WebDir, err = mkDir(c.WebDir, 0700)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting up directory: %v", err)
|
||||
|
@ -14,13 +14,15 @@ import (
|
||||
func ConvertToMarkdown(c *Config, filename string) ([]byte, error) {
|
||||
var stderr bytes.Buffer
|
||||
|
||||
tmpDir, err := os.MkdirTemp(os.TempDir(), "cpolis_images")
|
||||
articleID := uuid.New()
|
||||
articleFileName := fmt.Sprint("/tmp/", articleID, ".md")
|
||||
|
||||
tmpDir, err := os.MkdirTemp("/tmp", "cpolis_images")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating temporary directory: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
articleFileName := filepath.Join(os.TempDir(), fmt.Sprint(uuid.New(), ".md"))
|
||||
cmd := exec.Command("pandoc", "-s", "-f", "docx", "-t", "commonmark_x", "-o", articleFileName, "--extract-media", tmpDir, filename) // TODO: Is writing to a file necessary?
|
||||
cmd.Stderr = &stderr
|
||||
if err = cmd.Run(); err != nil {
|
||||
@ -33,7 +35,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,12 +47,12 @@ func ConvertToMarkdown(c *Config, filename string) ([]byte, error) {
|
||||
}
|
||||
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 {
|
||||
return nil, fmt.Errorf("error saving image %v: %v", name, err)
|
||||
}
|
||||
|
||||
articleContent = regexp.MustCompile(name).ReplaceAll(articleContent, []byte(c.Domain+"/image/serve/"+newImageName))
|
||||
articleContent = regexp.MustCompile(name).ReplaceAll(articleContent, []byte(c.PicsDir+"/"+newImageName))
|
||||
}
|
||||
|
||||
return articleContent, nil
|
||||
|
@ -6,9 +6,11 @@ import (
|
||||
"image"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/chai2010/webp"
|
||||
"github.com/disintegration/imaging"
|
||||
@ -17,53 +19,6 @@ 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 {
|
||||
@ -94,32 +49,58 @@ func SaveImage(src io.Reader, maxHeight, maxWidth int, path string) (string, err
|
||||
return filename, nil
|
||||
}
|
||||
|
||||
func CleanUpImages(c *Config, db *DB) error {
|
||||
if err := filepath.Walk(c.ImgDir, func(path string, info fs.FileInfo, err error) error {
|
||||
func CleanUpImages(c *Config) {
|
||||
for {
|
||||
if err := filepath.Walk(c.PicsDir, func(path string, info fs.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("error walking images filepath: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
imageName := info.Name()
|
||||
imagePath := path
|
||||
absImageName := path
|
||||
|
||||
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 checking image usage: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
mdFile, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer mdFile.Close()
|
||||
|
||||
scanner := bufio.NewScanner(mdFile)
|
||||
imageWasFound := false
|
||||
|
||||
for scanner.Scan() {
|
||||
if strings.Contains(scanner.Text(), imageName) {
|
||||
imageWasFound = true
|
||||
}
|
||||
}
|
||||
|
||||
if !imageWasFound {
|
||||
if err = os.Remove(imagePath); err != nil {
|
||||
return fmt.Errorf("error removing unused image: %v", err)
|
||||
if err = os.Remove(absImageName); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return scanner.Err()
|
||||
}
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return fmt.Errorf("error cleaning up: %v", err)
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
time.Sleep(time.Hour)
|
||||
}
|
||||
}
|
||||
|
@ -5,9 +5,8 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
b "streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
|
||||
@ -38,15 +37,15 @@ func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
uuidString := r.PathValue("uuid")
|
||||
uuid, err := uuid.Parse(uuidString)
|
||||
idString := r.PathValue("id")
|
||||
id, err := strconv.ParseInt(idString, 10, 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
article, err := db.GetArticleByUUID(uuid)
|
||||
article, err := db.GetArticle(id)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -57,8 +56,8 @@ func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))
|
||||
contentBytes, err := os.ReadFile(articlePath)
|
||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.ID, ".md")
|
||||
contentBytes, err := os.ReadFile(articleAbsName)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -88,14 +87,15 @@ func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc {
|
||||
|
||||
func ServeClicks(db *b.DB) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
uuid, err := uuid.Parse(r.PathValue("uuid"))
|
||||
idString := r.PathValue("id")
|
||||
id, err := strconv.ParseInt(idString, 10, 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
article, err := db.GetArticleByUUID(uuid)
|
||||
article, err := db.GetArticle(id)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
@ -1,7 +1,9 @@
|
||||
package calls
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
b "streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
@ -12,6 +14,13 @@ func ServeAtomFeed(c *b.Config) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
http.ServeFile(w, r, c.AtomFile)
|
||||
absFilepath, err := filepath.Abs(c.AtomFile)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.ServeFile(w, r, absFilepath)
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package calls
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
@ -10,12 +11,19 @@ import (
|
||||
|
||||
func ServeImage(c *b.Config, s map[string]*f.Session) http.HandlerFunc {
|
||||
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) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
http.ServeFile(w, r, filepath.Join(c.ImgDir, r.PathValue("pic")))
|
||||
absFilepath, err := filepath.Abs(c.PicsDir)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.ServeFile(w, r, absFilepath+"/"+r.PathValue("pic"))
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
b "streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
@ -43,6 +42,6 @@ func ServePDF(c *b.Config) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
http.ServeFile(w, r, filepath.Join(c.PDFDir, r.PathValue("id")))
|
||||
http.ServeFile(w, r, c.PDFDir+"/"+r.PathValue("id"))
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -84,7 +83,7 @@ func WriteArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "editor.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -206,7 +205,7 @@ func SubmitArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFun
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
@ -341,7 +340,7 @@ func ResubmitArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerF
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
@ -385,7 +384,7 @@ func ShowUnpublishedUnrejectedAndPublishedRejectedArticles(c *b.Config, db *b.DB
|
||||
}
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "unpublished-articles.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/unpublished-articles.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", articles); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -421,7 +420,7 @@ func ShowRejectedArticles(c *b.Config, db *b.DB, s map[string]*Session) http.Han
|
||||
}
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "rejected-articles.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/rejected-articles.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
@ -456,8 +455,8 @@ func ReviewRejectedArticle(c *b.Config, db *b.DB, s map[string]*Session) http.Ha
|
||||
|
||||
data.Image = data.Article.BannerLink
|
||||
|
||||
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(data.Article.UUID, ".md"))
|
||||
content, err := os.ReadFile(articlePath)
|
||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", data.Article.UUID, ".md")
|
||||
content, err := os.ReadFile(articleAbsName)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -525,7 +524,7 @@ func ReviewRejectedArticle(c *b.Config, db *b.DB, s map[string]*Session) http.Ha
|
||||
|
||||
data.Action = fmt.Sprint("resubmit/", data.Article.ID)
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "editor.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
@ -587,7 +586,7 @@ func PublishArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFu
|
||||
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)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@ -615,7 +614,7 @@ func PublishArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFu
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
@ -649,7 +648,7 @@ func RejectArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFun
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
@ -673,7 +672,7 @@ func ShowCurrentIssue(c *b.Config, db *b.DB, s map[string]*Session) http.Handler
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "current-issue.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/current-issue.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", articles); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -708,7 +707,7 @@ func ShowPublishedArticles(c *b.Config, db *b.DB, s map[string]*Session, action
|
||||
}
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "published-articles.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/published-articles.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
@ -765,8 +764,8 @@ func ReviewArticle(c *b.Config, db *b.DB, s map[string]*Session, action, title,
|
||||
return
|
||||
}
|
||||
|
||||
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))
|
||||
content, err := os.ReadFile(articlePath)
|
||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.UUID, ".md")
|
||||
content, err := os.ReadFile(articleAbsName)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -803,7 +802,7 @@ func ReviewArticle(c *b.Config, db *b.DB, s map[string]*Session, action, title,
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "review-article.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/review-article.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -840,19 +839,12 @@ func DeleteArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFun
|
||||
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)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
go func(c *b.Config, db *b.DB) {
|
||||
if err = b.CleanUpImages(c, db); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}(c, db)
|
||||
|
||||
feed, err := b.GenerateAtomFeed(c, db)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@ -868,7 +860,7 @@ func DeleteArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFun
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
@ -918,8 +910,8 @@ func AllowEditArticle(c *b.Config, db *b.DB, s map[string]*Session) http.Handler
|
||||
return
|
||||
}
|
||||
|
||||
src := filepath.Join(c.ArticleDir, fmt.Sprint(oldArticle.UUID, ".md"))
|
||||
dst := filepath.Join(c.ArticleDir, fmt.Sprint(newArticle.UUID, ".md"))
|
||||
src := fmt.Sprint(c.ArticleDir, "/", oldArticle.UUID, ".md")
|
||||
dst := fmt.Sprint(c.ArticleDir, "/", newArticle.UUID, ".md")
|
||||
if err = b.CopyFile(src, dst); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -961,7 +953,7 @@ func AllowEditArticle(c *b.Config, db *b.DB, s map[string]*Session) http.Handler
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl := template.Must(template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")))
|
||||
tmpl := template.Must(template.ParseFiles(c.WebDir + "/templates/hub.html"))
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -995,7 +987,7 @@ func EditArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc
|
||||
|
||||
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 {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -1023,7 +1015,7 @@ func EditArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc
|
||||
|
||||
data.Action = fmt.Sprint("save/", data.Article.ID)
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "editor.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
@ -44,15 +44,22 @@ func UploadDocx(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
docxFilepath := filepath.Join(os.TempDir(), fmt.Sprint(uuid.New(), ".docx"))
|
||||
if err = os.WriteFile(docxFilepath, buf.Bytes(), 0644); err != nil {
|
||||
docxFilename := fmt.Sprint(uuid.New(), ".docx")
|
||||
absDocxFilepath, err := filepath.Abs("/tmp/" + docxFilename)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer os.Remove(docxFilepath)
|
||||
|
||||
mdString, err := b.ConvertToMarkdown(c, docxFilepath)
|
||||
if err = os.WriteFile(absDocxFilepath, buf.Bytes(), 0644); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer os.Remove(absDocxFilepath)
|
||||
|
||||
mdString, err := b.ConvertToMarkdown(c, absDocxFilepath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -60,8 +67,15 @@ func UploadDocx(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
}
|
||||
|
||||
uuidName := uuid.New()
|
||||
mdFilepath := filepath.Join(c.ArticleDir, fmt.Sprint(uuidName, ".md"))
|
||||
if err = os.WriteFile(mdFilepath, mdString, 0644); err != nil {
|
||||
mdFilename := fmt.Sprint(uuidName, ".md")
|
||||
absMdFilepath, err := filepath.Abs(c.ArticleDir + "/" + mdFilename)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err = os.WriteFile(absMdFilepath, mdString, 0644); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
b "streifling.com/jason/cpolis/cmd/backend"
|
||||
@ -25,14 +24,14 @@ func HomePage(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
data.Version = c.Version
|
||||
|
||||
files := make([]string, 2)
|
||||
files[0] = filepath.Join(c.WebDir, "templates", "index.html")
|
||||
files[0] = c.WebDir + "/templates/index.html"
|
||||
if numRows == 0 {
|
||||
data.Role = b.NonExistent
|
||||
data.Title = "Erster Benutzer (Administrator)"
|
||||
data.ButtonText = "Anlegen"
|
||||
data.URL = "/user/add-first"
|
||||
|
||||
files[1] = filepath.Join(c.WebDir, "templates", "edit-user.html")
|
||||
files[1] = c.WebDir + "/templates/edit-user.html"
|
||||
tmpl, err := template.ParseFiles(files...)
|
||||
if err = template.Must(tmpl, err).Execute(w, data); err != nil {
|
||||
log.Println(err)
|
||||
@ -42,7 +41,7 @@ func HomePage(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
} else {
|
||||
cookie, err := r.Cookie("cpolis_session")
|
||||
if err != nil {
|
||||
files[1] = filepath.Join(c.WebDir, "templates", "login.html")
|
||||
files[1] = c.WebDir + "/templates/login.html"
|
||||
tmpl, err := template.ParseFiles(files...)
|
||||
if err = template.Must(tmpl, err).Execute(w, data); err != nil {
|
||||
log.Println(err)
|
||||
@ -57,7 +56,7 @@ func HomePage(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
cookie.Expires = time.Now()
|
||||
http.SetCookie(w, cookie)
|
||||
|
||||
files[1] = filepath.Join(c.WebDir, "templates", "login.html")
|
||||
files[1] = c.WebDir + "/templates/login.html"
|
||||
tmpl, err := template.ParseFiles(files...)
|
||||
if err = template.Must(tmpl, err).Execute(w, data); err != nil {
|
||||
log.Println(err)
|
||||
@ -68,7 +67,7 @@ func HomePage(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
}
|
||||
|
||||
data.Role = session.User.Role
|
||||
files[1] = filepath.Join(c.WebDir, "templates", "hub.html")
|
||||
files[1] = c.WebDir + "/templates/hub.html"
|
||||
tmpl, err := template.ParseFiles(files...)
|
||||
if err = template.Must(tmpl, err).Execute(w, data); err != nil {
|
||||
log.Println(err)
|
||||
@ -90,7 +89,7 @@ func ShowHub(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
b "streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
@ -25,7 +24,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.ImgDir)
|
||||
filename, err := b.SaveImage(file, c.MaxImgHeight, c.MaxImgWidth, c.PicsDir+"/")
|
||||
if err != nil {
|
||||
if err == b.ErrUnsupportedFormat {
|
||||
http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest)
|
||||
@ -57,7 +56,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.ImgDir)
|
||||
filename, err := b.SaveImage(file, c.MaxBannerHeight, c.MaxBannerWidth, c.PicsDir+"/")
|
||||
if err != nil {
|
||||
if err == b.ErrUnsupportedFormat {
|
||||
http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest)
|
||||
@ -71,7 +70,7 @@ func UploadImage(c *b.Config, s map[string]*Session, fileKey, htmlFile, htmlTemp
|
||||
data := new(struct{ Image string })
|
||||
data.Image = filename
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", htmlFile))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/" + htmlFile)
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, htmlTemplate, data); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@ -58,8 +57,8 @@ func PublishLatestIssue(c *b.Config, db *b.DB, s map[string]*Session) http.Handl
|
||||
return
|
||||
}
|
||||
|
||||
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(article.UUID, ".md"))
|
||||
if err = os.WriteFile(articlePath, content, 0644); err != nil {
|
||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.UUID, ".md")
|
||||
if err = os.WriteFile(articleAbsName, content, 0644); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@ -92,7 +91,7 @@ func PublishLatestIssue(c *b.Config, db *b.DB, s map[string]*Session) http.Handl
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
b "streifling.com/jason/cpolis/cmd/backend"
|
||||
@ -18,7 +17,7 @@ func UploadPDF(c *b.Config, s map[string]*Session) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("pdf-upload")
|
||||
file, _, err := r.FormFile("pdf-upload")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -44,11 +43,15 @@ func UploadPDF(c *b.Config, s map[string]*Session) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
oldFilename := strings.Join(strings.Split(header.Filename, ".")[:len(header.Filename)-1], ".")
|
||||
filename := fmt.Sprint(oldFilename, ".", uuid.New(), ".pdf")
|
||||
filepath := filepath.Join(c.PDFDir, filename)
|
||||
filename := fmt.Sprint(uuid.New(), ".pdf")
|
||||
absFilepath, err := filepath.Abs(c.PDFDir + "/" + filename)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err = b.WriteFile(filepath, file); err != nil {
|
||||
if err = b.WriteFile(absFilepath, file); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@ -64,23 +63,11 @@ 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.
|
||||
func ManageSession(w http.ResponseWriter, r *http.Request, c *b.Config, s map[string]*Session) (*Session, error) {
|
||||
tmpl, tmplErr := template.ParseFiles(filepath.Join(c.WebDir, "templates", "index.html"), filepath.Join(c.WebDir, "templates", "login.html"))
|
||||
tmpl, tmplErr := template.ParseFiles(c.WebDir+"/templates/index.html", c.WebDir+"/templates/login.html")
|
||||
|
||||
cookie, err := r.Cookie("cpolis_session")
|
||||
if err != nil {
|
||||
@ -137,7 +124,7 @@ func Login(c *b.Config, db *b.DB, s map[string]*Session, sessionExpiryChan chan
|
||||
s[session.cookie.Value] = session
|
||||
http.SetCookie(w, session.cookie)
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -148,7 +135,7 @@ func Login(c *b.Config, db *b.DB, s map[string]*Session, sessionExpiryChan chan
|
||||
|
||||
func Logout(c *b.Config, s map[string]*Session) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
tmpl, tmplErr := template.ParseFiles(filepath.Join(c.WebDir, "templates", "login.html"))
|
||||
tmpl, tmplErr := template.ParseFiles(c.WebDir + "/templates/login.html")
|
||||
|
||||
cookie, err := r.Cookie("cpolis_session")
|
||||
if err != nil {
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
b "streifling.com/jason/cpolis/cmd/backend"
|
||||
)
|
||||
@ -16,7 +15,7 @@ func CreateTag(c *b.Config, s map[string]*Session) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "add-tag.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-tag.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -43,7 +42,7 @@ func AddTag(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
@ -58,7 +57,7 @@ func CreateUser(c *b.Config, s map[string]*Session) http.HandlerFunc {
|
||||
URL: "/user/add",
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "edit-user.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -135,7 +134,7 @@ func AddUser(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
@ -168,7 +167,7 @@ func EditSelf(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
Image: user.ProfilePicLink,
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "edit-user.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -243,7 +242,7 @@ func UpdateSelf(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
@ -313,7 +312,7 @@ func AddFirstUser(c *b.Config, db *b.DB, s map[string]*Session, sessionExpiryCha
|
||||
data := new(struct{ Role int })
|
||||
data.Role = user.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -344,7 +343,7 @@ func ShowAllUsers(c *b.Config, db *b.DB, s map[string]*Session, action string) h
|
||||
}
|
||||
delete(data.Users, session.User.ID)
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "show-all-users.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/show-all-users.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -382,7 +381,7 @@ func EditUser(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
Image: user.ProfilePicLink,
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "edit-user.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -467,7 +466,7 @@ func UpdateUser(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl := template.Must(template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")))
|
||||
tmpl := template.Must(template.ParseFiles(c.WebDir + "/templates/hub.html"))
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@ -500,7 +499,7 @@ func DeleteUser(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
log.Println(err)
|
||||
|
14
cmd/main.go
14
cmd/main.go
@ -4,7 +4,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
b "streifling.com/jason/cpolis/cmd/backend"
|
||||
c "streifling.com/jason/cpolis/cmd/calls"
|
||||
@ -33,14 +32,7 @@ func main() {
|
||||
sessions, sessionExpiryChan := f.StartSessions()
|
||||
defer close(sessionExpiryChan)
|
||||
|
||||
go func(c *b.Config, db *b.DB) {
|
||||
for {
|
||||
if err = b.CleanUpImages(c, db); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
time.Sleep(time.Hour * 24)
|
||||
}
|
||||
}(config, db)
|
||||
// go b.CleanUpImages(config)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/web/static/", http.StripPrefix("/web/static/",
|
||||
@ -60,8 +52,8 @@ func main() {
|
||||
mux.HandleFunc("GET /article/review-edit/{id}", f.ReviewArticle(config, db, sessions, "allow-edit", "Artikel bearbeiten", "Bearbeiten erlauben"))
|
||||
mux.HandleFunc("GET /article/review-rejected/{id}", f.ReviewRejectedArticle(config, db, sessions))
|
||||
mux.HandleFunc("GET /article/review-unpublished/{id}", f.ReviewArticle(config, db, sessions, "publish", "Artikel veröffentlichen", "Veröffentlichen"))
|
||||
mux.HandleFunc("GET /article/serve/{uuid}", c.ServeArticle(config, db))
|
||||
mux.HandleFunc("GET /article/serve/{uuid}/clicks", c.ServeClicks(db))
|
||||
mux.HandleFunc("GET /article/serve/{id}", c.ServeArticle(config, db))
|
||||
mux.HandleFunc("GET /article/serve/{id}/clicks", c.ServeClicks(db))
|
||||
mux.HandleFunc("GET /article/write", f.WriteArticle(config, db, sessions))
|
||||
mux.HandleFunc("GET /atom/serve", c.ServeAtomFeed(config))
|
||||
mux.HandleFunc("GET /hub", f.ShowHub(config, db, sessions))
|
||||
|
@ -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
|
@ -38,11 +38,11 @@
|
||||
</main>
|
||||
|
||||
<footer class="text-center text-gray-500 my-8">
|
||||
<p>© 2025 Jason Streifling. Alle Rechte vorbehalten.</p>
|
||||
<p>© 2024 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@latest"></script>
|
||||
<script src="https://unpkg.com/htmx.org@2.0.3"></script>
|
||||
<script src="https://unpkg.com/easymde/dist/easymde.min.js"></script>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
|
Reference in New Issue
Block a user