2020-01-01 12:44:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-01-01 15:03:13 +00:00
|
|
|
"mime"
|
2020-01-01 12:44:44 +00:00
|
|
|
"net/http"
|
2020-01-01 15:03:13 +00:00
|
|
|
"path/filepath"
|
2020-01-01 12:44:44 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
2020-01-12 16:48:05 +00:00
|
|
|
if SelectedMediaStorage == nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = w.Write([]byte("No media storage configured"))
|
|
|
|
return
|
|
|
|
}
|
2020-01-01 12:44:44 +00:00
|
|
|
if r.Method != "POST" {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
_, _ = w.Write([]byte("The HTTP method is not allowed, make a POST request"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if contentType := r.Header.Get("content-type"); !strings.Contains(contentType, "multipart/form-data") {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
_, _ = w.Write([]byte("Wrong Content-Type"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := r.ParseMultipartForm(0)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = w.Write([]byte("Failed to parse Multipart"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
authCode := r.Header.Get("authorization")
|
2020-01-10 10:14:47 +00:00
|
|
|
if formAuth := r.FormValue("access_token"); len(authCode) == 0 && len(formAuth) > 0 {
|
|
|
|
authCode = "Bearer " + formAuth
|
2020-01-01 12:44:44 +00:00
|
|
|
}
|
|
|
|
if CheckAuthorization(authCode) {
|
|
|
|
file, header, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = w.Write([]byte("Failed to get file"))
|
|
|
|
return
|
|
|
|
}
|
2020-01-01 14:43:59 +00:00
|
|
|
hashFile, _, _ := r.FormFile("file")
|
|
|
|
defer func() { _ = hashFile.Close() }()
|
2020-01-19 09:55:18 +00:00
|
|
|
fileName, err := getSHA256(hashFile)
|
|
|
|
if err != nil {
|
2020-01-01 14:43:59 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2020-01-19 09:55:18 +00:00
|
|
|
_, _ = w.Write([]byte(err.Error()))
|
2020-01-01 14:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
2020-01-01 15:03:13 +00:00
|
|
|
fileExtension := filepath.Ext(header.Filename)
|
|
|
|
if len(fileExtension) == 0 {
|
|
|
|
// Find correct file extension if original filename does not contain one
|
|
|
|
mimeType := header.Header.Get("content-type")
|
|
|
|
if len(mimeType) > 0 {
|
|
|
|
allExtensions, _ := mime.ExtensionsByType(mimeType)
|
|
|
|
if len(allExtensions) > 0 {
|
|
|
|
fileExtension = allExtensions[0]
|
|
|
|
}
|
|
|
|
}
|
2020-01-01 12:44:44 +00:00
|
|
|
}
|
2020-01-19 10:01:24 +00:00
|
|
|
fileName += strings.ToLower(fileExtension)
|
2020-01-01 12:44:44 +00:00
|
|
|
location, err := SelectedMediaStorage.Upload(fileName, file)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2020-01-19 09:55:18 +00:00
|
|
|
_, _ = w.Write([]byte("Failed to upload original file"))
|
2020-01-01 12:44:44 +00:00
|
|
|
return
|
|
|
|
}
|
2020-01-19 09:55:18 +00:00
|
|
|
if SelectedImageCompression != nil {
|
|
|
|
compressedLocation, err := SelectedImageCompression.Compress(location)
|
|
|
|
if err == nil && len(compressedLocation) > 0 {
|
|
|
|
location = compressedLocation
|
|
|
|
}
|
|
|
|
}
|
2020-01-01 12:44:44 +00:00
|
|
|
w.Header().Add("Location", location)
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
_, _ = w.Write([]byte("Forbidden, there was a problem with the provided access token"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|