Skip to content
Snippets Groups Projects
Commit 5b1f7cdf authored by Dean's avatar Dean
Browse files

version 1.6.5: update file extension logic

parent c97d9277
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,7 @@ import (
"path/filepath"
"regexp"
"strconv"
"strings"
"owo.codes/whats-this/api/lib/apierrors"
"owo.codes/whats-this/api/lib/db"
......@@ -28,7 +29,26 @@ const maxMemory = 1000 * 1000 * 50 // 50 MB
const fieldName = "files[]"
// File extension regex.
var fileExtRegex = regexp.MustCompile(`(\.[a-z0-9_.-]+)$`)
var fileExtRegex = regexp.MustCompile(`(\.[a-z0-9_-]+)$`)
// .*.gz extension regex.
var gzExtRegex = regexp.MustCompile(`(\.[a-z0-9_-]+\.gz)$`)
// determineExtension returns a file extension (including leading dot) for a
// filename.
func determineExtension(filename string) string {
ext := ""
if strings.HasSuffix(filename, ".gz") {
ext = gzExtRegex.FindString(filename)
}
if ext == "" && filename != "" {
ext = fileExtRegex.FindString(filename)
}
if len(ext) > 20 {
ext = ext[0:20] // limit ext length to 20 chars
}
return ext
}
// fileResponse represents a file response in an upload request.
type fileResponse struct {
......@@ -123,13 +143,7 @@ func UploadPomf(associateObjectsWithUser bool) func(http.ResponseWriter, *http.R
}
// Determine object extension for response.
ext := ""
if file.Filename != "" {
ext = fileExtRegex.FindString(file.Filename)
}
if len(ext) > 20 {
ext = ext[0:20] // limit ext length to 20 chars
}
ext := determineExtension(file.Filename)
// Determine Content-Type
contentType := "application/octet-stream"
......
......@@ -26,7 +26,7 @@ import (
const (
configLocationUnix = "/etc/whats-this/api/config.toml"
shutdownTimeout = 10 * time.Second
version = "1.6.4"
version = "1.6.5"
)
// printConfiguration iterates through a configuration map[string]interface{}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment