0

I have paperclip running to upload and store files in a non-public directory on the server. Now I want to be able to read the files directly and or feed them into a gem such as axlsx. I'm struggling with even simply looping threw a text file and think I'm missing something basic (as is usually the case).

Here is my first attempt at opening the file:

Paperclip config in application.rb:

     config.paperclip_defaults = {:storage => :fog, :fog_credentials => {:provider => "Local", :local_root => "#{Rails.root}/secured_storage"}, :fog_directory => "", :fog_host => "localhost"}

Model:

class Census < ActiveRecord::Base
  has_attached_file :censusfile
  validates_attachment_content_type :censusfile, 
            :content_type => ["application/octet-stream", "text/plain"]
end

In the Controller:

def processcensus
  @census=Census.find(params[:id])
  @file=@census.censusfile.path
end

In the View:

<% File.readlines(@file).read do |line| %>
<%= line %>
<% end  %>

This fails because the 'path' returned by Paperclip is the path relative to its sotrage path, not the full path.

UPDATE: If I add the directory (in this case "secured_storage" in from of the path it work as expected. For example:

  @file="secured_storage/" + @census.censusfile.path

Not sure if this is at all the way to address this. If it is, is there a way to ask Paperclip where it is storing the files??

I've read where I could use:

Paperclip.io_adapters.for(@census.censusfile).path 

But that seems to read the file into an array unless I'm missing something completely. My goal is to be able to loop threw a text file as well as feed an Excel file to axlsx for processing. I would also like to be able to eventually feed these files directly to a user somehow to allow for secure downloads.

I have looked hard for some documentation on all this and haven't found anything that really explains it yet. I'm to that point of just randomly throwing code here or there and hoping something works, which rarely does. Any help/direction that could be provided would be GREATLY appreciated!!!

Mark

MechDog
  • 508
  • 7
  • 18

2 Answers2

2

I think io adapter can support read

Paperclip.io_adapters.for(@census.censusfile).read

so

<% Paperclip.io_adapters.for(@census.censusfile).read %>
<%= line %>
<% end  %>
Abs
  • 3,902
  • 1
  • 31
  • 30
0

Use the copy_to_local_file method. This returns a file object on which you can read like on a normal file`.

joni
  • 5,402
  • 1
  • 27
  • 40