Best way to display non-public images in a rails view - ruby-on-rails

I am trying to find the best way to render confidential images in a view, without storing the images within the rails application flat-filesystem, as I have no idea where to place the images. I am storing the image data binary as :text in a sqlite3 database table and successfully display the images using
<% s = "data:image/png;base64,#{ActiveSupport::Base64.encode64(#my_image)}"%>
<img style = 'width:100%; height:600px' src = '<%= s %>'/>
This works for me in Firefox and Chrome, but my client cannot get the images to display. I'll find out in an hour or two what browser they are using. Client says they want the image src url to look like a relative path within a controller's folder, which seems to contradict the notion of not storing the image in the flat-file system.
I think I am missing something very small here, but I would like to know the proper way to store images and documents in an application that are not public to all users. If my question is not clear or you need information, please let me know and I will provide more information.
I have read of attachment_fu and paperclip, but they appear to allow attachment downloads, and I just need to display an image inline on a page. Any help is greatly appreciated. Thank you much in advance.

You can keep files in non-public repositories and have controllers action with send_file(path, options = {}) It allows you store files somewhere on the hard disc and keep access logic inside your controller.

Have you tried the paperclip gem? You can upload images to amazon and amazon allows you to set permissions for files...if you want to do it that way.

As Artem says, Amazon is a great way to achieve this. But if I get you right, they want to see an URL to the image directly (i.e. be able to type the source into the address-field if they want to).
You need to decide wether everyone should be able to access the image (given they know the name/path), or to have authentication, in which case I don't think a relative path is worth anything.
Can't you just have an image-folder containing all images (not accessible by URL), and a table to lookup wether userX is allowed to see imageY?

Related

How to return image from a Rails API?

Just to clarify, I am trying to return images, that I have stored using active storage on my machine, through the localhost for development testing purposes. I am not trying to render images from localhost unto the internet.
So I have a model that has a has_one_attached relation to an image, called sample.
meal.rb:
include Rails.application.routes.url_helpers
has_one_attached :sample
def get_sample_url
url_for(self.sample)
end
So I use the get_sample_url, above, to get the images. It has been returning a path and I just assumed that it was working and have built my entire API using similar code and getting similar links. But when I put the link in the browser, it doesn't return anything.
The link returned is http://localhost:3001/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--c83eba36bfec0607652b47d857104d3cf9e88009/cute%20owl.jpg
I want to know:
Is the ideal way to return images from a Rails API?
Why the link is not returning my images?
What is the ideal way to return images from a Rails API?
It depends.
If we are talking about JSON it does not actually have a binary type so in order to include an image in the JSON response itself you would need to base64 encode it. Since this increases the size of the file dramatically its something you would really only consider for simple images like bar codes or QR codes.
For most "normal" images like user avatar and the like you want to just include a URL in the json that the client can request when/if it needs the image.
If the scope of your application is small you can use file system storage. But Rails is not going to make your localhost available to the whole internet.
But if you want your app to scale use a cloud CDN that is fast and distributed. I would consider using a CDN with an existing adapter instead of Cloudinary if you want to spend time doing something else then setup.

Generate file-directory-safe string using URL?

My app is sent messages which sometimes include http links to images. When this happens, I want to cache the image locally, so I can display the image in future without needing to download it, if it exists locally.
But lets say the image url is http://imgur.com/abcdef.jpg. A file path of something like this, I'd imagine could have all sorts of issues:
/var/Applications/[application]/documents/http://imgur.com/abcdef.jpg
What I need is a way to take the given image URL and generate a file directory-friendly string, so that I can save the image using that URL every time, or check for its existence.
While there are many ways how to implement it (simpliest way would be to create MD5 of the web address, store image as [md5name.png] and then just try to check if md5 file reference exists), I suggest you use one of two libraries made for that:
https://github.com/Haneke/Haneke
https://github.com/rs/SDWebImage
They both work on url-based principles, provide you with all known implementations of caching and they handle cases like this by default. Both of them also download images in background which is recommended way to do it and have convenience methods to load images in UIImageView.
Hope it helps!

How would you upload an image URL to an external website which generates a new URL for it and retrieve the newly generated url in rails?

I am creating a website which allows users to post links to gif images stored externally. Unfortunately gifs are terribly expensive and luckily I have found the following website which compresses them quite radically: http://gfycat.com/
Currently my app stores the url to the linked image (as of yet it is uncompressed and without the use of gfycat)
What I would like to do is to take the url that the user posts, use it to compress the gif via gfycat.com and then store the newly generated url (pointing to the compressed image) in my database.
How would I do this?
Sorry for the long explanation btw
Just took a look at gfycat.com, seems like they do have an accessible webservice at http://gfycat.com/fetch/:url
If this is the case, and you already have the URL you can very easily grab the 'minified' version of the gif by grabbing the URL and saving the file.
So, I would probably do something like this:
Install carrierwave or another image upload gem of your choice
In the case you used the above gem...
assuming Cat is your model, and you have an uploader attached to it named gif_photo, and also assuming the original 'gif' url is stored as original_gif_url
You can do something like the following (assuming #cat is some instance of the cat model):
#cat.update_attribute(:remote_gif_photo_url, "http://gfycat.com/fetch/#{#cat.original_gif_url}")
as simple as that :)... to get back at the gif, you can simply call #cat.gif_photo_url
(again, assumptions for code above are using carrierwave syntax, but can easily be done with any of its alternatives.)

Where do Instagram/Facebook store images internally?

Like the title says. In an iPhone, in what directory do Instagram/Facebook (and similar applications) store the images they downloaded at runtime?
When is that directory wiped?
For non permanent images (e.g. post thumbnails but not user profile pics) I would imagine they get stored in the <Application_Home>/Library/Caches, other content probably goes in Documents
You don't need to know what facebook or instagram do, but If I understand your question right, you want to download images asynchronously from web and cache them. For that u can use a third party class to handle the caching instead of writing your own, I would suggest SDWebImage.

Issues with Filemaker IWP working with hyperlinks on images

I have a filemaker database that I need to be able to link records and all associated data (including container field data) to various points placed on a large PDF image, and then make that data appear via instant web publishing when someone clicks on the marker for that area on the PDF. For example the PDF may be an image of a car, and then I would have various close up images of issues with the car and descriptions of those images as records in the database. I would then want to drop points on the base PDF image and when you clicked on those points be able to see the close up images and other data related to those images.
I'm being told this is too much for IWP because:
I need to place the markers outside filemaker via PDF annotation
Filemaker IWP can't handle the number of markers that may be necessary (it could be up to 1,000 on an E sized image.
Does anyone have a work around or explanation why this is a problem?
If I understand correctly, you would like to setup a PDF with links that will open a browser and show data related to what was clicked. Assuming that is the case, the reason this wont work is because IWP does not provide a unique URL for a unique page. For example, here on StackOverflow you can directly link to any question based on its URL:
http://stackoverflow.com/questions/3207775/ -- this question
http://stackoverflow.com/questions/4973921/ -- some other question
IWP uses Javascript and session variables to manipulate the output to the screen, so there is no way to link to a specific section of your IWP site, since the URL is always something like:
http://yoursite.com/fmi/iwp/cgi?-db=YOUR_DB-loadframes -- Product A
http://yoursite.com/fmi/iwp/cgi?-db=YOUR_DB-loadframes -- Product B
http://yoursite.com/fmi/iwp/cgi?-db=YOUR_DB-loadframes -- Product C
Because of the limited nature of IWP, you will not be able to workaround this issue. You'll need to build your own web-interface using the Custom Web Publishing Engine, either using the built-in PHP extensions or some other technology where you invoke the XML publishing API.
I agree with Nate
IWP is the wrong solution to this problem. You'd be better off simply hosting those images on a webserver.
Now here comes the plug, you can use SuperContainer to really simplify the management of the images from FileMaker.

Resources