0

In a rails app, if I am working locally, I can read a folder, with its own subfolders and files. in Heroku is not working. any help?

def upload_multifiles
dir = params[:path]

files_directories = Dir["#{dir}/**/*"]
files = []

files_directories.each do |file_directory|
  if file_directory.include? ".pdf"
    files << file_directory
  end
end

end

the error that i get is:

2017-08-07T08:23:06.984190+00:00 app[web.1]: I, [2017-08-07T08:23:06.984142 #4]  INFO -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8] Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.6ms)
2017-08-07T08:23:06.983899+00:00 app[web.1]: found_dir
2017-08-07T08:23:06.984787+00:00 app[web.1]: F, [2017-08-07T08:23:06.984747 #4] FATAL -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8] app/controllers/multiuploader_controller.rb:14:in `open'
2017-08-07T08:23:06.984642+00:00 app[web.1]: F, [2017-08-07T08:23:06.984593 #4] FATAL -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8]
2017-08-07T08:23:06.984756+00:00 app[web.1]: F, [2017-08-07T08:23:06.984698 #4] FATAL -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8]
2017-08-07T08:23:06.984697+00:00 app[web.1]: F, [2017-08-07T08:23:06.984647 #4] FATAL -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8] Errno::ENOENT (No such file or directory @ dir_initialize - /Users/xxxxx/yyyyy/ttttt/zzzzzz):
2017-08-07T08:23:06.984788+00:00 app[web.1]: [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8] app/controllers/multiuploader_controller.rb:14:in `upload_multifiles'

Thanks a lot in advance

Albert

djmayank
  • 388
  • 2
  • 18
AlbertMunichMar
  • 1,680
  • 5
  • 25
  • 49

1 Answers1

0

You can not use the File system on heroku like you can do on your lolcal machine. You will need to use something like S3 if you want to store files (from uploads). You can read more about it here: How to use heroku's ephemeral filesystem

If you just want to read files that you have shipped with your app: the path /Users/xxxx is a OS X directory structure, which does not exist on heroku. You could use Rails.root to build paths that work in all environments.

In addition: listing files from a path supplied through parameters sounds like a huge security risk to me. You should not do this.

Also: there is a more elegant way to create a filtered array, for example:

files = ['a.pdf', 'b.pdf', 'c.txt']
pdfs = files.select { |file| file.ends_with?('.pdf') }
Pascal
  • 8,464
  • 1
  • 20
  • 31
  • thanks a lot for your answer. the problem with S3 would be the same. i don't want to upload the files one by one manually. i wanted to write an script and do it with code. – AlbertMunichMar Aug 08 '17 at 11:39