feat: images

This commit is contained in:
2026-03-02 22:38:01 +01:00
parent e689ab08c9
commit 3cc632e358
19 changed files with 517 additions and 28 deletions
+49
View File
@@ -0,0 +1,49 @@
package images
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"strings"
)
var ErrNotAnImage = errors.New("not an image")
func (s *Service) getMime(path string) (mimeType string, err error) {
info, err := s.fs.Stat(path)
if err != nil {
err = fmt.Errorf("image file info could not be fetched")
}
if mimer, ok := info.(interface{ ContentType() string }); ok {
mimeType = mimer.ContentType()
}
return
}
func (s *Service) GetImage(ctx context.Context, uid string) (img io.Reader, mimeType string, err error) {
path, err := PathFromUID(uid)
if err != nil {
err = fmt.Errorf("could not get path: %w", err)
return
}
mimeType, err = s.getMime(path)
if err != nil {
return
}
if !strings.HasPrefix(mimeType, "image") {
err = ErrNotAnImage
return
}
file, err := s.fs.Read(path)
if err != nil {
err = fmt.Errorf("image file could not be fetched")
}
return bytes.NewBuffer(file), mimeType, err
}
+18
View File
@@ -0,0 +1,18 @@
package images
import "os"
type Service struct {
fs fileSystem
}
func New(fs fileSystem) *Service {
return &Service{
fs: fs,
}
}
type fileSystem interface {
Read(path string) ([]byte, error)
Stat(path string) (os.FileInfo, error)
}