Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
Set cache_dir and move uploads outside of public
Browse files Browse the repository at this point in the history
This allows it to work on Heroku while also keeping the potentially
sensitive documents out of webroot.

We are intentionally at this point NOT persisting these files
indefinitely (which is sort of a problem for the model to handle as it
doesn’t really expect these files to disappear all the time).

It will likely be prudent to move them to S3 if even for a short time.
  • Loading branch information
JPrevost committed Dec 8, 2015
1 parent 8c4718d commit 3c21719
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 45 deletions.
12 changes: 8 additions & 4 deletions app/controllers/submissions_controller.rb
Expand Up @@ -10,18 +10,22 @@ def create
@submission = Submission.new(submission_params)
@submission.user = current_user
if @submission.save
process_submission(@submission)
flash.notice = 'Your Submission is now in progress.'
# temporarily render mets for demo purposes
# we'll send to a receipt / next steps page eventually
render json: @submission.to_mets
# redirect_to root_path
redirect_to root_path
else
render 'new'
end
end

private

def process_submission(submission)
# this will likely want to be an asynch job queue
# to create the package and handle the sword submission
submission.to_sword_package
end

def submission_params
params.require(:submission).permit(:title, :agreed_to_license, :author,
:journal, :doi, :grant_number, :doe,
Expand Down
43 changes: 5 additions & 38 deletions app/uploaders/document_uploader.rb
@@ -1,50 +1,17 @@
# encoding: utf-8

class DocumentUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick

# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog

def cache_dir
"#{Rails.root}/tmp/uploads"
end

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
"#{cache_dir}/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path(
# "fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end

# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
# # do something
# end

# Create different versions of your uploaded files:
# version :thumb do
# process resize_to_fit: [50, 50]
# end

# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(pdf)
end

# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb.
# def filename
# "something.jpg" if original_filename
# end
end
5 changes: 4 additions & 1 deletion test/features/submission_test.rb
Expand Up @@ -2,6 +2,8 @@

class SubmissionPagesTest < Capybara::Rails::TestCase
def setup
FileUtils.rm_f('tmp/69b9156a124c96bbdb55cad753810e14.zip')
FileUtils.rm_f('tmp/40550618d6b4d97792b0773c97207186.zip')
Rails.application.env_config['devise.mapping'] = Devise.mappings[:user]
Rails.application.env_config['omniauth.auth'] =
OmniAuth.config.mock_auth[:mit_oauth2]
Expand Down Expand Up @@ -107,6 +109,7 @@ def base_valid_form
check('I am authorized to submit this article.')
click_on('Create Submission')
assert_equal(Submission.count, (subs + 1))
assert('a_pdf.pdf', Submission.last.documents.first.filename)
@sub = Submission.last
assert('a_pdf.pdf', @sub.documents.first.filename)
end
end
4 changes: 2 additions & 2 deletions test/models/submission_test.rb
Expand Up @@ -64,9 +64,9 @@ class SubmissionTest < ActiveSupport::TestCase

def setup_sword_files(sub)
FileUtils.rm_f(sub.sword_path)
FileUtils.mkdir_p('./public/uploads/submission/documents/783478147')
FileUtils.mkdir_p('./tmp/uploads/submission/documents/783478147')
FileUtils.cp(['./test/fixtures/a_pdf.pdf', './test/fixtures/b_pdf.pdf'],
'./public/uploads/submission/documents/783478147')
'./tmp/uploads/submission/documents/783478147')
end

def cleaup_sword_files(sub)
Expand Down

0 comments on commit 3c21719

Please sign in to comment.