mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
fix: fail to open file while generate thumbnail (#1687)
* Fix fail to open file while generate thumbnail * Fix for Uncontrolled data used in path expression check --------- Co-authored-by: Athurg Feng <athurg@gooth.org>
This commit is contained in:
@ -430,7 +430,8 @@ func (s *Server) registerResourcePublicRoutes(g *echo.Group) {
|
|||||||
|
|
||||||
if c.QueryParam("thumbnail") == "1" && common.HasPrefixes(resource.Type, "image/png", "image/jpeg") {
|
if c.QueryParam("thumbnail") == "1" && common.HasPrefixes(resource.Type, "image/png", "image/jpeg") {
|
||||||
ext := filepath.Ext(filename)
|
ext := filepath.Ext(filename)
|
||||||
thumbnailPath := path.Join(s.Profile.Data, thumbnailImagePath, resource.PublicID+ext)
|
thumbnailDir := path.Join(s.Profile.Data, thumbnailImagePath)
|
||||||
|
thumbnailPath := path.Join(thumbnailDir, resource.PublicID+ext)
|
||||||
if _, err := os.Stat(thumbnailPath); err != nil {
|
if _, err := os.Stat(thumbnailPath); err != nil {
|
||||||
if !errors.Is(err, os.ErrNotExist) {
|
if !errors.Is(err, os.ErrNotExist) {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to check thumbnail image stat: %s", thumbnailPath)).SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to check thumbnail image stat: %s", thumbnailPath)).SetInternal(err)
|
||||||
@ -442,6 +443,11 @@ func (s *Server) registerResourcePublicRoutes(g *echo.Group) {
|
|||||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to decode thumbnail image: %s", thumbnailPath)).SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to decode thumbnail image: %s", thumbnailPath)).SetInternal(err)
|
||||||
}
|
}
|
||||||
thumbnailImage := imaging.Resize(src, 512, 0, imaging.Lanczos)
|
thumbnailImage := imaging.Resize(src, 512, 0, imaging.Lanczos)
|
||||||
|
|
||||||
|
if err := os.MkdirAll(thumbnailDir, os.ModePerm); err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to create thumbnail dir: %s", thumbnailDir)).SetInternal(err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := imaging.Save(thumbnailImage, thumbnailPath); err != nil {
|
if err := imaging.Save(thumbnailImage, thumbnailPath); err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to resize thumbnail image: %s", thumbnailPath)).SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to resize thumbnail image: %s", thumbnailPath)).SetInternal(err)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user