Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download file from google drive using paperclip-googledrive?

I have done file uploading on google drive but at the time of downloading got error:

ActionController::MissingFile: Cannot read file original_22_Wages372-817339(wages).pdf

I think it didn't get google drive path.

Attachment model:

has_attached_file :doc, 
  storage: :google_drive,
  google_drive_credentials: "#{Rails.root}/config/google_drive.yml",
  google_drive_options: {
    public_folder_id: '0BwCp_aK6VhiaQmh5TG9Qbm0zcDQ',
    path: proc { |style| "#{style}_#{id}_#{doc.original_filename}" }
  }

Controller:

 attachment = Attachment.find(params[:id])      
 send_file attachment.doc.path(:original), 
   filename: attachment.doc_file_name, 
   type: attachment.doc_content_type, 
   stream: 'true', 
   x_sendfile: true

Thanks in advance

like image 915
Dinesh Avatar asked Sep 29 '22 14:09

Dinesh


1 Answers

You should read file first from your application. And then make it available for download.

attachment = Attachment.find(params[:id])
data = open(attachment.doc.url).read
send_file data, 
  :filename => attachment.doc_file_name, 
  type: attachment.doc_content_type, stream: 'true', 
  :x_sendfile => true

You can also make it available like :

redirect_to attachment.doc.url

But this is not a right way to do so. Because you directly opening your resources to end user.

like image 189
Dipak Gupta Avatar answered Oct 03 '22 09:10

Dipak Gupta