Module: SendFile

Defined in:
lib/autoload/send_file.rb

Class Method Summary collapse

Class Method Details

.local_path(path) ⇒ String

Obtains the local path for the given publicly accessible file path.

Parameters:

  • path (String)

    The URL to the publicly accessible file.

Returns:

  • (String)

    The absolute file path to the publicly accessible file.


24
25
26
# File 'lib/autoload/send_file.rb', line 24

def self.local_path(path)
  File.join(Rails.public_path, URI.decode(path))
end

.send_file(file, public_name = File.basename(file)) ⇒ String

Makes the given file publicly accessible.

Parameters:

  • file (String)

    The path to the local file.

  • public_name (String) (defaults to: File.basename(file))

    The file's public name, by default set to the base name of the file.

Returns:

  • (String)

    The url to the publicly accessible file


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/autoload/send_file.rb', line 8

def self.send_file(file, public_name = File.basename(file))
  # TODO: Support remote path and publish the file on the load balancing server.
  downloads_dir = Application.config.x.public_download_folder
  public_dir = File.join(Rails.public_path, downloads_dir)

  temporary_dir = File.basename(Dir.mktmpdir(nil, public_dir))
  public_file = File.join(public_dir, temporary_dir, public_name)
  FileUtils.cp(file, public_file)

  URI.encode(File.join('/', downloads_dir, temporary_dir, public_name))
end