Senthil Kumar Bhaskaran
Senthil Kumar Bhaskaran

Reputation: 7451

How to validate corrupt image file while uploading using Rails

How to validate corrupt image file while uploading using Rails

Upvotes: 1

Views: 1686

Answers (2)

ryanb
ryanb

Reputation: 16287

I second Mike Trpcic's suggestion on using Paperclip, I have a screencast on the topic.

If you are using that and doing some graphic conversion (such as making thumbnails) you can look for PaperclipCommandLineError exception. I believe that is triggered if the conversion fails which would be the case if ImageMagick was not able to read the graphic (due to corruption or incorrect type).

If you rescue Paperclip::PaperclipCommandLineError you can present a nice error to the user.

Upvotes: 2

Mike Trpcic
Mike Trpcic

Reputation: 25659

You can check the MIME type to make sure that it's not file with a changed extension by using the mime-types library.

type = MIME::Types.type_for(your_file.original_filename).first

I'm not sure how that would work with corrupted/broken files though. Make sure to look into Paperclip for uploading files, as it's ridiculously easy to set up and use.

Upvotes: 1

Related Questions