Skip to content

Commit

Permalink
Move paperclip storage for FileUploads and guard with a controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
Domenoth committed Oct 27, 2015
1 parent 0c28bca commit b3faa43
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tmp/
.env
bin/stubs
coverage/*
public/system
paperclip/*
public/assets
rerun.txt
tags
Expand Down
33 changes: 33 additions & 0 deletions app/controllers/original_files_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class OriginalFilesController < ApplicationController
def show
@upload = FileUpload.where(id: params[:id]).first
if params_match? && viewing_allowed?
render text: File.read(file_path), content_type: @upload.file_content_type
else
head :not_found
end
end

private

def file_path
@file_path ||= "#{path_params}#{".#{params[:format]}" if params[:format].present?}"
end

def path_params
url_params = params[:file_path].split('/').reject { |x| x == '..' }
File.join([Rails.root, 'paperclip', 'file_uploads', 'files', url_params].flatten)
end

def viewing_allowed?
if @upload.kind == 'original'
can?(:access, :original_files)
else
true
end
end

def params_match?
@upload && file_path && file_path == @upload.file.path && File.file?(file_path)
end
end
1 change: 1 addition & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def grant_admin_access
can :access, :rails_admin
can :dashboard
can :search, Entity
can :access, :original_files
end

def grant_redact
Expand Down
4 changes: 3 additions & 1 deletion app/models/file_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class FileUpload < ActiveRecord::Base
validates_inclusion_of :kind, in: %w( original supporting )

belongs_to :notice
has_attached_file :file
has_attached_file :file,
path: ":rails_root/paperclip/:class/:attachment/:id_partition/:style/:filename",
url: "/:class/:attachment/:id/:id_partition/:style/:filename"

before_save :rename_file, if: ->(instance) { instance.file_name.present? }
delegate :url, to: :file
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Chill::Application.routes.draw do
get "file_uploads/files/:id/*file_path", to: 'original_files#show'

devise_for :users

mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
Expand Down
Empty file added paperclip/.gitkeep
Empty file.
19 changes: 19 additions & 0 deletions spec/controllers/original_files_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'spec_helper'

describe OriginalFilesController do

describe "GET 'show'" do
let(:upload) { create(:file_upload) }
it "returns not found without valid params" do
get 'show', id: upload.id, file_path: ['a', 'b', 'c']
expect(response).not_to be_success
end

it "returns http success" do
File.stub(:file?).and_return(:true)
File.stub(:read).and_return('Content!')
expect(response).to be_success
end
end

end

0 comments on commit b3faa43

Please sign in to comment.