2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-03-07 19:05:34 +00:00
|
|
|
"bytes"
|
|
|
|
"mime/multipart"
|
2018-10-25 13:51:47 +00:00
|
|
|
"net/http"
|
2021-05-23 14:47:42 +00:00
|
|
|
"path/filepath"
|
2018-10-25 13:51:47 +00:00
|
|
|
"strconv"
|
|
|
|
|
2020-03-07 19:05:34 +00:00
|
|
|
"github.com/disintegration/imaging"
|
2021-12-09 15:21:07 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-10-25 05:41:47 +00:00
|
|
|
thumbPrefix = "thumb_"
|
|
|
|
thumbnailSize = 90
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
2021-05-23 14:47:42 +00:00
|
|
|
// validMimes is the list of image types allowed to be uploaded.
|
|
|
|
var (
|
|
|
|
validMimes = []string{"image/jpg", "image/jpeg", "image/png", "image/gif"}
|
|
|
|
validExts = []string{".jpg", ".jpeg", ".png", ".gif"}
|
|
|
|
)
|
2020-03-07 17:30:55 +00:00
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// handleUploadMedia handles media file uploads.
|
|
|
|
func handleUploadMedia(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
cleanUp = false
|
|
|
|
)
|
2019-10-15 12:21:32 +00:00
|
|
|
file, err := c.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("media.invalidFile", "error", err.Error()))
|
2019-10-15 12:21:32 +00:00
|
|
|
}
|
2020-03-07 17:30:55 +00:00
|
|
|
|
2021-05-23 14:47:42 +00:00
|
|
|
// Validate file extension.
|
|
|
|
ext := filepath.Ext(file.Filename)
|
|
|
|
if ok := inArray(ext, validExts); !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
|
|
|
app.i18n.Ts("media.unsupportedFileType", "type", ext))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate file's mime.
|
|
|
|
typ := file.Header.Get("Content-type")
|
|
|
|
if ok := inArray(typ, validMimes); !ok {
|
2019-10-15 12:21:32 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("media.unsupportedFileType", "type", typ))
|
2019-10-15 12:21:32 +00:00
|
|
|
}
|
2020-03-07 17:30:55 +00:00
|
|
|
|
2019-10-15 12:21:32 +00:00
|
|
|
// Generate filename
|
2021-06-19 11:41:27 +00:00
|
|
|
fName := makeFilename(file.Filename)
|
2020-03-07 17:30:55 +00:00
|
|
|
|
2019-10-15 12:21:32 +00:00
|
|
|
// Read file contents in memory
|
|
|
|
src, err := file.Open()
|
|
|
|
if err != nil {
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("media.errorReadingFile", "error", err.Error()))
|
2019-10-15 12:21:32 +00:00
|
|
|
}
|
|
|
|
defer src.Close()
|
2020-03-07 17:30:55 +00:00
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// Upload the file.
|
2020-03-07 18:33:22 +00:00
|
|
|
fName, err = app.media.Put(fName, typ, src)
|
2018-10-25 13:51:47 +00:00
|
|
|
if err != nil {
|
2020-03-07 18:33:22 +00:00
|
|
|
app.log.Printf("error uploading file: %v", err)
|
2019-10-15 12:21:32 +00:00
|
|
|
cleanUp = true
|
2018-10-25 13:51:47 +00:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("media.errorUploading", "error", err.Error()))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// If any of the subroutines in this function fail,
|
|
|
|
// the uploaded image should be removed.
|
|
|
|
if cleanUp {
|
2020-03-07 18:33:22 +00:00
|
|
|
app.media.Delete(fName)
|
|
|
|
app.media.Delete(thumbPrefix + fName)
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-10-15 12:21:32 +00:00
|
|
|
// Create thumbnail from file.
|
|
|
|
thumbFile, err := createThumbnail(file)
|
2018-10-25 13:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
cleanUp = true
|
2020-03-07 18:33:22 +00:00
|
|
|
app.log.Printf("error resizing image: %v", err)
|
2018-10-25 13:51:47 +00:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("media.errorResizing", "error", err.Error()))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
2020-03-07 15:07:48 +00:00
|
|
|
|
2019-10-15 12:21:32 +00:00
|
|
|
// Upload thumbnail.
|
2020-03-07 18:33:22 +00:00
|
|
|
thumbfName, err := app.media.Put(thumbPrefix+fName, typ, thumbFile)
|
2019-10-15 12:21:32 +00:00
|
|
|
if err != nil {
|
2018-10-25 13:51:47 +00:00
|
|
|
cleanUp = true
|
2020-03-07 18:33:22 +00:00
|
|
|
app.log.Printf("error saving thumbnail: %v", err)
|
2018-10-25 13:51:47 +00:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("media.errorSavingThumbnail", "error", err.Error()))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
2020-03-07 15:07:48 +00:00
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// Write to the DB.
|
2022-04-03 15:24:40 +00:00
|
|
|
// TODO: cleanup
|
|
|
|
if _, err := app.core.InsertMedia(fName, thumbfName, app.constants.MediaProvider, app.media); err != nil {
|
2018-10-25 13:51:47 +00:00
|
|
|
cleanUp = true
|
2022-04-03 15:24:40 +00:00
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, okResp{true})
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleGetMedia handles retrieval of uploaded media.
|
|
|
|
func handleGetMedia(c echo.Context) error {
|
|
|
|
var (
|
2022-04-03 15:24:40 +00:00
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.Atoi(c.Param("id"))
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
// Fetch one list.
|
|
|
|
if id > 0 {
|
|
|
|
out, err := app.core.GetMedia(id, "", app.media)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
out, err := app.core.GetAllMedia(app.constants.MediaProvider, app.media)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-25 13:51:47 +00: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 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
fname, err := app.core.DeleteMedia(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
2019-10-15 12:21:32 +00:00
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
app.media.Delete(fname)
|
|
|
|
app.media.Delete(thumbPrefix + fname)
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{true})
|
|
|
|
}
|
2020-03-07 19:05:34 +00: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 10:55:52 +00:00
|
|
|
return nil, err
|
2020-03-07 19:05:34 +00: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
|
|
|
|
}
|