Fix GIF and PNG uploads

This commit is contained in:
Noah Petherbridge 2023-06-25 23:03:41 -07:00
parent e051da21b5
commit 7d4326e251
4 changed files with 28 additions and 23 deletions

View File

@ -26,19 +26,7 @@ func CSRF(handler http.Handler) http.Handler {
// If we are running a POST request, validate the CSRF form value. // If we are running a POST request, validate the CSRF form value.
if r.Method != http.MethodGet { if r.Method != http.MethodGet {
r.Body = http.MaxBytesReader(w, r.Body, config.MaxBodySize) r.ParseMultipartForm(config.MultipartMaxMemory)
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
check := r.FormValue(config.CSRFInputName) check := r.FormValue(config.CSRFInputName)
if check != token { if check != token {
log.Error("CSRF mismatch! %s <> %s", check, token) log.Error("CSRF mismatch! %s <> %s", check, token)

View File

@ -35,7 +35,10 @@ type UploadConfig struct {
// - error on errors // - error on errors
func UploadPhoto(cfg UploadConfig) (string, string, error) { func UploadPhoto(cfg UploadConfig) (string, string, error) {
// Validate and normalize the extension. // Validate and normalize the extension.
var extension = strings.ToLower(cfg.Extension) var (
extension = strings.ToLower(cfg.Extension)
dbExtension = extension
)
switch extension { switch extension {
case ".jpg": case ".jpg":
fallthrough fallthrough
@ -45,16 +48,18 @@ func UploadPhoto(cfg UploadConfig) (string, string, error) {
extension = ".jpg" extension = ".jpg"
case ".png": case ".png":
extension = ".png" extension = ".png"
dbExtension = ".jpg"
case ".gif": case ".gif":
extension = ".mp4" extension = ".gif"
dbExtension = ".mp4"
default: default:
return "", "", errors.New("unsupported image extension, must be jpg or png") return "", "", errors.New("unsupported image extension, must be jpg or png")
} }
// Decide on a filename for this photo. // Decide on a filename for this photo.
var ( var (
filename = NewFilename(extension) filename = NewFilename(dbExtension)
cropFilename = NewFilename(extension) cropFilename = NewFilename(dbExtension)
) )
// Decode the image using exiffix, which will auto-rotate jpeg images // Decode the image using exiffix, which will auto-rotate jpeg images

View File

@ -28,7 +28,14 @@
<input type="hidden" name="confirm" value="true"> <input type="hidden" name="confirm" value="true">
<div class="image block"> <div class="image block">
<!-- GIF video? -->
{{if HasSuffix .Photo.Filename ".mp4"}}
<video autoplay loop controls>
<source src="{{PhotoURL .Photo.Filename}}" type="video/mp4">
</video>
{{else}}
<img src="{{PhotoURL .Photo.Filename}}"> <img src="{{PhotoURL .Photo.Filename}}">
{{end}}
</div> </div>
<div class="block"> <div class="block">
Are you sure you want to delete this photo? Are you sure you want to delete this photo?

View File

@ -68,10 +68,15 @@
</div> </div>
</header> </header>
<div class="card-image"> <div class="card-image has-text-centered">
<figure class="image"> <!-- GIF video? -->
{{if HasSuffix .Photo.Filename ".mp4"}}
<video autoplay loop controls>
<source src="{{PhotoURL .Photo.Filename}}" type="video/mp4">
</video>
{{else}}
<img src="{{PhotoURL .Photo.Filename}}"> <img src="{{PhotoURL .Photo.Filename}}">
</figure> {{end}}
</div> </div>
<div class="card-content"> <div class="card-content">