2020-10-14 19:20:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2021-06-18 12:32:03 +00:00
|
|
|
|
|
|
|
"git.jlel.se/jlelse/GoBlog/pkgs/contenttype"
|
2020-10-14 19:20:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const micropubMediaSubPath = "/media"
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) serveMicropubMedia(w http.ResponseWriter, r *http.Request) {
|
2021-02-08 17:51:07 +00:00
|
|
|
if !strings.Contains(r.Context().Value(indieAuthScope).(string), "media") {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, "media scope missing", http.StatusForbidden)
|
2020-11-22 19:30:02 +00:00
|
|
|
return
|
2020-10-14 19:20:17 +00:00
|
|
|
}
|
2021-06-18 12:32:03 +00:00
|
|
|
if ct := r.Header.Get(contentType); !strings.Contains(ct, contenttype.MultipartForm) {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, "wrong content-type", http.StatusBadRequest)
|
2020-10-14 19:20:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err := r.ParseMultipartForm(0)
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusBadRequest)
|
2020-10-14 19:20:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
file, header, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusBadRequest)
|
2020-10-14 19:20:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() { _ = file.Close() }()
|
|
|
|
hashFile, _, _ := r.FormFile("file")
|
|
|
|
defer func() { _ = hashFile.Close() }()
|
|
|
|
fileName, err := getSHA256(hashFile)
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2020-10-14 19:20:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fileExtension := filepath.Ext(header.Filename)
|
|
|
|
if len(fileExtension) == 0 {
|
|
|
|
// Find correct file extension if original filename does not contain one
|
|
|
|
mimeType := header.Header.Get(contentType)
|
|
|
|
if len(mimeType) > 0 {
|
|
|
|
allExtensions, _ := mime.ExtensionsByType(mimeType)
|
|
|
|
if len(allExtensions) > 0 {
|
|
|
|
fileExtension = allExtensions[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fileName += strings.ToLower(fileExtension)
|
2021-01-10 14:59:43 +00:00
|
|
|
// Save file
|
2021-06-06 12:39:42 +00:00
|
|
|
location, err := a.uploadFile(fileName, file)
|
2020-10-14 19:20:17 +00:00
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, "failed to save original file: "+err.Error(), http.StatusInternalServerError)
|
2020-10-14 19:20:17 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-21 18:10:32 +00:00
|
|
|
// Try to compress file (only when not in private mode)
|
2021-06-20 13:18:02 +00:00
|
|
|
if pm := a.cfg.PrivateMode; pm == nil || !pm.Enabled {
|
|
|
|
compressedLocation, compressionErr := a.compressMediaFile(location)
|
|
|
|
if compressionErr != nil {
|
|
|
|
a.serveError(w, r, "failed to compress file: "+compressionErr.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2021-02-09 18:30:07 +00:00
|
|
|
}
|
2021-06-20 13:18:02 +00:00
|
|
|
// Overwrite location
|
|
|
|
if compressedLocation != "" {
|
|
|
|
location = compressedLocation
|
2020-10-14 19:20:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-13 10:28:46 +00:00
|
|
|
http.Redirect(w, r, location, http.StatusCreated)
|
2020-10-14 19:20:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-20 13:18:02 +00:00
|
|
|
type fileUploadFunc func(filename string, f io.Reader) (location string, err error)
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) uploadFile(filename string, f io.Reader) (string, error) {
|
|
|
|
ms := a.cfg.Micropub.MediaStorage
|
2021-01-10 14:59:43 +00:00
|
|
|
if ms != nil && ms.BunnyStorageKey != "" && ms.BunnyStorageName != "" {
|
2021-06-19 06:37:16 +00:00
|
|
|
return a.uploadToBunny(filename, f)
|
2021-01-10 14:59:43 +00:00
|
|
|
}
|
|
|
|
loc, err := saveMediaFile(filename, f)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if ms != nil && ms.MediaURL != "" {
|
|
|
|
return ms.MediaURL + loc, nil
|
|
|
|
}
|
2021-06-10 20:09:50 +00:00
|
|
|
return a.getFullAddress(loc), nil
|
2021-01-10 14:59:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-19 06:37:16 +00:00
|
|
|
func (a *goBlog) uploadToBunny(filename string, f io.Reader) (location string, err error) {
|
|
|
|
config := a.cfg.Micropub.MediaStorage
|
2021-02-09 18:30:07 +00:00
|
|
|
if config == nil || config.BunnyStorageName == "" || config.BunnyStorageKey == "" || config.MediaURL == "" {
|
|
|
|
return "", errors.New("Bunny storage not completely configured")
|
|
|
|
}
|
2021-01-10 14:59:43 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPut, fmt.Sprintf("https://storage.bunnycdn.com/%s/%s", url.PathEscape(config.BunnyStorageName), url.PathEscape(filename)), f)
|
|
|
|
req.Header.Add("AccessKey", config.BunnyStorageKey)
|
2021-06-19 06:37:16 +00:00
|
|
|
resp, err := a.httpClient.Do(req)
|
2021-03-31 07:29:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
_, _ = io.Copy(io.Discard, resp.Body)
|
|
|
|
if resp.StatusCode != http.StatusCreated {
|
2020-10-14 19:20:17 +00:00
|
|
|
return "", errors.New("failed to upload file to BunnyCDN")
|
|
|
|
}
|
2021-01-10 14:59:43 +00:00
|
|
|
return config.MediaURL + "/" + filename, nil
|
2020-10-14 19:20:17 +00:00
|
|
|
}
|