✨ Decode configs of image files to set Width and Height
This commit is contained in:
parent
56ddbcce4f
commit
be8fedcb65
2
go.mod
2
go.mod
|
@ -14,6 +14,8 @@ require github.com/yuin/goldmark v1.6.0
|
|||
|
||||
require github.com/yuin/goldmark-emoji v1.0.2
|
||||
|
||||
require golang.org/x/image v0.14.0
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v0.3.1 // indirect
|
||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -23,6 +23,8 @@ github.com/yuin/goldmark v1.6.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5ta
|
|||
github.com/yuin/goldmark-emoji v1.0.2 h1:c/RgTShNgHTtc6xdz2KKI74jJr6rWi7FPgnP9GAsO5s=
|
||||
github.com/yuin/goldmark-emoji v1.0.2/go.mod h1:RhP/RWpexdp+KHs7ghKnifRoIs/Bq4nDS7tRbCkOwKY=
|
||||
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
|
||||
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
|
||||
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
|
|
@ -1,10 +1,20 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
_ "golang.org/x/image/bmp"
|
||||
_ "golang.org/x/image/webp"
|
||||
|
||||
"source.toby3d.me/toby3d/home/internal/domain"
|
||||
"source.toby3d.me/toby3d/home/internal/static"
|
||||
|
@ -23,19 +33,39 @@ func NewFileServerStaticRepository(root fs.FS) static.Repository {
|
|||
func (repo *fileServerStaticRepository) Get(ctx context.Context, p string) (*domain.File, error) {
|
||||
info, err := fs.Stat(repo.root, p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot stat static on path '%s': %w", p, err)
|
||||
return nil, fmt.Errorf("cannot read static info on path '%s': %w", p, err)
|
||||
}
|
||||
|
||||
content, err := fs.ReadFile(repo.root, p)
|
||||
f, err := repo.root.Open(p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot open static on path '%s': %w", p, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
content, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read static content on path '%s': %w", p, err)
|
||||
}
|
||||
|
||||
return &domain.File{
|
||||
out := &domain.File{
|
||||
Path: p,
|
||||
Updated: info.ModTime(),
|
||||
Content: content,
|
||||
}, nil
|
||||
}
|
||||
|
||||
parts := strings.Split(out.MediaType(), "/")
|
||||
if len(parts) < 2 || parts[0] != "image" {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
config, _, err := image.DecodeConfig(bytes.NewReader(content))
|
||||
if err != nil {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
out.Width, out.Height = config.Width, config.Height
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (repo *fileServerStaticRepository) Fetch(ctx context.Context, d string) (domain.Files, int, error) {
|
||||
|
|
Loading…
Reference in New Issue