diff --git a/cmd/control/cli.go b/cmd/control/cli.go index 25c8299..1715d66 100644 --- a/cmd/control/cli.go +++ b/cmd/control/cli.go @@ -20,13 +20,13 @@ func HandleCliArgs() (*CliArgs, error) { var err error cliArgs := new(CliArgs) + flag.StringVar(&cliArgs.DBName, "db", "cpolis", "DB name") keyFile := flag.String("key", "/var/www/cpolis/cpolis.key", "key file") logFile := flag.String("log", "/var/log/cpolis.log", "log file") - picsDir := flag.String("pics", "/var/www/cpolis/pics", "pictures directory") + flag.StringVar(&cliArgs.PicsDir, "pics", "pics", "pictures directory") port := flag.Int("port", 8080, "port") rssFile := flag.String("rss", "/var/www/cpolis/cpolis.rss", "RSS file") webDir := flag.String("web", "/var/www/cpolis/web", "web directory") - flag.StringVar(&cliArgs.DBName, "db", "cpolis", "DB name") flag.Parse() cliArgs.KeyFile, err = filepath.Abs(*keyFile) @@ -39,7 +39,7 @@ func HandleCliArgs() (*CliArgs, error) { return nil, fmt.Errorf("error finding absolute path for LogFile: %v", err) } - cliArgs.PicsDir, err = filepath.Abs(*picsDir) + _, err = filepath.Abs(cliArgs.PicsDir) if err != nil { return nil, fmt.Errorf("error finding absolute path for PicsDir: %v", err) } diff --git a/cmd/main.go b/cmd/main.go index dbca8bf..06bc7ae 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -60,7 +60,10 @@ func main() { mux.HandleFunc("GET /rejected-articles", view.ShowRejectedArticles(args, db, store)) mux.HandleFunc("GET /review-rejected-article/{id}", view.ReviewRejectedArticle(args, db, store)) mux.HandleFunc("GET /review-unpublished-article/{id}", view.ReviewUnpublishedArticle(args, db, store)) - mux.HandleFunc("GET /rss", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, args.RSSFile) }) + mux.HandleFunc("GET /rss", func(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, args.RSSFile) + }) + mux.HandleFunc("GET /pics/{pic}", view.ServeImage(args, store)) mux.HandleFunc("GET /this-issue", view.ShowCurrentArticles(args, db)) mux.HandleFunc("GET /unpublished-articles", view.ShowUnpublishedArticles(args, db)) mux.HandleFunc("GET /write-article", view.WriteArticle(args, db)) diff --git a/cmd/view/articles.go b/cmd/view/articles.go index 20b7b85..5bd0ac2 100644 --- a/cmd/view/articles.go +++ b/cmd/view/articles.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "os" + "path/filepath" "strconv" "time" @@ -412,8 +413,15 @@ func UploadImage(c *control.CliArgs) http.HandlerFunc { } defer file.Close() - filename := fmt.Sprint(c.PicsDir, time.Now().Format("2006-01-02_15:04:05"), "-", header.Filename) - img, err := os.Create(filename) + filename := fmt.Sprint(c.PicsDir, "/", time.Now().Format("2006-01-02_15:04:05"), "-", header.Filename) + absFilepath, err := filepath.Abs(filename) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + img, err := os.Create(absFilepath) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -428,6 +436,7 @@ func UploadImage(c *control.CliArgs) http.HandlerFunc { } tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html") - template.Must(tmpl, err).ExecuteTemplate(w, "editor-images", fmt.Sprint("![", header.Filename, "](", filename, ")")) + tmpl = template.Must(tmpl, err) + tmpl.ExecuteTemplate(w, "editor-images", fmt.Sprint("![", header.Filename, "](", filename, ")")) } } diff --git a/cmd/view/images.go b/cmd/view/images.go new file mode 100644 index 0000000..70cbc21 --- /dev/null +++ b/cmd/view/images.go @@ -0,0 +1,22 @@ +package view + +import ( + "log" + "net/http" + "path/filepath" + + "streifling.com/jason/cpolis/cmd/control" +) + +func ServeImage(c *control.CliArgs, s *control.CookieStore) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + 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")) + } +}