-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get image blob in backend (#495)
* feat: get image blob in backend * chore: update
- Loading branch information
Showing
10 changed files
with
175 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
plugin/crawler/crawler.go → plugin/http_getter/http_getter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// crawler is using to get resources from url. | ||
// getter is using to get resources from url. | ||
// * Get metadata for website; | ||
// * Get image blob to avoid CORS; | ||
package crawler | ||
package getter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package getter | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
type Image struct { | ||
Blob []byte | ||
Mediatype string | ||
} | ||
|
||
func GetImage(urlStr string) (*Image, error) { | ||
if _, err := url.Parse(urlStr); err != nil { | ||
return nil, err | ||
} | ||
|
||
response, err := http.Get(urlStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer response.Body.Close() | ||
|
||
mediatype, err := getMediatype(response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !strings.HasPrefix(mediatype, "image/") { | ||
return nil, fmt.Errorf("Wrong image mediatype") | ||
} | ||
|
||
bodyBytes, err := io.ReadAll(response.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
image := &Image{ | ||
Blob: bodyBytes, | ||
Mediatype: mediatype, | ||
} | ||
return image, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package getter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetImage(t *testing.T) { | ||
tests := []struct { | ||
urlStr string | ||
}{ | ||
{ | ||
urlStr: "https://star-history.com/bytebase.webp", | ||
}, | ||
} | ||
for _, test := range tests { | ||
_, err := GetImage(test.urlStr) | ||
require.NoError(t, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package getter | ||
|
||
import ( | ||
"mime" | ||
"net/http" | ||
) | ||
|
||
func getMediatype(response *http.Response) (string, error) { | ||
contentType := response.Header.Get("content-type") | ||
mediatype, _, err := mime.ParseMediaType(contentType) | ||
if err != nil { | ||
return "", err | ||
} | ||
return mediatype, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package server | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/labstack/echo/v4" | ||
getter "github.com/usememos/memos/plugin/http_getter" | ||
metric "github.com/usememos/memos/plugin/metrics" | ||
) | ||
|
||
func (s *Server) registerCrawlerPublicRoutes(g *echo.Group) { | ||
g.GET("/get/httpmeta", func(c echo.Context) error { | ||
ctx := c.Request().Context() | ||
urlStr := c.QueryParam("url") | ||
if urlStr == "" { | ||
return echo.NewHTTPError(http.StatusBadRequest, "Missing website url") | ||
} | ||
if _, err := url.Parse(urlStr); err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, "Wrong url").SetInternal(err) | ||
} | ||
|
||
htmlMeta, err := getter.GetHTMLMeta(urlStr) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusNotAcceptable, fmt.Sprintf("Failed to get website meta with url: %s", urlStr)).SetInternal(err) | ||
} | ||
s.Collector.Collect(ctx, &metric.Metric{ | ||
Name: "getter used", | ||
Labels: map[string]string{ | ||
"type": "httpmeta", | ||
}, | ||
}) | ||
|
||
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8) | ||
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(htmlMeta)); err != nil { | ||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode website HTML meta").SetInternal(err) | ||
} | ||
return nil | ||
}) | ||
g.GET("/get/image", func(c echo.Context) error { | ||
ctx := c.Request().Context() | ||
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.StatusNotAcceptable, fmt.Sprintf("Failed to get image url: %s", urlStr)).SetInternal(err) | ||
} | ||
s.Collector.Collect(ctx, &metric.Metric{ | ||
Name: "getter used", | ||
Labels: map[string]string{ | ||
"type": "image", | ||
}, | ||
}) | ||
|
||
c.Response().Writer.WriteHeader(http.StatusOK) | ||
c.Response().Writer.Header().Set("Content-Type", image.Mediatype) | ||
if _, err := c.Response().Writer.Write(image.Blob); err != nil { | ||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to write image blob").SetInternal(err) | ||
} | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters