Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make FileFilter check more generic so ActionDispatch::Http::UploadedFile is accepted #236

Closed
wants to merge 1 commit into from

Conversation

conf
Copy link
Contributor

@conf conf commented Dec 3, 2014

Hello!

I stumbled with this bug when I tried to use active_interaction and CarrierWave together. The bug consists in the following:
Since actionpack's ActionDispatch::Http::UploadedFile responds to tempfle, CarrierWave (inside ActiveRecord model) receives Tempfile instance and not its wrapper ActionDispatch::Http::UploadedFile, losing this way access to extra variables like original_file_name, content_type and etc. This breaks some Carrierwave validations and ends up with saving file with a generic temporary name like rack20141203-27678-2wbop without extension which is not good.
So I made this check more generic according the fact that all IO-like classes respond to close method.
I couldn't write more explicit specs to test this behavior without adding CarrierWave or actionpack as a dependency. If you're not satisfied with changes in this pull request, I'll be happy to work on it together.
Cheers!

@tfausak
Copy link
Collaborator

tfausak commented Dec 3, 2014

We considered supporting any IO before. Check out issue #67. This might be a good motivation to do that.

I have not heard of CarrierWave before. Since it's a gem that we don't claim to support, I would argue that this isn't a bug per se.

@conf
Copy link
Contributor Author

conf commented Dec 3, 2014

Well, it's quite popular: it seconds Paperclip, and, IMO much flexible than it.
Generally speaking, Paperclip will be broken too, because underlying mechanism works similarly so it won't be able to save uploaded file with original filename and will resort to Tempfile's name instead.
So, what do you propose? Doesn't this PR solve #67, does it?
Since every IO object responds to close, it would be treated as a file object. I would argue against explicit IO class check since it will break wrappers like ActionDispatch::Http::UploadedFile.
P.S. Don't know how to fix the build, it failed very strangely. Any ideas?

@tfausak
Copy link
Collaborator

tfausak commented Dec 3, 2014

Yes, this would fix #67.

I would prefer to check for #to_io instead of #close.

@conf
Copy link
Contributor Author

conf commented Dec 3, 2014

Well, ActionDispatch::Http::UploadedFile#to_io is only implemented in edge version of rails (4.2.0beta, still unreleased) and in all released versions of rails ActionDispatch::Http::UploadedFile will fail this check.

@tfausak
Copy link
Collaborator

tfausak commented Dec 4, 2014

Oh, I didn't realize that method was so new. I figured that it's been around since Ruby 1.8.6 at least, so any IO-ish object would probably implement it. Looks like I was wrong.

This pull request is effectively making the file filter a shortcut for the interface filter with some methods preset. It might be better to implement it that way.

@conf conf force-pushed the fix-carrierwave-integration branch from b1cb20d to 025c21b Compare December 9, 2014 05:57
@conf
Copy link
Contributor Author

conf commented Dec 9, 2014

Hi, reimplemented FileFilter via InterfaceFilter as you suggested. Any thoughts?

super
end
def methods
super + [:close]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should not call super. If it does, you could do this:

file :whatever,
  methods: [:foobar]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

@tfausak
Copy link
Collaborator

tfausak commented Dec 9, 2014

Thank you! I think it looks a lot better. We should see what @AaronLasseigne thinks.

@AaronLasseigne
Copy link
Owner

I'm a little concerned about using close as the method to check. It feels too wide open to me. I could see non-file objects implementing that method for other purposes. If we're going to use a method to check I think eof? might be better. It's more file specific and less likely to be implemented elsewhere. Additionally, we could specifically check for File, Tempfile, or ActionDispatch::Http::UploadedFile. Thoughts?

Also, thanks @conf for the PR!

@AaronLasseigne
Copy link
Owner

I should also note that we originally pulled the tempfile so that you were guaranteed an object that responded to all the File methods. Accepting ActionDispatch::Http::UploadedFile would make the thing you get from a file filter less consistent (i.e. polymorphic). That might be a trade worth making though for the purposes of practicality.

@conf conf force-pushed the fix-carrierwave-integration branch from 025c21b to 4e9a7be Compare December 9, 2014 19:02
@conf
Copy link
Contributor Author

conf commented Dec 9, 2014

@AaronLasseigne Ok, changed check to .eof?.
I don't think we should concern a lot about strict guarantees of File objects. The main purpose of active_interaction's filters is validation of user-supplied data and in this context clear separation of strings, nils and IO-like objects is enough. Actually, this pull-request has been born because of too strict check than was needed.
Thank you for your time and for great gem!

@conf conf force-pushed the fix-carrierwave-integration branch from 4e9a7be to 11dcaff Compare December 9, 2014 19:34
@tfausak
Copy link
Collaborator

tfausak commented Dec 9, 2014

👍

This is a breaking change to that API we provide for file inputs, so it'll have to be part of v2.0.0.

@tfausak tfausak added this to the v2.0.0 milestone Dec 9, 2014
@tfausak tfausak self-assigned this Dec 9, 2014
@conf
Copy link
Contributor Author

conf commented Dec 10, 2014

So, when this is going to be merged? Should I rebase it against some other branch or smth?

@tfausak
Copy link
Collaborator

tfausak commented Dec 10, 2014

Yes, could you please open a new pull request against the v2.0.0 branch?

@conf
Copy link
Contributor Author

conf commented Dec 10, 2014

Sure: #243

@conf conf closed this Dec 10, 2014
@conf conf deleted the fix-carrierwave-integration branch December 10, 2014 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants