diff options
author | 2024-06-26 15:01:16 +0000 | |
---|---|---|
committer | 2024-06-26 16:01:16 +0100 | |
commit | 21bb324156f582e918a097ea744e52fc21b2ddf4 (patch) | |
tree | 50db5cfd42e26224591f59ff62de14a3715677b5 /internal/media/image.go | |
parent | [docs] restructure federation section (#3038) (diff) | |
download | gotosocial-21bb324156f582e918a097ea744e52fc21b2ddf4.tar.xz |
[chore] media and emoji refactoring (#3000)
* start updating media manager interface ready for storing attachments / emoji right away
* store emoji and media as uncached immediately, then (re-)cache on Processing{}.Load()
* remove now unused media workers
* fix tests and issues
* fix another test!
* fix emoji activitypub uri setting behaviour, fix remainder of test compilation issues
* fix more tests
* fix (most of) remaining tests, add debouncing to repeatedly failing media / emojis
* whoops, rebase issue
* remove kim's whacky experiments
* do some reshuffling, ensure emoji uri gets set
* ensure marked as not cached on cleanup
* tweaks to media / emoji processing to handle context canceled better
* ensure newly fetched emojis actually get set in returned slice
* use different varnames to be a bit more obvious
* move emoji refresh rate limiting to dereferencer
* add exported dereferencer functions for remote media, use these for recaching in processor
* add check for nil attachment in updateAttachment()
* remove unused emoji and media fields + columns
* see previous commit
* fix old migrations expecting image_updated_at to exists (from copies of old models)
* remove freshness checking code (seems to be broken...)
* fix error arg causing nil ptr exception
* finish documentating functions with comments, slight tweaks to media / emoji deref error logic
* remove some extra unneeded boolean checking
* finish writing documentation (code comments) for exported media manager methods
* undo changes to migration snapshot gtsmodels, updated failing migration to have its own snapshot
* move doesColumnExist() to util.go in migrations package
Diffstat (limited to 'internal/media/image.go')
-rw-r--r-- | internal/media/image.go | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/internal/media/image.go b/internal/media/image.go index 29527c085..8a34e5062 100644 --- a/internal/media/image.go +++ b/internal/media/image.go @@ -43,12 +43,9 @@ var ( BufferPool: &pngEncoderBufferPool{}, } - // jpegBufferPool is a memory pool of byte buffers for JPEG encoding. - jpegBufferPool = sync.Pool{ - New: func() any { - return bufio.NewWriter(nil) - }, - } + // jpegBufferPool is a memory pool + // of byte buffers for JPEG encoding. + jpegBufferPool sync.Pool ) // gtsImage is a thin wrapper around the standard library image @@ -80,25 +77,29 @@ func decodeImage(r io.Reader, opts ...imaging.DecodeOption) (*gtsImage, error) { } // Width returns the image width in pixels. -func (m *gtsImage) Width() uint32 { - return uint32(m.image.Bounds().Size().X) +func (m *gtsImage) Width() int { + return m.image.Bounds().Size().X } // Height returns the image height in pixels. -func (m *gtsImage) Height() uint32 { - return uint32(m.image.Bounds().Size().Y) +func (m *gtsImage) Height() int { + return m.image.Bounds().Size().Y } // Size returns the total number of image pixels. -func (m *gtsImage) Size() uint64 { - return uint64(m.image.Bounds().Size().X) * - uint64(m.image.Bounds().Size().Y) +func (m *gtsImage) Size() int { + return m.image.Bounds().Size().X * + m.image.Bounds().Size().Y } // AspectRatio returns the image ratio of width:height. func (m *gtsImage) AspectRatio() float32 { - return float32(m.image.Bounds().Size().X) / - float32(m.image.Bounds().Size().Y) + + // note: we cast bounds to float64 to prevent truncation + // and only at the end aspect ratio do we cast to float32 + // (as the sizes are likely to be much larger than ratio). + return float32(float64(m.image.Bounds().Size().X) / + float64(m.image.Bounds().Size().Y)) } // Thumbnail returns a small sized copy of gtsImage{}, limited to 512x512 if not small enough. @@ -160,7 +161,11 @@ func (m *gtsImage) ToPNG() io.Reader { // getJPEGBuffer fetches a reset JPEG encoding buffer from global JPEG buffer pool. func getJPEGBuffer(w io.Writer) *bufio.Writer { - buf, _ := jpegBufferPool.Get().(*bufio.Writer) + v := jpegBufferPool.Get() + if v == nil { + v = bufio.NewWriter(nil) + } + buf := v.(*bufio.Writer) buf.Reset(w) return buf } |