|
|
@ -7,6 +7,7 @@ import ( |
|
|
|
"mime" |
|
|
|
"net/http" |
|
|
|
"path" |
|
|
|
"path/filepath" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
|
|
|
@ -50,18 +51,33 @@ func ParseUpload(r *http.Request, sizeLimit int64) (pu *ParsedUpload, e error) { |
|
|
|
|
|
|
|
pu.OriginalDataSize = len(pu.Data) |
|
|
|
pu.UncompressedData = pu.Data |
|
|
|
// println("received data", len(pu.Data), "isGzipped", pu.IsGzipped, "mime", pu.MimeType, "name", pu.FileName)
|
|
|
|
if pu.MimeType == "" { |
|
|
|
pu.MimeType = http.DetectContentType(pu.Data) |
|
|
|
// println("detected mimetype to", pu.MimeType)
|
|
|
|
if pu.MimeType == "application/octet-stream" { |
|
|
|
pu.MimeType = "" |
|
|
|
} |
|
|
|
} |
|
|
|
if pu.IsGzipped { |
|
|
|
if unzipped, e := util.UnGzipData(pu.Data); e == nil { |
|
|
|
pu.OriginalDataSize = len(unzipped) |
|
|
|
pu.UncompressedData = unzipped |
|
|
|
// println("ungzipped data size", len(unzipped))
|
|
|
|
} |
|
|
|
} else if shouldGzip, _ := util.IsGzippableFileType("", pu.MimeType); pu.MimeType == "" || shouldGzip { |
|
|
|
} else { |
|
|
|
ext := filepath.Base(pu.FileName) |
|
|
|
if shouldGzip, iAmSure := util.IsGzippableFileType(ext, pu.MimeType); pu.MimeType == "" && !iAmSure || shouldGzip && iAmSure { |
|
|
|
// println("ext", ext, "iAmSure", iAmSure, "shouldGzip", shouldGzip, "mimeType", pu.MimeType)
|
|
|
|
if compressedData, err := util.GzipData(pu.Data); err == nil { |
|
|
|
if len(compressedData)*10 < len(pu.Data)*9 { |
|
|
|
pu.Data = compressedData |
|
|
|
pu.IsGzipped = true |
|
|
|
} |
|
|
|
// println("gzipped data size", len(compressedData))
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|