feat: images
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user