Uploading Images Directly to Cloudinary - ruby-on-rails

I'm trying to upload images directly to Cloudinary from my Rails app, since I'm on Heroku and can't use the server as an intermediary. They have support for this using jQuery, but when I follow their directions, I get the following error jquery.fileupload-fp.js:
Uncaught TypeError: Cannot read property 'fileupload' of undefined.
$.widget('blueimp.fileupload', $.blueimp.fileupload, {
Cloudinary replied with an email saying that I should include the js files in the correct order, but they are all included and in the right order. I asked them again, but they might take a while to get back to me and this is somewhat time sensitive.
Thanks!

Related

How to tweet a jpeg/mp4 with the twitter gem in Rails through ActiveStorage S3 uploads?

I am trying to Tweet from my Rails App (7.0.1), Ruby (3.0.2). I am using the "twitter" gem and others to connect the Twitter account and publish to Twitter. Everything works fine as long as I am Tweeting text only. The Text is from "Model.content".
There is another "Model.media" that is an attachment with "has_one_attached" which I want to use for the media upload to Twitter. It is stored in AWS S3.
This is the how to Tweet text using the gem (just "content" and not "Model.content" because I am tweeting from the Model.rb itself):
client.update(content)
It works and posts to Twitter fine.
To Tweet with media I can do it like that this according to the gem:
client.update_with_media(content, media)
The errors I am getting from the Rails logs are:
/Users/~/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activesupport-7.0.1/lib/active_support/core_ext/module/delegation.rb:307:in `method_missing': undefined method `merge' for #<ActiveStorage::Attached::One:0x00007fca71579048 #name="media", #record=#<...">> (NoMethodError)
If I try this:
client.update_with_media(content, Rails.application.routes.url_helpers.rails_blob_path(media, only_path: true))
The errors I am getting from the Rails logs are (same happens for .jpeg):
/Users/~/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/twitter-7.0.0/lib/twitter/rest/upload_utils.rb:40:in `append_media': undefined method `eof?' for "/rails/active_storage/blobs/redirect/....mp4":String (NoMethodError)
I tried and looked for many solutions, they weren't specifically for this issue if I understand correctly. I feel I am missing a really small thing.
What is the best approach for this issue and how?
Links:
Gem link: https://rubygems.org/gems/twitter
Gem quick guide: https://github.com/sferik/twitter/blob/master/examples/Update.md#update
According to the docs, media must be either a File or an array of Files. This method doesn't support direct uploads so you have to download the file from S3 first and then upload it to twitter.
rails_blob_path returns a string and that's why it doesn't work. Also, this path is relative to your application domain, so you can't directly use it to reference files on your filesystem.
ActiveStorage has an open method that yields a Tempfile. Fortunately they behave just like regular Files so you should be able to pass it to update_with_media. You can use it like so:
media.open do |file|
client.update_with_media(content, file)
end
It will create a temporary file on the filesystem.
Alternatively, if your file is relatively small (which might not be the case with videos) you can use the download method. It will download the file and store it in memory:
client.update_with_media(content, media.download)
Side note: POST update_with_media is deprecated and shouldn't be used anymore.

Rails carrierwave and cloudinary multiple file uploads

So I'm trying to get carrierwave to work with cloudinary for multiple file uploads but it keeps giving me this error that says:
undefined method `all_versions_processors' for Array
I followed the carrierwave documentation where I added a listing_images attribute to my Listings table which is of type json.
I also set the multiple to true option in the form file input.
And in my ListingsController I have specified as one of the permitted params the following:
listing_images: []
I'm pretty sure everything is configured properly but I can't figure out why this error is thrown. Any help would be greatly appreciated.
It's on the road-map to officially support multiple uploads with Carrierwave on Cloudinary's GEM. In the meantime, as a workaround, you can accomplish multiple uploads a bit differently. Here's a basic sample project that demonstrates it:
https://github.com/taragano/Cloudinary_multiple_uploads

Paperclip: Validate/process attachment before upload

Paperclip offers nice validator methods like
validates :image, attachment_size: { in 0..2.megabytes }
My problem is that attachment files get uploaded to S3 even though the validators would add errors to the attachment hosting object. So if the image is too big it's getting uploaded and the ActiveRecord-Object is getting errors on it when validating. That's okay but for my situation it would be more clean to reject uploads that are too big.
Is there a way to tap into the process and prevent a file from being uploaded to S3 under certain conditions?
Currently my implementation cares for the errors and deletes the attachment afterwards if the hosting object is not valid.
The described situation refers to Rails 4.0 application using Ruby 2.0.
The described problem does not occur in more recent Paperclip versions (most recent version at the time I'm writing this: 4.2). Files won't be uploaded to S3 when validations have attached errors to the AR-Object then.

Rails' Paperclip gem POSTing instead of PUTting when uploading .zip file

I've got a form (Rails 3.2.8, Paperclip 3.1.4) with two Paperclip attachments for a model with two has_attached_files. One is meant to be an image, the other a generic file, usually a .zip file.
Everything works fine so long as I don't try to upload a .zip file. Uploading a .zip file of any size (original was 80 MB but tried 3 MB to see if it was a size issue) causes the form to POST instead of PUT and Rails throws a routing error.
The form method is POST but has the Rails' hidden _method value set to 'put', which works fine and does cause a PUT when I'm not trying to upload .zip files.
The form does have the enctype 'multipart' bit set correctly.
Any idea what could be causing this?
The file sounds large. Double check that the actual params are making it into the request. I get this on local as well depending on the size of the files.
The effect I've seen is that rails would basically get no params. Since a PUT is actually a post with a hidden element, rails would see only the POST since params are dropped.
I am actually not sure what is causing this. I think it may be the local webserver, so you may need to configure nginx or something. This never happens to me on heroku or anything, but always on local if the file is big enough.
Also note, webrick has a really really small size of the request payload limitation. So don't use that. Use "thin" as it is a really easy replacement.

Creating PDF from photos on a Facebook Rails app

I'm creating a Facebook app using Rails and hosted on Heroku and I'm having a bit of trouble finding the ideal way to solve a problem. I want the user to be able to move their photos around on the screen, position them, and then download that as either a PDF or a PNG file to be emailed or printed. I have the app getting the user's facebook photos and they can drag them on to a HTML5 Canvas element to position them. Taking this canvas, however, and converting it into something printable is where I'm hitting a dead end.
Basically I have a few ideas I have tried:
Convert the Canvas to a PNG using toDataURL() - Would work perfectly but since the photos are external, the canvas is "dirty" and will throw up a security issue. I've thought about trying to copy the pixels of each image to a pixel array but I've heard this may not work either due to security issues. Since the app is dealing with people's facebook images I really don't want to store them on the app's server.
Use PDFKit/wkhtmltopdf to create a PDF using Rails - I've tried this, but since the app is a Sinatra app (I think), it's confusing me a lot. It's throwing errors with the to_pdf call saying 'Command Failed'. I've tried adding a config.middleware.use line but I'm not 100% sure where to put it and everywhere seems to be failing saying "config" is an undefined variable. Also installing wkhtmltopdf seems to fail on Heroku once I test it outside localhost.
Use Prawn to create a PDF using Rails - I've tried prawn but it seems to have a similar problem to PDFKit and I get confused about what goes where on a Sinatra app. I'm sure I read as well that people were having problems with it too.
Have I missed any obvious solutions to this, or is there something I'm not thinking of? All I want to do is create some kind of easily printable file with positioning intact that can be easily downloaded and printed by the user but I've come across so many problems that I don't know where to go next and I'm going round in circles.
If anyone had any advice for me about how I could get around this problem I'd really appreciate it.
if prawn is giving you grief just use one of the jquery plugins to print your div content. You could even configure a pdf printer and print the document instead of hard copy if you so wish/need images in pdf format.
I use http://archive.plugins.jquery.com/project/jqPrint plugin in one of my projects and it works like a charm.
It sounds like many of your issues relate to the necessary PDF binaries not being accessible on Heroku. In following with the twelve factor approach to dependency isolation Heroku purposely provides a very bare system.
If you need to run a custom binary on Heroku I'd suggest looking at a tool called Vulcan which can compile a binary that's compatible with the Heroku runtime.

Resources