Browse Source

feat: add a helper to check whether an asset can be rendered on a web
page

Lukasz Badura 3 months ago
parent
commit
1fadcf7409
2 changed files with 11 additions and 0 deletions
  1. 5 0
      lib/fotos/asset.rb
  2. 6 0
      test/test_asset.rb

+ 5 - 0
lib/fotos/asset.rb

@@ -3,6 +3,7 @@ require 'vips'
 module Fotos
   class Asset
     THUMBNAIL_PREFIX = "TH_"
+    WEB_TYPES = ['.JPEG', '.JPG', '.GIF']
     SUPPORTED_TYPES = ['.JPEG', '.JPG', '.HEIC']
 
     def initialize(file_path)
@@ -43,6 +44,10 @@ module Fotos
       file_name.match?(THUMBNAIL_PREFIX)
     end
 
+    def web_supported?
+      WEB_TYPES.include?(ext_name.upcase)
+    end
+
     def image
       @image ||= Vips::Image.new_from_file(@file_path)
     end

+ 6 - 0
test/test_asset.rb

@@ -40,6 +40,12 @@ class TestAsset < Minitest::Test
     refute @gif_asset.supported?
   end
 
+  def test_web_supported
+    assert @jpg_asset.web_supported?
+    refute @heic_asset.web_supported?
+    assert @gif_asset.web_supported?
+  end
+
   def test_thumbnail_name_adds_prefix
     assert_equal "TH_cockapoo.jpg", @jpg_asset.thumbnail_name
     assert_equal "TH_mammoth.heic", @heic_asset.thumbnail_name