listmonk/cmd/media.go

179 lines
4.7 KiB
Go
Raw Permalink Normal View History

2018-10-25 15:51:47 +02:00
package main
import (
2020-03-07 20:05:34 +01:00
"bytes"
"mime/multipart"
2018-10-25 15:51:47 +02:00
"net/http"
"strconv"
2020-03-07 20:05:34 +01:00
"github.com/disintegration/imaging"
2020-03-07 16:07:48 +01:00
"github.com/gofrs/uuid"
"github.com/knadh/listmonk/internal/media"
2018-10-25 15:51:47 +02:00
"github.com/labstack/echo"
)
const (
thumbPrefix = "thumb_"
thumbnailSize = 90
2018-10-25 15:51:47 +02:00
)
2020-03-07 18:30:55 +01:00
// imageMimes is the list of image types allowed to be uploaded.
var imageMimes = []string{
"image/jpg",
"image/jpeg",
"image/png",
"image/svg",
"image/gif"}
2018-10-25 15:51:47 +02:00
// handleUploadMedia handles media file uploads.
func handleUploadMedia(c echo.Context) error {
var (
app = c.Get("app").(*App)
cleanUp = false
)
file, err := c.FormFile("file")
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("media.invalidFile", "error", err.Error()))
}
2020-03-07 18:30:55 +01:00
// Validate MIME type with the list of allowed types.
var typ = file.Header.Get("Content-type")
2020-03-07 18:30:55 +01:00
if ok := validateMIME(typ, imageMimes); !ok {
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("media.unsupportedFileType", "type", typ))
}
2020-03-07 18:30:55 +01:00
// Generate filename
fName := generateFileName(file.Filename)
2020-03-07 18:30:55 +01:00
// Read file contents in memory
src, err := file.Open()
if err != nil {
2020-12-19 11:55:52 +01:00
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("media.errorReadingFile", "error", err.Error()))
}
defer src.Close()
2020-03-07 18:30:55 +01:00
2018-10-25 15:51:47 +02:00
// Upload the file.
fName, err = app.media.Put(fName, typ, src)
2018-10-25 15:51:47 +02:00
if err != nil {
app.log.Printf("error uploading file: %v", err)
cleanUp = true
2018-10-25 15:51:47 +02:00
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("media.errorUploading", "error", err.Error()))
2018-10-25 15:51:47 +02:00
}
defer func() {
// If any of the subroutines in this function fail,
// the uploaded image should be removed.
if cleanUp {
app.media.Delete(fName)
app.media.Delete(thumbPrefix + fName)
2018-10-25 15:51:47 +02:00
}
}()
// Create thumbnail from file.
thumbFile, err := createThumbnail(file)
2018-10-25 15:51:47 +02:00
if err != nil {
cleanUp = true
app.log.Printf("error resizing image: %v", err)
2018-10-25 15:51:47 +02:00
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("media.errorResizing", "error", err.Error()))
2018-10-25 15:51:47 +02:00
}
2020-03-07 16:07:48 +01:00
// Upload thumbnail.
thumbfName, err := app.media.Put(thumbPrefix+fName, typ, thumbFile)
if err != nil {
2018-10-25 15:51:47 +02:00
cleanUp = true
app.log.Printf("error saving thumbnail: %v", err)
2018-10-25 15:51:47 +02:00
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("media.errorSavingThumbnail", "error", err.Error()))
2018-10-25 15:51:47 +02:00
}
2020-03-07 16:07:48 +01:00
uu, err := uuid.NewV4()
if err != nil {
app.log.Printf("error generating UUID: %v", err)
2020-12-19 11:55:52 +01:00
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("globals.messages.errorUUID", "error", err.Error()))
2020-03-07 16:07:48 +01:00
}
2018-10-25 15:51:47 +02:00
// Write to the DB.
if _, err := app.queries.InsertMedia.Exec(uu, fName, thumbfName, app.constants.MediaProvider); err != nil {
2018-10-25 15:51:47 +02:00
cleanUp = true
app.log.Printf("error inserting uploaded file to db: %v", err)
2018-10-25 15:51:47 +02:00
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("globals.messages.errorCreating",
"name", "{globals.terms.media}", "error", pqErrMsg(err)))
2018-10-25 15:51:47 +02:00
}
return c.JSON(http.StatusOK, okResp{true})
}
// handleGetMedia handles retrieval of uploaded media.
func handleGetMedia(c echo.Context) error {
var (
app = c.Get("app").(*App)
out = []media.Media{}
2018-10-25 15:51:47 +02:00
)
if err := app.queries.GetMedia.Select(&out, app.constants.MediaProvider); err != nil {
2018-10-25 15:51:47 +02:00
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("globals.messages.errorFetching",
"name", "{globals.terms.media}", "error", pqErrMsg(err)))
2018-10-25 15:51:47 +02:00
}
for i := 0; i < len(out); i++ {
out[i].URL = app.media.Get(out[i].Filename)
out[i].ThumbURL = app.media.Get(out[i].Thumb)
2018-10-25 15:51:47 +02:00
}
return c.JSON(http.StatusOK, okResp{out})
}
// deleteMedia handles deletion of uploaded media.
func handleDeleteMedia(c echo.Context) error {
var (
app = c.Get("app").(*App)
id, _ = strconv.Atoi(c.Param("id"))
)
if id < 1 {
2020-12-19 11:55:52 +01:00
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
2018-10-25 15:51:47 +02:00
}
var m media.Media
if err := app.queries.DeleteMedia.Get(&m, id); err != nil {
2018-10-25 15:51:47 +02:00
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("globals.messages.errorDeleting",
"name", "{globals.terms.media}", "error", pqErrMsg(err)))
2018-10-25 15:51:47 +02:00
}
app.media.Delete(m.Filename)
app.media.Delete(thumbPrefix + m.Filename)
2018-10-25 15:51:47 +02:00
return c.JSON(http.StatusOK, okResp{true})
}
2020-03-07 20:05:34 +01:00
// createThumbnail reads the file object and returns a smaller image
func createThumbnail(file *multipart.FileHeader) (*bytes.Reader, error) {
src, err := file.Open()
if err != nil {
return nil, err
}
defer src.Close()
img, err := imaging.Decode(src)
if err != nil {
2020-12-19 11:55:52 +01:00
return nil, err
2020-03-07 20:05:34 +01:00
}
// Encode the image into a byte slice as PNG.
var (
thumb = imaging.Resize(img, thumbnailSize, 0, imaging.Lanczos)
out bytes.Buffer
)
if err := imaging.Encode(&out, thumb, imaging.PNG); err != nil {
return nil, err
}
return bytes.NewReader(out.Bytes()), nil
}