Rails Paperclip uploads numbers after url - ruby-on-rails

Whenever I upload a file through paperclip the end of the URL gets kind of messed up, for instance, in stead of the expected URL
http://localhost:3000/assets/1/file.pdf
I get
http://localhost:3000/assets/1/file.pdf?1415287826
The url and path in my model are:
has_attached_file :file, url: "/assets/:id/:basename.pdf",
path: ":rails_root/public/assets/:id/:basename.pdf"
The actual file is not stored like this, this is only how the url looks using
<%= link_to "Open PDF", upload.file.url %>
It seems like an easy to fix problem but I just can't find the solution....

It's timestamp for your file. It holds the time when your file was uploaded to the server: in this case Time.at(1415287826) => 2014-11-06 15:30:26 +0000
When you will download that file, it will be cached by the browser (images are cached by default, pdfs can be cached now if browser supports pdf rendering). With that timestamp if you upload a file with exactly the same name (so the same url will be generated), browser will not highlight that link as visited. As a result, if it's an image with the same name, its cached version will not be rendered, because browser will considers such link as not visited therefore not cached.

Related

Rails: Permanent url for active storage videos

I'm building a webapp which is offline available.
Therefore I need urls to images and videos were not expiring at any time.
ActiveStorage is not build for that case.
So I build my own media controller for serving images and videos based on the url params
get '/media/:kind/:type/:size/:token'
# /media/video/mp4/middle/ABC123
# /media/image/logo/large/ABC124
There is a MediaObject with a unique token. With the params I get the right variant of the image or the converted video file, look for the path of the blob file, seasoned with a quick lookup what filetype it is and serving it.
path = ActiveStorage::Blob.service.send(:path_for, variant_by(size, type).key)
send_file path, type: content_type, disposition: 'inline'
everything is working good, very good. The images and videos got a cacheable (better looking) permanent url.
Now there is an issue with Safari. Every browser can handle the mp4/webm file the controller returns. Except of safari. The video cannot be watched in the browser.
I'm guessing, there is something missing in the headers.
But I cannot find the place in active storage where the videos served.
When I use the default way
# example
Rails.application.routes.url_helpers.url_for(video.mp4)
the video can be played inline.
I've tried to add the content-length to the response header, add range infos and status :partial_content, add the file extension to the url, but nothing works.
The mime type is correct.
Rails 6 introduced a public option in storage.yml. Set it to true to override the expiring URL stuff: https://edgeguides.rubyonrails.org/active_storage_overview.html#public-access
Also, if you're still on Rails 5.2, you should be able to use:
rails_blob_url(imageable.image, disposition: :attachment)

Rails: Refile for documents to download

Using the refile gem, I have uploaded documents (.pdf, .docx, .pptx, etc.). Uploading is fine. When I use attachemnt_url, it produces something like /attachments/...234jksdf2.../document. When I click the link_to, it downloads the document without an extension.
What's happening to make it operate this way? How can I restore my file type sanity?
I was trying to address the exact same issue, this is one approach I tried:
Refile allows you to save additional metadata such as the content_type: https://github.com/refile/refile#additional-metadata. The resulting file content type will be saved as something like "image/png" or "application/pdf".
We can then apply something like
link_to "Download file", attachment_url(#document, :file, format: #document.file_extension)
Whereby
in document.rb
def file_extension
file_content_type.split("/").last.to_sym
end
The only issue is that this doesn't automatically downloads the file, but rather opens it in a new page where you can then download the file. Still looking for better alternatives!
This is what ended up being the correct solution for me.
link_to "Download file", attachment_url(#document, :file, format: #document.file_extension)
def file_extension
require 'rack/mime'
Rack::Mime::MIME_TYPES.invert[document_content_type].split('.').last
end

Get server file path with Paperclip

I'm using Rails with Paperclip to make a small file upload app. I would like to be able to return the file path on the server of the uploaded file once its done but I can't seem to work out how to get the path? Paperclip only seems to record the name of the file itself.
Does anybody now how to do this?
Assuming you had an attachment called avatar on an instance of a user, you can use user.avatar.path to get the full path of the file on the filesystem, and you can use user.avatar.url to give the path which you could use in image tags and whatnot.
Is that what you're meaning?
I came cross the same problem, so I made a link to it's url in show.html.erb. It works.
<p>
<b>Pdf:</b><%= link_to "PDF" , #product.pdf.url %>
</p>

Accessing an uploaded file without actually storing it in the database or on the server -- Ruby on Rails

I have a system where the user uploads a file and I have to read the file and display its contents on a form without storing it either on the server side or in the database
When the file is uploaded Rails will automatically read it in and make it an instance of Tempfile so it's already stored, it won't however be stored forever on the system.
You can access the file using the normal params[:field_name] syntax as if the file were any other field (don't forget to set content-type of the form to multipart/form-data - i.e.
form_for #mything, :html => {:multipart => true})
and you will get back the tempfile. The tempfile can be read from like any other file.
Rails (Or Maybe Rack I'm not 100% up to date) determines whether to do this or not to uploaded content based on the attachment part of the mulitpart/form-data element containing the file.
It might be possible to override things if you need to though to stop this storage from happening. Common practice however is to just work with the file and then let Ruby deal with the temp file.

Rails send_data() send_file() problem

I have a paperclip attachment in one model, but I`m not saving the file in /public, but /assets. And when the user what to open the file I use the send_data() function, which makes the user to download the file.
My question is how can I show the file in other way (not nessecery to download)? So if the file is a image, I will see it directly in the browser without download.
Thanks!
Try:
send_data foo, :disposition => 'inline'
This would tell the browser to just render the content, instead of prompting the user to save it.
… from http://apidock.com/rails/ActionController/Streaming/send_data

Resources