diff --git a/lib/rails_ext/active_storage_blob_previewable.rb b/lib/rails_ext/active_storage_blob_previewable.rb new file mode 100644 index 000000000..70755896e --- /dev/null +++ b/lib/rails_ext/active_storage_blob_previewable.rb @@ -0,0 +1,18 @@ +# +# see https://fizzy.37signals.com/5986089/collections/7/cards/1302 +# +# Large previewable uploads may take longer than the "pin reads" interval. So we pick a small-ish +# number and turn off previews for anything larger, at least until we can come up with a permanent +# solution. +# +module ActiveStorageBlobPreviewable + MAX_PREVIEWABLE_SIZE = 16.megabytes # this is pretty arbitrary, feel free to adjust + + def previewable? + super && self.byte_size <= MAX_PREVIEWABLE_SIZE + end +end + +ActiveSupport.on_load :active_storage_blob do + prepend ::ActiveStorageBlobPreviewable +end diff --git a/test/lib/rails_ext/active_storage_blob_previewable_test.rb b/test/lib/rails_ext/active_storage_blob_previewable_test.rb new file mode 100644 index 000000000..290cc3cf9 --- /dev/null +++ b/test/lib/rails_ext/active_storage_blob_previewable_test.rb @@ -0,0 +1,13 @@ +require "test_helper" + +class ActiveStorageBlobPreviewableTest < ActiveSupport::TestCase + test "small files are previewable" do + blob = ActiveStorage::Blob.new(filename: "test.mp4", content_type: "video/mp4", byte_size: 1024 * 1024, key: "test", checksum: "abc") + assert blob.previewable? + end + + test "large files are not previewable" do + blob = ActiveStorage::Blob.new(filename: "test.mp4", content_type: "video/mp4", byte_size: ActiveStorageBlobPreviewable::MAX_PREVIEWABLE_SIZE + 1, key: "test", checksum: "abc") + assert_not blob.previewable? + end +end