Skip to content

Commit

Permalink
sreeni: verify photo uploads based on content type instead of file ex…
Browse files Browse the repository at this point in the history
…tesion as it breaks on photos uploaded from blackberry
  • Loading branch information
Sreenivas Ananthakrishna committed May 17, 2011
1 parent 6e596dd commit 461733c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
11 changes: 5 additions & 6 deletions app/models/child.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Child < CouchRestRails::Document
}
}"

validates_with_method :validate_photo_file_name
validates_with_method :validate_photos
validates_with_method :validate_audio_file_name
validates_fields_of_type Field::NUMERIC_FIELD
validates_fields_of_type Field::TEXT_FIELD
Expand Down Expand Up @@ -54,8 +54,8 @@ def validate_age
[false, "Age must be between 1 and 99"]
end

def validate_photo_file_name
return true if @file_names.blank? || @file_names.all?{|f| f =~ /([^\s]+(\.(?i)(jpg|jpeg|png))$)/ }
def validate_photos
return true if @photos.blank? || @photos.all?{|photo| photo.content_type =~ /(jpg|jpeg|png)/ }
[false, "Please upload a valid photo file (jpg or png) for this child record"]
end

Expand Down Expand Up @@ -145,10 +145,9 @@ def photo=(photo_file)
unless photo_file.is_a? Hash
photo_file = {'0' => photo_file}
end

@file_names = []
@photos = []
@photo_keys = photo_file.values.select {|photo| photo.respond_to? :content_type}.collect do |photo|
@file_names << photo.original_path
@photos << photo
attachment = FileAttachment.from_uploadable_file(photo, "photo-#{photo.path.hash}")
attach(attachment)
attachment.name
Expand Down
Binary file added features/resources/jorge_jpg
Binary file not shown.
11 changes: 7 additions & 4 deletions spec/models/child_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@
child.save.should == true
end

it "should save file based on mime type" do
child = Child.new
photo = uploadble_jpg_photo_without_file_extension
child.photo = photo
child.save.should == true
end

it "should disallow file formats that are not supported audio formats" do
child = Child.new

Expand Down Expand Up @@ -488,8 +495,6 @@
@child.audio= uploaded_file
end

it "should attach the created FileAttachment to the document with a key the same as the file name"

it "should store the audio attachment key with the 'original' key in the audio hash" do
FileAttachment.stub!(:from_uploadable_file).and_return(@file_attachment)
@child.audio= uploadable_audio
Expand Down Expand Up @@ -524,8 +529,6 @@
child.add_audio_file(@file, "audio/mpeg")
end

it "should attach the file to the child record with the name of the attachment as the key"

it "should add attachments key attachment to the audio hash using the content's media type as key" do
child = Child.new()
FileAttachment.stub!(:from_file).and_return(@file_attachment)
Expand Down
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ def uploadable_audio_ogg
uploadable_audio "features/resources/sample.ogg"
end

def uploadble_jpg_photo_without_file_extension
returning(uploadable_photo "features/resources/jorge_jpg") do |photo|
def photo.content_type
"image/jpg"
end
end
end


def find_child_by_name child_name
child = Summary.by_name(:key => child_name)
Expand Down

0 comments on commit 461733c

Please sign in to comment.