-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move paperclip storage for FileUploads and guard with a controller.
- Loading branch information
Showing
7 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ tmp/ | |
.env | ||
bin/stubs | ||
coverage/* | ||
public/system | ||
paperclip/* | ||
public/assets | ||
rerun.txt | ||
tags | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |