Files
opencloud/services/webdav/pkg/thumbnail/workflow/encode_imaging.go
T
Dominik Schmidt de467d7dd8 feat(webdav): write a thumbnail in the type the source maps to
A source type maps onto the type its thumbnail is returned in, and that
type is asked for by the name the generator knows it under. The answer
carries that type whether it was just generated or came from the cache,
which is also what the cache key says.
2026-10-19 14:57:35 +02:00

40 lines
914 B
Go

//go:build !enable_vips
package workflow
import (
"bytes"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
)
// encodeForUpload turns the value produced by the preprocessing step into
// bytes for upload to the generator, and says which type they are in.
func encodeForUpload(v any, mimeType string) ([]byte, string, error) {
switch data := v.(type) {
case image.Image:
var buf bytes.Buffer
if mimeType == "image/jpeg" || mimeType == "image/jpg" {
err := jpeg.Encode(&buf, data, &jpeg.Options{Quality: 85})
return buf.Bytes(), "image/jpeg", err
}
err := png.Encode(&buf, data)
return buf.Bytes(), "image/png", err
case *gif.GIF:
var buf bytes.Buffer
if err := gif.EncodeAll(&buf, data); err != nil {
return nil, "", fmt.Errorf("encode gif: %w", err)
}
return buf.Bytes(), "image/gif", nil
default:
return nil, "", fmt.Errorf("unsupported converted type %T", v)
}
}