2020-06-06 07:44:36 +00:00
|
|
|
package httputil
|
|
|
|
|
|
|
|
import (
|
2020-06-13 07:29:32 +00:00
|
|
|
"context"
|
2020-06-06 07:44:36 +00:00
|
|
|
"io"
|
2020-12-30 06:30:41 +00:00
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
2020-06-06 07:44:36 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/gts"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/log"
|
2020-12-20 09:48:52 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2020-07-18 07:16:47 +00:00
|
|
|
// TODO:
|
|
|
|
|
2020-06-07 04:27:28 +00:00
|
|
|
type ImageContainer interface {
|
2020-12-20 09:48:52 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-30 06:30:41 +00:00
|
|
|
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.
|
2020-12-20 09:48:52 +00:00
|
|
|
func AsyncImage(ctx context.Context,
|
2020-12-30 06:30:41 +00:00
|
|
|
img ImageContainer, imageURL string, procs ...imgutil.Processor) {
|
2020-07-11 06:48:44 +00:00
|
|
|
|
2020-12-30 06:30:41 +00:00
|
|
|
if imageURL == "" {
|
2020-06-13 07:29:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-30 06:30:41 +00:00
|
|
|
w, h := img.GetSizeRequest()
|
2020-12-25 08:32:08 +00:00
|
|
|
scale := 1
|
|
|
|
|
|
|
|
surfaceContainer, canSurface := img.(SurfaceContainer)
|
2020-12-30 06:30:41 +00:00
|
|
|
if canSurface {
|
|
|
|
scale = surfaceContainer.GetScaleFactor()
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
ctx := primitives.HandleDestroyCtx(ctx, img)
|
2020-12-25 08:32:08 +00:00
|
|
|
|
2020-12-30 06:30:41 +00:00
|
|
|
// 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
|
|
|
}
|
2020-12-30 06:30:41 +00:00
|
|
|
defer r.Body.Close()
|
2020-12-25 08:32:08 +00:00
|
|
|
|
2020-12-30 06:30:41 +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
|
|
|
|
2020-12-30 06:30:41 +00:00
|
|
|
_, fileType := path.Split(mimeType) // abuse split "a/b" to get b
|
2020-06-17 07:06:34 +00:00
|
|
|
|
2020-12-30 06:30:41 +00:00
|
|
|
isGIF := fileType == "gif"
|
|
|
|
if isGIF {
|
|
|
|
canSurface = false
|
|
|
|
scale = 1
|
2020-12-25 08:32:08 +00:00
|
|
|
}
|
2020-06-17 07:06:34 +00:00
|
|
|
|
2020-12-30 06:30:41 +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 {
|
|
|
|
log.Error(errors.Wrap(err, "failed to make pixbuf loader"))
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2020-06-17 07:06:34 +00:00
|
|
|
|
2020-12-30 06:30:41 +00:00
|
|
|
load := loadFn(ctx, img, isGIF)
|
|
|
|
l.Connect("area-prepared", load)
|
|
|
|
l.Connect("area-updated", load)
|
|
|
|
|
|
|
|
if err := downloadImage(r.Body, l, procs, isGIF); err != nil {
|
|
|
|
log.Error(errors.Wrapf(err, "failed to download %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
|
|
|
}
|
|
|
|
|
2020-12-30 06:30:41 +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) {
|
2020-12-30 06:30:41 +00:00
|
|
|
if pixbuf == nil {
|
|
|
|
if !isGIF {
|
|
|
|
pixbuf, _ = l.GetPixbuf()
|
|
|
|
} else {
|
|
|
|
pixbuf, _ = l.GetAnimation()
|
2020-06-07 04:27:28 +00:00
|
|
|
}
|
2020-12-30 06:30:41 +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
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
func execIfCtx(ctx context.Context, fn func()) {
|
2020-12-30 06:30:41 +00:00
|
|
|
gts.ExecLater(func() {
|
2020-06-13 07:29:32 +00:00
|
|
|
if ctx.Err() == nil {
|
|
|
|
fn()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-30 06:30:41 +00:00
|
|
|
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.
|
2020-06-17 07:06:34 +00:00
|
|
|
if len(p) > 0 {
|
2020-12-30 06:30:41 +00:00
|
|
|
if !isGIF {
|
|
|
|
err = imgutil.ProcessStream(dst, src, p)
|
2020-06-06 07:44:36 +00:00
|
|
|
} else {
|
2020-12-30 06:30:41 +00:00
|
|
|
err = imgutil.ProcessAnimationStream(dst, src, p)
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Else, directly copy the body over.
|
2020-12-30 06:30:41 +00:00
|
|
|
_, err = io.Copy(dst, src)
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2020-12-30 06:30:41 +00:00
|
|
|
return errors.Wrap(err, "failed to process image")
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
2020-12-30 06:30:41 +00:00
|
|
|
|
|
|
|
return nil
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|