diff --git a/services/thumbnails/pkg/preprocessor/preprocessor.go b/services/thumbnails/pkg/preprocessor/preprocessor.go index e03e52e5da..b8949dc953 100644 --- a/services/thumbnails/pkg/preprocessor/preprocessor.go +++ b/services/thumbnails/pkg/preprocessor/preprocessor.go @@ -4,7 +4,6 @@ import ( "archive/zip" "bufio" "bytes" - "fmt" "image" "image/draw" "image/gif" @@ -22,12 +21,15 @@ import ( "github.com/dhowden/tag" ) +// FileConverter is the interface for the file converter type FileConverter interface { Convert(r io.Reader) (interface{}, error) } +// ImageDecoder is a converter for the image file type ImageDecoder struct{} +// Convert reads the image file and returns the thumbnail image func (i ImageDecoder) Convert(r io.Reader) (interface{}, error) { img, err := imaging.Decode(r, imaging.AutoOrientation(true)) if err != nil { @@ -36,8 +38,10 @@ func (i ImageDecoder) Convert(r io.Reader) (interface{}, error) { return img, nil } +// GifDecoder is a converter for the gif file type GifDecoder struct{} +// Convert reads the gif file and returns the thumbnail image func (i GifDecoder) Convert(r io.Reader) (interface{}, error) { img, err := gif.DecodeAll(r) if err != nil { @@ -46,8 +50,10 @@ func (i GifDecoder) Convert(r io.Reader) (interface{}, error) { return img, nil } +// GgsDecoder is a converter for the geogebra slides file type GgsDecoder struct{} +// Convert reads the ggs file and returns the thumbnail image func (g GgsDecoder) Convert(r io.Reader) (interface{}, error) { geogebraThumbnail := "_slide0/geogebra_thumbnail.png" var buf bytes.Buffer @@ -73,11 +79,13 @@ func (g GgsDecoder) Convert(r io.Reader) (interface{}, error) { return img, nil } } - return nil, errors.New(fmt.Sprintf("%s not found", geogebraThumbnail)) + return nil, errors.Errorf("%s not found", geogebraThumbnail) } +// AudioDecoder is a converter for the audio file type AudioDecoder struct{} +// Convert reads the audio file and extracts the thumbnail image from the id3 tag func (i AudioDecoder) Convert(r io.Reader) (interface{}, error) { b, err := io.ReadAll(r) if err != nil { @@ -101,10 +109,12 @@ func (i AudioDecoder) Convert(r io.Reader) (interface{}, error) { return converter.Convert(bytes.NewReader(picture.Data)) } +// TxtToImageConverter is a converter for the text file type TxtToImageConverter struct { fontLoader *FontLoader } +// Convert reads the text file and renders it into a thumbnail image func (t TxtToImageConverter) Convert(r io.Reader) (interface{}, error) { img := image.NewRGBA(image.Rect(0, 0, 640, 480)) @@ -221,6 +231,7 @@ func drawWord(canvas *font.Drawer, word string, minX, maxX, incY, maxY fixed.Int } } +// ForType returns the converter for the specified mimeType func ForType(mimeType string, opts map[string]interface{}) FileConverter { // We can ignore the error here because we parse it in IsMimeTypeSupported before and if it fails // return the service call. So we should only get here when the mimeType parses fine.