-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: get image blob in backend #495
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
Check failure Code scanning / CodeQL Uncontrolled data used in network request
The [URL](1) of this request depends on a [user-provided value](2).
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Uncontrolled data used in network request