Ask A Question

Notifications

You’re not receiving notifications from this thread.

File Uploads with Refile Discussion

Discussion for File Uploads with Refile

Awesome.

Reply

Thank you for teaching the new Refile gem. The information shared in the Pro episode for upload progress is worth the subscription to Go Rails. In thirteen minutes, you communicated what has taken hours of work to implement with other file upload gems.

Question: With the refile gem, what is your recommendation for uploading multiple files in a single form, each with progress indicator? There is some discussion at https://github.com/elabs/re.... Perhaps another episode is in order?

Reply

Thanks Stan! :)

I believe with multiple files (at least for separate things) you just say

class User
attachment :photo
attachment :resume
end

You may need to modify the Javascript somewhat so that it can tell the difference between the two. That likely is worth doing another episode on, some form of refactoring Javascript to handle this better.

The thing they don't handle right now is uploading multiple files (like User has_many :photos) but that should be coming soon. At least after reading that thread, it seems it should be easy for someone to implement it.

Reply

It is Refile good for uploading video files? I need upload video files (not host them on youtube, vimeo, etc.) and maybe do some post production (format convert/generate, add watermark...).

Reply

You can upload anything with it. You'll want to check out https://zencoder.com/en/ for transcoding. If you set Refile to upload to S3, zencoder can take it from there. https://github.com/zencoder...

Reply

If you are using the built in support for S3, it appears that you set that up in the initializer for refile:

# config/initializers/refile.rb

But what if you only want to use S3 in production, and the filesystem in dev and test? Is there a way to move this config to the environment level? For example, with Paperclip, the config to use AWS is here when you only want it in production:

# config/production.rb

Reply

Chris, you can just setup an environment conditional in your initializer:


if Rails.env.production?
# Use S3 Backend
else
# Use FileSystem Backend
end

Reply
Nick Chernyshev Nick Chernyshev

Chris,
Rails 4.1 comes with pretty secrets feature so you can easily use it like this:


# config/initializers/refile.rb

aws = {
access_key_id: Rails.application.secrets.aws_api_key,
secret_access_key: Rails.application.secrets.aws_secret,
bucket: Rails.application.secrets.aws_bucket,
}

and then set different config options for different environments:


# config/secrets.yml
development:
aws_api_key: ...
aws_secret: ...
aws_bucket: ...

production:
aws_api_key: ...
aws_secret: ...
aws_bucket: ...

Reply

Looking through the documentation it does not seem like there is any way to limit or throttle the reformatting/re-streaming of assets. I understand you say use CDN, but I do not see any method of stopping malicious users of hard spinning the asset urls with different pixel sizes for example to DoS the server... Any ideas or am I right in seeing this as a baseline flaw of this gem?

Reply

Definitely a worthy discussion to be had with the author of the gem.

Reply
Anthony Candaele Anthony Candaele

very cool gem indeed. Just implemented it to my new Rals app. Thanks for the very instructive video Chris!
I also need to implement document uploads (.pdf, .doc, .txt, etc ...) Can this also be done with Refile?

greetings,

Anthony

Reply

Yep, shouldn't be a problem. You'll just skip the image processing parts.

Reply

Can you use this to upload a CSV file and have each row render as a new object? What do you think is the best way to do this?

Reply

CSV upload is definitely a topic I need to cover soon. I usually don't save the CSV files so I traditionally just use a file_field and let the file get deleted after the request is done in most cases.

Refile could work for storage of the CSVs though and then the model could access the file and parse it as necessary if you want to store the CSVs permanently.

Reply

Can you use this gem to upload multiple files at the same time?

Reply

Not at the same exact time just yet, but that's on his todo list to add as a feature. https://github.com/elabs/re...

Reply

Has anyone made a rake task to populate the database with images? I went through the documentation and didn't see how this could be done. Thanks!

Reply

I get this error when trying to use S3 backend:
/home/gabriel/.rvm/gems/ruby-2.1.5/gems/refile-0.5.3/lib/refile/backend/s3.rb:37:in `initialize': uninitialized constant Refile::Backend::S3::AWS (NameError)

Any ideas?

Reply

This is because Refile uses the 1.x version of the aws gem. You'll need to update your gemfile like this:

gem "aws-sdk", '< 2'

Now you'll be using the same version as Refile. The 2.x version of the aws-sdk gem uses Aws for name spacing instead of AWS so that you can use both versions of the gem in the same application.

Reply

I want to try and use Refile in my rails API app. Can I just attach a base64 encoded image in the post body?

Reply
andre bautista andre bautista

I'm curious about this as well, let me know if you find a solution!

Reply

Great video! Still deciding between carrierwave and refile tho since it's still new and carrierwave's wiki is filled with howtos. Btw, I think you missed out on the uploading via url part. :)

Reply

I did forget to include that. It's an awesome little feature along with deleting uploads. It definitely still is pretty new and you can do everything with Carrierwave no problem.

Reply
Daniel Borges Leal Daniel Borges Leal

Can I check de md5, to prevent repeated uploads?

Reply

That's a great question. You should ask in an Issue on the github repo for Refile.

Reply

guys, how to add more than one image?

Reply

The best way to do it right now is to to upload each file to an associated has_many model.

Reply

Hi... Nice screencast! I'm getting a "no find image error" after deploy, using Capistrano... Seems like the app can't find the image after assets precompile... should i set up a directory for shared_children, or something like that? Thanks.

Reply

How would you do authorization on uploaded files with Pundit? I have uploaded files which should be restricted to the user that uploaded them, but anyone with the url that attachment_url generates can access the file. Since Refile uses the sinatra app, there's no controller for me to call Pundit's authorize method in.

Reply

That's a great question and I don't particularly know the answer to that. I'm sure you can open an issue on Github and get a response.

Reply

I've been using Dragonfly gem (https://github.com/markevan... for a long time and I find it very similar to Refile. Has somebody compare them to have a better idea if it is worth adopting Refile?

Reply

Looks similar but I've never used Dragonfly. Unless you need some other feature, it probably isn't worth switching yet.

Reply

The problem I had with Carrierwave was that when I used my phone to upload an image, I couldn't use my Gallery to upload an image (the gallery did not pop up as one of the ways to upload the image). Instead, I had to find my images in different folders, so I had to go to My files folder-> DCIM -> select the image, which is not a very good user experience (I was using an Android by the way).

I was wondering if I use Refile on my phone, will I be able to select images from my gallery directly? Or, would it be the same UX as Carrierwave where I have to navigate into my folder (i.e. DCIM).

Reply

Unfortunately that wouldn't make a difference. That feature is controlled entirely by the browser on the device. It will work differently depending on the OS version and browser.

One thing you can try is to play with the accept and capture attributes on the file field itself and see if it will trigger different things on your Android browser. I can't find great documentation on it though, but check this out to get started. http://stackoverflow.com/qu...

Reply

Would you recommend Refile for users - posting videos? Or, would Zencoder work well with Refile??

Reply

Any uploader that submits the file to S3 will work with Zencoder. They expect that your original video file is already available on S3 so they don't have to worry about which uploader you use. You're free to go with Refile, Carrierwave, Paperclip, etc.

Reply

hi there! love these tutorials! I would love to get your opinion about Transloadit !!! ???? It seems super awesome but I was unable to populate mysql with URLs after the robots did their jobs and packaged up JSON Assemblies on the Transloadit API.....just wondering if you think this service is worth using, etc. THANKS!

Reply

This looks super cool. I haven't used it before, so I'm not quite sure how they return the urls afterwards. I have previously seen a lot of people using https://www.filepicker.com/ in the past.

I think they're definitely great and give your users a lot of flexibility as well saving you from a lot of time managing the files and processing them. Would definitely recommend it if file uploading is something you don't really need to manage or customize too deeply.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.