feat: generate thumbnail while get and improve thumbnail quality (#1680)

* Use disintegration/imaging to optimize thumbnail quality

* Generate thumbnail if not exists while GET it

* Changes for `go mod tidy`

* Changes for golang comments lint

---------

Co-authored-by: Athurg Feng <athurg@gooth.org>
This commit is contained in:
Athurg Gooth
2023-05-19 20:07:39 +08:00
committed by GitHub
parent 2730b90512
commit 04124a2ace
4 changed files with 55 additions and 48 deletions

View File

@ -164,29 +164,11 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
}
if filetype == "image/jpeg" || filetype == "image/png" {
_, err := sourceFile.Seek(0, io.SeekStart)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to seek file").SetInternal(err)
}
fileBytes, err := io.ReadAll(sourceFile)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to load file").SetInternal(err)
}
thumbnailBytes, err := common.ResizeImageBlob(fileBytes, 302, filetype)
thumbnailPath := path.Join(s.Profile.Data, common.ThumbnailDir, publicID)
err := common.ResizeImageFile(thumbnailPath, filePath, filetype)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate thumbnail").SetInternal(err)
}
dir := filepath.Join(s.Profile.Data, common.ThumbnailPath)
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create directory").SetInternal(err)
}
err = os.WriteFile(filepath.Join(dir, publicID), thumbnailBytes, 0666)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create thumbnail").SetInternal(err)
}
}
resourceCreate = &api.ResourceCreate{
@ -346,7 +328,7 @@ func (s *Server) registerResourceRoutes(g *echo.Group) {
log.Warn(fmt.Sprintf("failed to delete local file with path %s", resource.InternalPath), zap.Error(err))
}
thumbnailPath := filepath.Join(s.Profile.Data, common.ThumbnailPath, resource.PublicID)
thumbnailPath := path.Join(s.Profile.Data, common.ThumbnailDir, resource.PublicID)
err = os.Remove(thumbnailPath)
if err != nil {
log.Warn(fmt.Sprintf("failed to delete local thumbnail with path %s", thumbnailPath), zap.Error(err))
@ -442,9 +424,18 @@ func (s *Server) registerResourcePublicRoutes(g *echo.Group) {
if resource.InternalPath != "" {
resourcePath := resource.InternalPath
if c.QueryParam("thumbnail") == "1" && (resource.Type == "image/jpeg" || resource.Type == "image/png") {
thumbnailPath := filepath.Join(s.Profile.Data, common.ThumbnailPath, resource.PublicID)
thumbnailPath := path.Join(s.Profile.Data, common.ThumbnailDir, resource.PublicID)
if _, err := os.Stat(thumbnailPath); err == nil {
resourcePath = thumbnailPath
} else if os.IsNotExist(err) {
err := common.ResizeImageFile(thumbnailPath, resourcePath, resource.Type)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to resize resource: %s", resourcePath)).SetInternal(err)
}
resourcePath = thumbnailPath
} else {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to check resource thumbnail stat: %s", thumbnailPath)).SetInternal(err)
}
}