11class DocumentsController < ApplicationController
2- before_action :set_document , only : [ :show , :update , :destroy ]
2+ before_action :set_document , only : [ :show , :download , :update , :destroy ]
3+ skip_before_action :authorize_request , only : :download
34
45 # GET /documents
56 def index
@@ -10,13 +11,17 @@ def index
1011 # POST /documents
1112 def create
1213 @file = params [ :document ]
13- @file_name = ( Time . now . to_f * 1000 ) . to_s + '.pdf'
14-
15- File . open ( '/var/www/everydocs-files/' + @file_name , 'w+b' ) { |f | f . write ( @file . read ) }
14+
15+ if @file . blank?
16+ @file_name = nil
17+ else
18+ @file_name = SecureRandom . uuid + '.pdf'
19+ File . open ( '/var/www/html/everydocs-web/files/' + @file_name , 'w+b' ) { |f | f . write ( @file . read ) }
20+ end
1621
17- @folder = Folder . find ( params [ :folder ] )
18- @state = State . find ( params [ :state ] )
19- @person = Person . find ( params [ :person ] )
22+ @folder = params [ :folder ] . blank? ? nil : Folder . find ( params [ :folder ] )
23+ @state = params [ :state ] . blank? ? nil : State . find ( params [ :state ] )
24+ @person = params [ :person ] . blank? ? nil : Person . find ( params [ :person ] )
2025
2126 @params = {
2227 "title" => params [ :title ] ,
@@ -37,6 +42,11 @@ def show
3742 json_response ( @document )
3843 end
3944
45+ # GET /documents/file/:id
46+ def download
47+ send_file '/var/www/html/everydocs-web/files/' + @document . document_url , :type => "application/pdf" , :x_sendfile => true
48+ end
49+
4050 # PUT /documents/:id
4151 def update
4252 @folder = Folder . find ( params [ :folder ] )
0 commit comments