Pictures are now handeled correctly
This commit is contained in:
		| @@ -20,13 +20,13 @@ func HandleCliArgs() (*CliArgs, error) { | |||||||
| 	var err error | 	var err error | ||||||
| 	cliArgs := new(CliArgs) | 	cliArgs := new(CliArgs) | ||||||
|  |  | ||||||
|  | 	flag.StringVar(&cliArgs.DBName, "db", "cpolis", "DB name") | ||||||
| 	keyFile := flag.String("key", "/var/www/cpolis/cpolis.key", "key file") | 	keyFile := flag.String("key", "/var/www/cpolis/cpolis.key", "key file") | ||||||
| 	logFile := flag.String("log", "/var/log/cpolis.log", "log 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") | 	port := flag.Int("port", 8080, "port") | ||||||
| 	rssFile := flag.String("rss", "/var/www/cpolis/cpolis.rss", "RSS file") | 	rssFile := flag.String("rss", "/var/www/cpolis/cpolis.rss", "RSS file") | ||||||
| 	webDir := flag.String("web", "/var/www/cpolis/web", "web directory") | 	webDir := flag.String("web", "/var/www/cpolis/web", "web directory") | ||||||
| 	flag.StringVar(&cliArgs.DBName, "db", "cpolis", "DB name") |  | ||||||
| 	flag.Parse() | 	flag.Parse() | ||||||
|  |  | ||||||
| 	cliArgs.KeyFile, err = filepath.Abs(*keyFile) | 	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) | 		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 { | 	if err != nil { | ||||||
| 		return nil, fmt.Errorf("error finding absolute path for PicsDir: %v", err) | 		return nil, fmt.Errorf("error finding absolute path for PicsDir: %v", err) | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -60,7 +60,10 @@ func main() { | |||||||
| 	mux.HandleFunc("GET /rejected-articles", view.ShowRejectedArticles(args, db, store)) | 	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-rejected-article/{id}", view.ReviewRejectedArticle(args, db, store)) | ||||||
| 	mux.HandleFunc("GET /review-unpublished-article/{id}", view.ReviewUnpublishedArticle(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 /this-issue", view.ShowCurrentArticles(args, db)) | ||||||
| 	mux.HandleFunc("GET /unpublished-articles", view.ShowUnpublishedArticles(args, db)) | 	mux.HandleFunc("GET /unpublished-articles", view.ShowUnpublishedArticles(args, db)) | ||||||
| 	mux.HandleFunc("GET /write-article", view.WriteArticle(args, db)) | 	mux.HandleFunc("GET /write-article", view.WriteArticle(args, db)) | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ import ( | |||||||
| 	"log" | 	"log" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"os" | 	"os" | ||||||
|  | 	"path/filepath" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| @@ -412,8 +413,15 @@ func UploadImage(c *control.CliArgs) http.HandlerFunc { | |||||||
| 		} | 		} | ||||||
| 		defer file.Close() | 		defer file.Close() | ||||||
|  |  | ||||||
| 		filename := fmt.Sprint(c.PicsDir, time.Now().Format("2006-01-02_15:04:05"), "-", header.Filename) | 		filename := fmt.Sprint(c.PicsDir, "/", time.Now().Format("2006-01-02_15:04:05"), "-", header.Filename) | ||||||
| 		img, err := os.Create(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 { | 		if err != nil { | ||||||
| 			log.Println(err) | 			log.Println(err) | ||||||
| 			http.Error(w, err.Error(), http.StatusInternalServerError) | 			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") | 		tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html") | ||||||
| 		template.Must(tmpl, err).ExecuteTemplate(w, "editor-images", fmt.Sprint("")) | 		tmpl = template.Must(tmpl, err) | ||||||
|  | 		tmpl.ExecuteTemplate(w, "editor-images", fmt.Sprint("")) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										22
									
								
								cmd/view/images.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								cmd/view/images.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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")) | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user