From 7d4326e251038226685f8f83cb218a573393ee56 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sun, 25 Jun 2023 23:03:41 -0700 Subject: [PATCH] Fix GIF and PNG uploads --- pkg/middleware/csrf.go | 14 +------------- pkg/photo/upload.go | 13 +++++++++---- web/templates/photo/delete.html | 9 ++++++++- web/templates/photo/permalink.html | 15 ++++++++++----- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/pkg/middleware/csrf.go b/pkg/middleware/csrf.go index 5849a5d..398e91d 100644 --- a/pkg/middleware/csrf.go +++ b/pkg/middleware/csrf.go @@ -26,19 +26,7 @@ func CSRF(handler http.Handler) http.Handler { // If we are running a POST request, validate the CSRF form value. if r.Method != http.MethodGet { - r.Body = http.MaxBytesReader(w, r.Body, config.MaxBodySize) - err := r.ParseMultipartForm(config.MultipartMaxMemory) - if err != nil { - log.Error("ParseMultipartForm: %s", err) - templates.MakeErrorPage( - "Request Size Too Large", - "You have uploaded too big of a file! Go back and try something else.", - http.StatusNotAcceptable, - )(w, r.WithContext(ctx)) - return - } - - // Check for the CSRF token + r.ParseMultipartForm(config.MultipartMaxMemory) check := r.FormValue(config.CSRFInputName) if check != token { log.Error("CSRF mismatch! %s <> %s", check, token) diff --git a/pkg/photo/upload.go b/pkg/photo/upload.go index e2f4d7d..0e1d1a5 100644 --- a/pkg/photo/upload.go +++ b/pkg/photo/upload.go @@ -35,7 +35,10 @@ type UploadConfig struct { // - error on errors func UploadPhoto(cfg UploadConfig) (string, string, error) { // Validate and normalize the extension. - var extension = strings.ToLower(cfg.Extension) + var ( + extension = strings.ToLower(cfg.Extension) + dbExtension = extension + ) switch extension { case ".jpg": fallthrough @@ -45,16 +48,18 @@ func UploadPhoto(cfg UploadConfig) (string, string, error) { extension = ".jpg" case ".png": extension = ".png" + dbExtension = ".jpg" case ".gif": - extension = ".mp4" + extension = ".gif" + dbExtension = ".mp4" default: return "", "", errors.New("unsupported image extension, must be jpg or png") } // Decide on a filename for this photo. var ( - filename = NewFilename(extension) - cropFilename = NewFilename(extension) + filename = NewFilename(dbExtension) + cropFilename = NewFilename(dbExtension) ) // Decode the image using exiffix, which will auto-rotate jpeg images diff --git a/web/templates/photo/delete.html b/web/templates/photo/delete.html index 3debb25..e778dc2 100644 --- a/web/templates/photo/delete.html +++ b/web/templates/photo/delete.html @@ -28,7 +28,14 @@
+ + {{if HasSuffix .Photo.Filename ".mp4"}} + + {{else}} + {{end}}
Are you sure you want to delete this photo? @@ -45,4 +52,4 @@
-{{end}} \ No newline at end of file +{{end}} diff --git a/web/templates/photo/permalink.html b/web/templates/photo/permalink.html index 16f6eed..166191b 100644 --- a/web/templates/photo/permalink.html +++ b/web/templates/photo/permalink.html @@ -68,10 +68,15 @@ -
-
- -
+
+ + {{if HasSuffix .Photo.Filename ".mp4"}} + + {{else}} + + {{end}}
@@ -280,4 +285,4 @@
-{{end}} \ No newline at end of file +{{end}}