Commit f85e52c7

mo khan <mo@mokhan.ca>
2014-06-08 03:12:36
raise error when the file extension is not in the whitelist.
1 parent 0952415
Changed files (2)
app
services
infrastructure
spec
services
infrastructure
app/services/infrastructure/image.rb
@@ -4,6 +4,7 @@ class Image
   def initialize(path, exif = ExifParser.new)
     @path = path
     @exif = exif
+    ensure_in_whitelist!(@path)
   end
 
   def filename
@@ -85,4 +86,10 @@ class Image
   def sanitize_regexp
     /[^a-zA-Z0-9\.\-\+_]/
   end
+
+  def ensure_in_whitelist!(path)
+    unless %w(.jpg .jpeg .gif .png .bmp .tif).include?(File.extname(path))
+      raise StandardError.new("This file is not in the whitelist. #{path}")
+    end
+  end
 end
spec/services/infrastructure/image_spec.rb
@@ -52,4 +52,8 @@ describe Image do
       expect(Image.new('blah.tif').content_type).to eql('image/tiff')
     end
   end
+
+  it "raises an errorwhen the file is not in the whitelist" do
+    expect(-> { Image.new('blah.exe') }).to raise_error
+  end
 end