2023-07-06 16:56:42 +03:00
|
|
|
package v1
|
2022-11-19 13:43:56 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2023-09-17 17:55:13 +03:00
|
|
|
|
2023-03-14 03:38:54 +03:00
|
|
|
getter "github.com/usememos/memos/plugin/http-getter"
|
2022-11-19 13:43:56 +03:00
|
|
|
)
|
|
|
|
|
2023-07-06 16:56:42 +03:00
|
|
|
func (*APIV1Service) registerGetterPublicRoutes(g *echo.Group) {
|
2023-07-15 18:30:20 +03:00
|
|
|
// GET /get/image?url={url} - Get image.
|
2023-08-09 17:30:27 +03:00
|
|
|
g.GET("/get/image", GetImage)
|
2023-08-09 16:53:06 +03:00
|
|
|
}
|
|
|
|
|
2023-08-09 17:30:27 +03:00
|
|
|
// GetImage godoc
|
2023-08-09 16:53:06 +03:00
|
|
|
//
|
2023-08-09 17:30:27 +03:00
|
|
|
// @Summary Get GetImage from URL
|
2023-11-11 18:21:15 +03:00
|
|
|
// @Tags image-url
|
2023-08-09 17:30:27 +03:00
|
|
|
// @Produce GetImage/*
|
2023-08-09 16:53:06 +03:00
|
|
|
// @Param url query string true "Image url"
|
|
|
|
// @Success 200 {object} nil "Image"
|
2023-08-09 17:30:27 +03:00
|
|
|
// @Failure 400 {object} nil "Missing GetImage url | Wrong url | Failed to get GetImage url: %s"
|
|
|
|
// @Failure 500 {object} nil "Failed to write GetImage blob"
|
|
|
|
// @Router /o/get/GetImage [GET]
|
|
|
|
func GetImage(c echo.Context) error {
|
2023-08-09 16:53:06 +03:00
|
|
|
urlStr := c.QueryParam("url")
|
|
|
|
if urlStr == "" {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Missing image url")
|
|
|
|
}
|
|
|
|
if _, err := url.Parse(urlStr); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Wrong url").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
image, err := getter.GetImage(urlStr)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Failed to get image url: %s", urlStr)).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Writer.WriteHeader(http.StatusOK)
|
|
|
|
c.Response().Writer.Header().Set("Content-Type", image.Mediatype)
|
|
|
|
c.Response().Writer.Header().Set(echo.HeaderCacheControl, "max-age=31536000, immutable")
|
|
|
|
if _, err := c.Response().Writer.Write(image.Blob); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to write image blob").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
2022-11-19 13:43:56 +03:00
|
|
|
}
|