Peter R
Peter R

Reputation: 3516

Best place to store files for download

I have a script that runs once a week and creates a CSV file. A download link is emailed out so users can download the file. I am currently storing the file in /tmp/. This works sometimes, but some users get an error and the logs show

ActionController::MissingFile occurred in csv_downloads#download:
Cannot read file tmp/filename.csv

What is a better way to store these files? Here is part of the current script

  csv_string = CSV.generate(write_headers: true, headers: HEADER) { |csv| write_data(csv) }
  file = File.new(File.join(Dir.pwd, "/tmp/#{@file_name}.csv"), "w+")
  file.write(csv_string)
  file.close
  file

Upvotes: 1

Views: 897

Answers (1)

dimakura
dimakura

Reputation: 7655

If the files are public why not to store them at "#{Rails.root}/public/csv-files/#{@file_name}.csv". If public is not an option, then place them at some safe place. Not in /tmp, where they can be deleted by system.

/tmp dir is cleaned regularly by your system. Read this discussion for more info https://askubuntu.com/questions/20783/how-is-the-tmp-directory-cleaned-up

Upvotes: 3

Related Questions