From b2814c1125fb1097af49ac46e0c9a47997d6e6c8 Mon Sep 17 00:00:00 2001 From: lllusion3469 <31420484+lllusion3469@users.noreply.github.com> Date: Mon, 25 May 2020 18:43:03 +0200 Subject: [PATCH 1/2] uploads: fix getting dimensions of flash files MediaFile#dimensions is called twice - in #width and in #height but it only works on the first call because the file is read to the end and consumed the first time so when #read is called the second time it only returns the empty string --- app/logical/media_file/flash.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/logical/media_file/flash.rb b/app/logical/media_file/flash.rb index be0f8c829..f7e4c8c5c 100644 --- a/app/logical/media_file/flash.rb +++ b/app/logical/media_file/flash.rb @@ -5,7 +5,7 @@ class MediaFile::Flash < MediaFile def dimensions # Read the entire stream into memory because the # dimensions aren't stored in a standard location - contents = file.read.force_encoding("ASCII-8BIT") + contents = File.read(file.path, binmode: true).force_encoding("ASCII-8BIT") # Our 'signature' is the first 3 bytes # Either FWS or CWS. CWS indicates compression From 5c2ecee60ff09eadcc70493aa6d6f36026823a3a Mon Sep 17 00:00:00 2001 From: lllusion3469 <31420484+lllusion3469@users.noreply.github.com> Date: Mon, 25 May 2020 18:58:48 +0200 Subject: [PATCH 2/2] uploads: memoize dimensions of flash files flash files can be quite big (the biggest on danbooru.donmai.us being 68.6MB atm). Reading it and applying complex transformations twice seems unnecessary. --- app/logical/media_file/flash.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/logical/media_file/flash.rb b/app/logical/media_file/flash.rb index f7e4c8c5c..c734936f3 100644 --- a/app/logical/media_file/flash.rb +++ b/app/logical/media_file/flash.rb @@ -54,4 +54,5 @@ class MediaFile::Flash < MediaFile [width, height] end + memoize :dimensions end