after_photo_post_process :post_process_photo
def post_process_photo
img = EXIFR::JPEG.new(photo.queued_for_write[:original].path) # error on this line
return unless img
self.width = img.width
self.height = img.height
self.model = img.model
end
I am using a ruby gem called EXIFR that extracts the EXIF data from JPEG files. The EXIF data开发者_如何学Python is simply technical data about the picture. So my code works fine when I upload a JPEG, however any other image type causes it to break.
EXIFR::MalformedJPEG in ImagesController#create
no start of image marker found
I assumed that the return statement would allow this to work if nothing gets assigned to the img variable, but that looks like it is not the case.
You could rescue the error and return something else.
def post_process_photo
begin
img = EXIFR::JPEG.new(photo.queued_for_write[:original].path) # error on this line
self.width = img.width
self.height = img.height
self.model = img.model
rescue EXIFR::MalformedJPEG
return nil
end
end
精彩评论