Class: ErpApp::PublicController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/erp_app/public_controller.rb

Instance Method Summary collapse

Instance Method Details

#downloadObject

Download Inline Example: /download/filename.ext?path=/directory&disposition=inline Download Prompt Example: /download/filename.ext?path=/directory&disposition=attachment



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
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/erp_app/public_controller.rb', line 11

def download
  filename = "#{params[:filename]}.#{params[:format]}"

  # remove trailing period if present
  if filename.last == '.'
    filename.chop!
  end

  path = params[:path].blank? ? nil : params[:path].gsub("/#{params[:filename]}.#{params[:format]}", '')
  disposition = params[:disposition]

  file = FileAsset.where(:name => filename)
  file = file.where(:directory => path) if path
  file = file.first

  unless file.nil?
    if file.is_secured?
      begin
        unless current_user == false
          current_user.with_capability(:download, file) do
            serve_file(file, disposition)
          end
        else
          raise ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability
        end
      rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
        render :text => ex.message and return
      rescue => ex
        Rails.logger.error ex.message
        Rails.logger.error ex.backtrace.join("\n")

        ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

        render :text => ex.message and return
      end
    else
      serve_file(file, disposition)
    end
  else
    render :text => 'File not found.'
  end
end