Skip to content

Commit 43f1341

Browse files
author
Gogs
committed
Add route to download the file of a document
1 parent f45b877 commit 43f1341

10 files changed

Lines changed: 52 additions & 12 deletions

File tree

app/controllers/documents_controller.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class 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])

app/controllers/folders_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ def index
77
json_response(@folders)
88
end
99

10+
# GET /folders-all
11+
def all
12+
@folders = current_user.folders
13+
json_response(@folders)
14+
end
15+
1016
# POST /folders
1117
def create
1218
@folder = current_user.folders.create!(folder_params)

app/controllers/states_controller.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
class StatesController < ApplicationController
2-
before_action :set_state, only: [:show]
2+
before_action :set_state, only: [:show, :update, :destroy]
33

44
# GET /states
55
def index
66
@states = State.where("id >= ?", 0)
77
json_response(@states)
88
end
99

10+
# POST /states
11+
def create
12+
@state = current_user.states.create!(state_params)
13+
json_response(@state, :created)
14+
end
15+
1016
# GET /states/:id
1117
def show
1218
json_response(@state)
1319
end
1420

21+
# PUT /states/:id
22+
def update
23+
@tag.update(state_params)
24+
head :no_content
25+
end
26+
27+
# DELETE /states/:id
28+
def destroy
29+
@tag.destroy
30+
head :no_content
31+
end
32+
1533
private
1634

1735
def state_params

app/models/document.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Document < ActiveRecord::Base
77
has_many :documenttags
88
has_many :tags, through: :documenttags
99

10-
validates_presence_of :title, :document_date, :user, :state, :folder
10+
validates_presence_of :title, :document_date, :user, :document_url
1111

1212
def as_json(_options = {})
1313
super include: {

app/models/state.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class State < ActiveRecord::Base
2+
belongs_to :user
3+
24
has_many :documents
35

46
validates_presence_of :name

app/models/tag.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ class Tag < ActiveRecord::Base
44
has_many :documenttags
55
has_many :documents, through: :documenttags
66

7-
validates_presence_of :name, :color
7+
validates_presence_of :name
88
end

app/models/user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class User < ActiveRecord::Base
55
has_many :folders
66
has_many :tags
77
has_many :people
8+
has_many :states
89

910
validates_presence_of :name, :email, :password_digest
1011
end

config/routes.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
post 'signup', to: 'users#create'
66

77
resources :documents
8+
get 'documents/file/:id', to: 'documents#download'
89
resources :folders
9-
resources :states, except: [:create, :update, :destroy, :new, :edit]
10+
get 'folders-all', to: 'folders#all'
11+
resources :states
1012
resources :people
1113
resources :tags
1214
end

db/migrate/20190525164329_create_states.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class CreateStates < ActiveRecord::Migration
22
def change
33
create_table :states do |t|
44
t.string :name
5-
5+
t.references :user
66
t.timestamps null: false
77
end
88
add_index :states, :name, unique: true

db/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363

6464
create_table "states", force: :cascade do |t|
6565
t.string "name", limit: 255
66+
t.integer "user_id", limit: 4
6667
t.datetime "created_at", null: false
6768
t.datetime "updated_at", null: false
6869
end

0 commit comments

Comments
 (0)