1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-16 11:12:44 +00:00
cchat-gtk/internal/gts/httputil/image.go

237 lines
5.4 KiB
Go
Raw Normal View History

2020-06-06 07:44:36 +00:00
package httputil
import (
2021-01-05 02:05:33 +00:00
"bufio"
"context"
2020-06-06 07:44:36 +00:00
"io"
"mime"
"net/http"
"net/url"
"path"
2020-06-06 07:44:36 +00:00
"strings"
2021-01-05 02:05:33 +00:00
"sync"
2020-06-06 07:44:36 +00:00
"github.com/diamondburned/cchat-gtk/internal/gts"
"github.com/diamondburned/cchat-gtk/internal/log"
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
2020-06-06 07:44:36 +00:00
"github.com/diamondburned/imgutil"
2020-12-25 08:32:08 +00:00
"github.com/gotk3/gotk3/cairo"
2020-06-06 07:44:36 +00:00
"github.com/gotk3/gotk3/gdk"
2020-12-25 08:32:08 +00:00
"github.com/gotk3/gotk3/gtk"
2020-06-06 07:44:36 +00:00
"github.com/pkg/errors"
)
2021-01-05 02:05:33 +00:00
// bufferPool provides a sync.Pool of *bufio.Writers. This is used to reduce the
// amount of cgo calls, by writing bytes in larger chunks.
//
// Technically, httpcache already wraps its cached reader around a bufio.Reader,
// but we have no control over the buffer size.
var bufferPool = sync.Pool{
New: func() interface{} {
// Allocate a 512KB buffer by default.
const defaultBufSz = 512 * 1024
return bufio.NewWriterSize(nil, defaultBufSz)
},
}
func bufferedWriter(w io.Writer) *bufio.Writer {
buf := bufferPool.Get().(*bufio.Writer)
buf.Reset(w)
return buf
}
func returnBufferedWriter(buf *bufio.Writer) {
// Unreference the internal reader.
buf.Reset(nil)
bufferPool.Put(buf)
}
2020-06-07 04:27:28 +00:00
type ImageContainer interface {
primitives.Connector
2020-06-07 04:27:28 +00:00
SetFromPixbuf(*gdk.Pixbuf)
SetFromAnimation(*gdk.PixbufAnimation)
2020-12-20 08:18:23 +00:00
GetSizeRequest() (w, h int)
}
2020-12-25 08:32:08 +00:00
type SurfaceContainer interface {
ImageContainer
GetScaleFactor() int
SetFromSurface(*cairo.Surface)
}
var (
_ ImageContainer = (*gtk.Image)(nil)
_ SurfaceContainer = (*gtk.Image)(nil)
)
type surfaceWrapper struct {
SurfaceContainer
scale int
}
func (wrapper *surfaceWrapper) SetFromPixbuf(pb *gdk.Pixbuf) {
2020-12-25 08:32:08 +00:00
surface, _ := gdk.CairoSurfaceCreateFromPixbuf(pb, wrapper.scale, nil)
wrapper.SetFromSurface(surface)
}
// AsyncImage loads an image. This method uses the cache. It prefers loading
// SetFromSurface over SetFromPixbuf, but will fallback if needed be.
func AsyncImage(ctx context.Context,
img ImageContainer, imageURL string, procs ...imgutil.Processor) {
2020-07-11 06:48:44 +00:00
if imageURL == "" {
return
}
w, h := img.GetSizeRequest()
2020-12-25 08:32:08 +00:00
scale := 1
surfaceContainer, canSurface := img.(SurfaceContainer)
if canSurface {
scale = surfaceContainer.GetScaleFactor()
}
2021-01-05 02:05:33 +00:00
ctx = primitives.HandleDestroyCtx(ctx, img)
2020-12-25 08:32:08 +00:00
2021-01-05 02:05:33 +00:00
go func() {
// Try and guess the MIME type from the URL.
mimeType := mime.TypeByExtension(urlExt(imageURL))
r, err := get(ctx, imageURL, true)
if err != nil {
log.Error(errors.Wrap(err, "failed to GET"))
return
2020-12-25 08:32:08 +00:00
}
defer r.Body.Close()
2020-12-25 08:32:08 +00:00
// Try and use the image type from the MIME header over the type from
// the URL, as it is more reliable.
if mime := mimeFromHeaders(r.Header); mime != "" {
mimeType = mime
}
2020-06-07 04:27:28 +00:00
_, fileType := path.Split(mimeType) // abuse split "a/b" to get b
isGIF := fileType == "gif"
if isGIF {
canSurface = false
scale = 1
2020-12-25 08:32:08 +00:00
}
// Only bother with this if we even have HiDPI. We also can't use a
// Surface for a GIF.
if canSurface && scale > 1 {
img = &surfaceWrapper{surfaceContainer, scale}
}
l, err := gdk.PixbufLoaderNewWithType(fileType)
if err != nil {
2021-01-05 02:05:33 +00:00
log.Error(errors.Wrapf(err, "failed to make PixbufLoader type %q", fileType))
return
}
l.Connect("size-prepared", func(l *gdk.PixbufLoader, imgW, imgH int) {
w, h = imgutil.MaxSize(imgW, imgH, w, h)
if w != imgW || h != imgH || scale > 1 {
l.SetSize(w*scale, h*scale)
}
})
load := loadFn(ctx, img, isGIF)
l.Connect("area-prepared", load)
l.Connect("area-updated", load)
2021-01-05 02:05:33 +00:00
// Borrow a buffered writer and return it at the end.
bufWriter := bufferedWriter(l)
defer returnBufferedWriter(bufWriter)
if err := downloadImage(r.Body, bufWriter, procs, isGIF); err != nil {
log.Error(errors.Wrapf(err, "failed to download %q", imageURL))
// Force close after downloading.
}
2021-01-05 02:05:33 +00:00
if err := bufWriter.Flush(); err != nil {
log.Error(errors.Wrapf(err, "failed to flush writer for %q", imageURL))
// Force close after downloading.
}
if err := l.Close(); err != nil {
log.Error(errors.Wrapf(err, "failed to close pixbuf loader for %q", imageURL))
}
}()
2020-06-07 04:27:28 +00:00
}
func urlExt(anyURL string) string {
u, err := url.Parse(anyURL)
if err != nil {
return path.Ext(strings.SplitN(anyURL, "?", 1)[0])
}
return path.Ext(u.Path)
}
func mimeFromHeaders(headers http.Header) string {
cType := headers.Get("Content-Type")
if cType == "" {
return ""
}
media, _, err := mime.ParseMediaType(cType)
if err != nil {
return ""
}
return media
}
func loadFn(ctx context.Context, img ImageContainer, isGIF bool) func(l *gdk.PixbufLoader) {
var pixbuf interface{}
2020-06-07 04:27:28 +00:00
return func(l *gdk.PixbufLoader) {
if pixbuf == nil {
if !isGIF {
pixbuf, _ = l.GetPixbuf()
} else {
pixbuf, _ = l.GetAnimation()
2020-06-07 04:27:28 +00:00
}
}
switch pixbuf := pixbuf.(type) {
case *gdk.Pixbuf:
execIfCtx(ctx, func() { img.SetFromPixbuf(pixbuf) })
case *gdk.PixbufAnimation:
execIfCtx(ctx, func() { img.SetFromAnimation(pixbuf) })
2020-06-07 04:27:28 +00:00
}
}
2020-06-06 07:44:36 +00:00
}
func execIfCtx(ctx context.Context, fn func()) {
gts.ExecLater(func() {
if ctx.Err() == nil {
fn()
}
})
}
func downloadImage(src io.Reader, dst io.Writer, p []imgutil.Processor, isGIF bool) error {
var err error
2020-06-06 07:44:36 +00:00
// If we have processors, then write directly in there.
if len(p) > 0 {
if !isGIF {
err = imgutil.ProcessStream(dst, src, p)
2020-06-06 07:44:36 +00:00
} else {
err = imgutil.ProcessAnimationStream(dst, src, p)
2020-06-06 07:44:36 +00:00
}
} else {
// Else, directly copy the body over.
_, err = io.Copy(dst, src)
2020-06-06 07:44:36 +00:00
}
if err != nil {
return errors.Wrap(err, "failed to process image")
2020-06-06 07:44:36 +00:00
}
return nil
2020-06-06 07:44:36 +00:00
}