Class: Meribah::DownloadController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/meribah/download_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/meribah/download_controller.rb', line 5

def index
  yaml_path = nil
  Meribah::MUTEX.synchronize do
    # Return if there are no files
    if session[:meribah_files].nil? || session[:meribah_files].empty?
      redirect_to '/404.html' 
      return
    end
    
    # Get the path to the YAML file
    files = session[:meribah_files]
    yaml_path = "#{Rails.root}/tmp/#{files.shift}"
    session[:meribah_files] = files
    unless File.exist? yaml_path
      redirect_to '/404.html' 
      return
    end
  end
  
  # Extract path and options from YAML file
  ::Meribah::TEMPFILES.delete(yaml_path)
  yaml_file = File.open(yaml_path)
  path,options = YAML::load(yaml_file)
  yaml_file.close
  File::unlink(yaml_file)
  
  unless File.exist? path
    redirect_to '/404.html' 
    return
  end
  
  # Send the file
  file = File.open(path,'rb')
  data = file.read
  file.close
  File::unlink(path) if ::Meribah::TEMPFILES.delete(path) # Delete it if it was a temp file
  send_data data, options
end