Compare commits

...

4 Commits

12 changed files with 70 additions and 61 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path/filepath"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
@ -334,9 +335,9 @@ func (db *DB) DeleteArticle(id int64) error {
} }
func WriteArticleToFile(c *Config, articleUUID uuid.UUID, content []byte) error { func WriteArticleToFile(c *Config, articleUUID uuid.UUID, content []byte) error {
articleAbsName := fmt.Sprint(c.ArticleDir, "/", articleUUID, ".md") articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(articleUUID, ".md"))
if err := os.WriteFile(articleAbsName, content, 0644); err != nil { if err := os.WriteFile(articlePath, content, 0644); err != nil {
return fmt.Errorf("error writing article %v to file: %v", articleUUID, err) return fmt.Errorf("error writing article %v to file: %v", articleUUID, err)
} }

View File

@ -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,14 +43,14 @@ func newConfig() *Config {
ConfigFile: "/etc/cpolis/config.toml", ConfigFile: "/etc/cpolis/config.toml",
CookieExpiryHours: 24 * 30, CookieExpiryHours: 24 * 30,
DBName: "cpolis", DBName: "cpolis",
FirebaseKey: "/var/www/cpolis/serviceAccountKey.json", FirebaseKey: "/etc/cpolis/serviceAccountKey.json",
ImgDir: "/var/www/cpolis/images",
LogFile: "/var/log/cpolis.log", LogFile: "/var/log/cpolis.log",
MaxBannerHeight: 1080, MaxBannerHeight: 1080,
MaxBannerWidth: 1920, MaxBannerWidth: 1920,
MaxImgHeight: 1080, MaxImgHeight: 1080,
MaxImgWidth: 1920, MaxImgWidth: 1920,
PDFDir: "/var/www/cpolis/pdfs", PDFDir: "/var/www/cpolis/pdfs",
PicsDir: "/var/www/cpolis/pics",
Port: ":1664", Port: ":1664",
Version: "v0.16.0", Version: "v0.16.0",
WebDir: "/var/www/cpolis/web", WebDir: "/var/www/cpolis/web",
@ -58,12 +58,9 @@ func newConfig() *Config {
} }
func mkDir(path string, perm fs.FileMode) (string, error) { func mkDir(path string, perm fs.FileMode) (string, error) {
var err error name := filepath.Base(path)
stringSlice := strings.Split(path, "/") path, err := filepath.Abs(path)
name := stringSlice[len(stringSlice)-1]
path, err = filepath.Abs(path)
if err != nil { if err != nil {
return "", fmt.Errorf("error finding absolute path for %v directory: %v", name, err) return "", fmt.Errorf("error finding absolute path for %v directory: %v", name, err)
} }
@ -82,20 +79,20 @@ func mkFile(path string, filePerm, dirPerm fs.FileMode) (string, error) {
return "", fmt.Errorf("error finding absolute path for %v: %v", path, err) return "", fmt.Errorf("error finding absolute path for %v: %v", path, err)
} }
stringSlice := strings.Split(path, "/")
_, err = os.Stat(path) _, err = os.Stat(path)
if os.IsNotExist(err) { if os.IsNotExist(err) {
dir := strings.Join(stringSlice[:len(stringSlice)-1], "/") dir := filepath.Dir(path)
if err = os.MkdirAll(dir, dirPerm); err != nil { if err = os.MkdirAll(dir, dirPerm); err != nil {
return "", fmt.Errorf("error creating %v: %v", dir, err) return "", fmt.Errorf("error creating %v: %v", dir, err)
} }
fileName := stringSlice[len(stringSlice)-1] fileName := filepath.Base(path)
file, err := os.Create(filepath.Join(dir, fileName)) file, err := os.Create(filepath.Join(dir, fileName))
if err != nil { if err != nil {
return "", fmt.Errorf("error creating %v: %v", fileName, err) return "", fmt.Errorf("error creating %v: %v", fileName, err)
} }
defer file.Close() defer file.Close()
if err = file.Chmod(filePerm); err != nil { if err = file.Chmod(filePerm); err != nil {
return "", fmt.Errorf("error setting permissions for %v: %v", fileName, err) return "", fmt.Errorf("error setting permissions for %v: %v", fileName, err)
} }
@ -116,10 +113,10 @@ func (c *Config) handleCliArgs() error {
flag.StringVar(&c.Description, "desc", c.Description, "channel description") flag.StringVar(&c.Description, "desc", c.Description, "channel description")
flag.StringVar(&c.Domain, "domain", c.Domain, "domain name") flag.StringVar(&c.Domain, "domain", c.Domain, "domain name")
flag.StringVar(&c.FirebaseKey, "firebase", c.FirebaseKey, "Firebase service account key file") flag.StringVar(&c.FirebaseKey, "firebase", c.FirebaseKey, "Firebase service account key file")
flag.StringVar(&c.ImgDir, "pics", c.ImgDir, "pictures directory")
flag.StringVar(&c.Link, "link", c.Link, "channel Link") flag.StringVar(&c.Link, "link", c.Link, "channel Link")
flag.StringVar(&c.LogFile, "log", c.LogFile, "log file") flag.StringVar(&c.LogFile, "log", c.LogFile, "log file")
flag.StringVar(&c.PDFDir, "pdfs", c.PDFDir, "pdf directory") flag.StringVar(&c.PDFDir, "pdfs", c.PDFDir, "pdf directory")
flag.StringVar(&c.PicsDir, "pics", c.PicsDir, "pictures directory")
flag.StringVar(&c.Title, "title", c.Title, "channel title") flag.StringVar(&c.Title, "title", c.Title, "channel title")
flag.StringVar(&c.WebDir, "web", c.WebDir, "web directory") flag.StringVar(&c.WebDir, "web", c.WebDir, "web directory")
flag.IntVar(&c.CookieExpiryHours, "cookie-expiry-hours", c.CookieExpiryHours, "cookies expire after this amount of hours") flag.IntVar(&c.CookieExpiryHours, "cookie-expiry-hours", c.CookieExpiryHours, "cookies expire after this amount of hours")
@ -223,6 +220,18 @@ func (c *Config) setupConfig(cliConfig *Config) error {
return fmt.Errorf("error setting up file: %v", err) return fmt.Errorf("error setting up file: %v", err)
} }
if cliConfig.ImgDir != defaultConfig.ImgDir {
c.ImgDir = cliConfig.ImgDir
}
c.ImgDir, err = filepath.Abs(c.ImgDir)
if err != nil {
return fmt.Errorf("error setting absolute filepath for PicsDir: %v", err)
}
c.ImgDir, err = mkDir(c.ImgDir, 0700)
if err != nil {
return fmt.Errorf("error setting up directory: %v", err)
}
if cliConfig.Link != defaultConfig.Link { if cliConfig.Link != defaultConfig.Link {
c.Link = cliConfig.Link c.Link = cliConfig.Link
} }
@ -267,18 +276,6 @@ func (c *Config) setupConfig(cliConfig *Config) error {
return fmt.Errorf("error setting up directory: %v", err) return fmt.Errorf("error setting up directory: %v", err)
} }
if cliConfig.PicsDir != defaultConfig.PicsDir {
c.PicsDir = cliConfig.PicsDir
}
c.PicsDir, err = filepath.Abs(c.PicsDir)
if err != nil {
return fmt.Errorf("error setting absolute filepath for PicsDir: %v", err)
}
c.PicsDir, err = mkDir(c.PicsDir, 0700)
if err != nil {
return fmt.Errorf("error setting up directory: %v", err)
}
if cliConfig.Port != defaultConfig.Port { if cliConfig.Port != defaultConfig.Port {
c.Port = cliConfig.Port c.Port = cliConfig.Port
} }

View File

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

View File

@ -48,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.PicsDir, func(path string, info fs.FileInfo, err error) error { if err := filepath.Walk(c.ImgDir, func(path string, info fs.FileInfo, err error) error {
if err != nil { if err != nil {
return fmt.Errorf("error walking images filepath: %v", err) return fmt.Errorf("error walking images filepath: %v", err)
} }

View File

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

View File

@ -10,12 +10,12 @@ import (
func ServeImage(c *b.Config, s map[string]*f.Session) http.HandlerFunc { func ServeImage(c *b.Config, s map[string]*f.Session) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if _, err := f.ManageSession(w, r, c, s); err != nil { if _, err := f.ValidateSession(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.PicsDir, r.PathValue("pic"))) http.ServeFile(w, r, filepath.Join(c.ImgDir, r.PathValue("pic")))
} }
} }

View File

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

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

View File

@ -25,7 +25,7 @@ func UploadEasyMDEImage(c *b.Config, s map[string]*Session) http.HandlerFunc {
} }
defer file.Close() defer file.Close()
filename, err := b.SaveImage(file, c.MaxImgHeight, c.MaxImgWidth, c.PicsDir+"/") filename, err := b.SaveImage(file, c.MaxImgHeight, c.MaxImgWidth, c.ImgDir)
if err != nil { if err != nil {
if err == b.ErrUnsupportedFormat { if err == b.ErrUnsupportedFormat {
http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest) http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest)
@ -57,7 +57,7 @@ func UploadImage(c *b.Config, s map[string]*Session, fileKey, htmlFile, htmlTemp
} }
defer file.Close() defer file.Close()
filename, err := b.SaveImage(file, c.MaxBannerHeight, c.MaxBannerWidth, c.PicsDir+"/") filename, err := b.SaveImage(file, c.MaxBannerHeight, c.MaxBannerWidth, c.ImgDir)
if err != nil { if err != nil {
if err == b.ErrUnsupportedFormat { if err == b.ErrUnsupportedFormat {
http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest) http.Error(w, "Das Dateiformat wird nicht unterstützt.", http.StatusBadRequest)

View File

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

View File

@ -64,31 +64,39 @@ func StartSessions() (map[string]*Session, chan string) {
return sessions, sessionExpiryChan return sessions, sessionExpiryChan
} }
// ManageSession is used for verifying that the user is logged in and returns // ValidateSession 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.
// logged in. func ValidateSession(w http.ResponseWriter, r *http.Request, c *b.Config, s map[string]*Session) (*Session, error) {
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"))
cookie, err := r.Cookie("cpolis_session") cookie, err := r.Cookie("cpolis_session")
if err != nil { if err != nil {
if err = template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", nil); err != nil {
return nil, fmt.Errorf("error executing template: %v", err)
}
return nil, errors.New("no cookie set") return nil, errors.New("no cookie set")
} }
session, ok := s[cookie.Value] session, ok := s[cookie.Value]
if !ok { if !ok {
cookie.Expires = time.Now() return nil, errors.New("session does not exist")
http.SetCookie(w, cookie) }
return session, nil
}
// 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) {
session, err := ValidateSession(w, r, c, s)
if err != nil {
if session.cookie != nil {
session.cookie.Expires = time.Now()
http.SetCookie(w, session.cookie)
}
tmpl, tmplErr := template.ParseFiles(filepath.Join(c.WebDir, "templates", "index.html"), filepath.Join(c.WebDir, "templates", "login.html"))
if err = template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", nil); err != nil { if err = template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", nil); err != nil {
return nil, fmt.Errorf("error executing template: %v", err) return nil, fmt.Errorf("error executing template: %v", err)
} }
return nil, errors.New("session does not exist") return nil, err
} }
session.cookie.Expires = time.Now().Add(time.Hour * time.Duration(c.CookieExpiryHours)) session.cookie.Expires = time.Now().Add(time.Hour * time.Duration(c.CookieExpiryHours))

View File

@ -4,7 +4,8 @@ services:
ports: ports:
- "1664:1664" - "1664:1664"
volumes: volumes:
- /path/to/serviceAccountKey.json:/var/www/cpolis/serviceAccountKey.json - /etc/cpolis/config.toml:/etc/cpolis/config.toml
- /etc/cpolis/serviceAccountKey.json:/etc/cpolis/serviceAccountKey.json
- /var/log/cpolis.log:/var/log/cpolis.log - /var/log/cpolis.log:/var/log/cpolis.log
depends_on: depends_on:
- db - db
@ -19,4 +20,4 @@ services:
ports: ports:
- "3306:3306" - "3306:3306"
volumes: volumes:
- ./path/to/create_db.sql:/docker-entrypoint-initdb.d/create_db.sql - ./create_db.sql:/docker-entrypoint-initdb.d/create_db.sql