diff --git a/Gemfile b/Gemfile index 63197b66b..318bdb9bb 100644 --- a/Gemfile +++ b/Gemfile @@ -55,6 +55,7 @@ gem 'deface', '~> 1.0' gem 'nokogiri' # HTML/XML parser gem 'sneaky-save', git: 'git://github.com/einzige/sneaky-save.git' gem 'rails_autolink', '~> 1.1', '>= 1.1.6' +gem 'delayed_paperclip' gem 'paperclip', '~> 4.3' # File attachment, image attachment library gem 'aws-sdk', '~> 2.2.8' diff --git a/Gemfile.lock b/Gemfile.lock index 7704ec058..041f92eb0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -120,6 +120,9 @@ GEM delayed_job_active_record (4.1.0) activerecord (>= 3.0, < 5) delayed_job (>= 3.0, < 5) + delayed_paperclip (3.0.1) + activejob (>= 4.2) + paperclip (>= 3.3) devise (3.5.6) bcrypt (~> 3.0) orm_adapter (~> 0.1) @@ -246,6 +249,8 @@ GEM rainbow (2.1.0) rake (11.2.2) rdoc (4.2.0) + recaptcha (4.0.0) + json remotipart (1.2.1) responders (2.2.0) railties (>= 4.2.0, < 5.1) @@ -352,6 +357,7 @@ DEPENDENCIES commit_param_routing deface (~> 1.0) delayed_job_active_record + delayed_paperclip devise (= 3.5.6) devise-async devise_invitable @@ -379,6 +385,7 @@ DEPENDENCIES rails (= 4.2.5) rails_12factor rails_autolink (~> 1.1, >= 1.1.6) + recaptcha remotipart (~> 1.2) rgl roo (~> 2.1.0) diff --git a/app/assets/javascripts/my_modules/results.js.erb b/app/assets/javascripts/my_modules/results.js.erb index f5d0e824f..114244e38 100644 --- a/app/assets/javascripts/my_modules/results.js.erb +++ b/app/assets/javascripts/my_modules/results.js.erb @@ -149,8 +149,8 @@ var ResultTypeEnum = Object.freeze({ COMMENT: 3 }); -function processResult(ev, resultTypeEnum, editMode, forS3) { - forS3 = (typeof forS3 !== 'undefined') ? forS3 : false; +function processResult(ev, resultTypeEnum, editMode) { + var $form = $(ev.target.form); $form.clearFormErrors(); @@ -164,14 +164,9 @@ function processResult(ev, resultTypeEnum, editMode, forS3) { editMode); if(nameValid && filesValid) { - if(forS3) { - // Redirects file uploading to S3 - var url = '/asset_signature.json'; - directUpload(ev, url); - } else { - // Local file uploading - animateSpinner(); - } + // Local file uploading + animateSpinner(); + } break; case ResultTypeEnum.TABLE: diff --git a/app/assets/javascripts/protocols/steps.js.erb b/app/assets/javascripts/protocols/steps.js.erb index 9dbb8b598..96352b6d7 100644 --- a/app/assets/javascripts/protocols/steps.js.erb +++ b/app/assets/javascripts/protocols/steps.js.erb @@ -495,7 +495,7 @@ $("[data-action='new-step']").on("ajax:success", function(e, data) { // Needed because server-side validation failure clears locations of // files to be uploaded and checklist's items etc. Also user // experience is improved -function processStep(ev, editMode, forS3) { +function processStep(ev, editMode) { var $form = $(ev.target.form); $form.clearFormErrors(); $form.removeBlankExcelTables(editMode); @@ -513,14 +513,8 @@ function processStep(ev, editMode, forS3) { <%= Constants::TEXT_MAX_LENGTH %>); if (filesValid && checklistsValid && nameValid && descriptionValid) { - if (forS3) { - // Redirects file uploading to S3 - var url = '/asset_signature.json'; - directUpload(ev, url); - } else { - // Local file uploading - animateSpinner(); - } + // Local file uploading + animateSpinner(); } } diff --git a/app/assets/javascripts/users/registrations/edit.js b/app/assets/javascripts/users/registrations/edit.js index 69db00151..28adf7ca9 100644 --- a/app/assets/javascripts/users/registrations/edit.js +++ b/app/assets/javascripts/users/registrations/edit.js @@ -63,19 +63,13 @@ forms $(this).renderFormErrors("user", data.responseJSON); }); -function processFile(ev, forS3) { +function processFile(ev) { var $form = $(ev.target.form); $form.clearFormErrors(); var $fileInput = $form.find("input[type=file]"); if(filesValidator(ev, $fileInput, FileTypeEnum.AVATAR)) { - if(forS3) { - // Redirects file uploading to S3 - var url = "/avatar_signature.json"; - directUpload(ev, url, true); - } else { - // Local file uploading - animateSpinner(); - } + // Local file uploading + animateSpinner(); } } diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index a81b6b03b..51efe466c 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb @@ -48,7 +48,11 @@ class AssetsController < ApplicationController def preview if @asset.is_image? - redirect_to @asset.url(:medium), status: 307 + if @asset.file_content_type.include? %w(images/bmp images/tiff) + redirect_to @asset.url(:web_friendly), status: 307 + else + redirect_to @asset.url(:medium), status: 307 + end else render_400 end @@ -129,7 +133,7 @@ class AssetsController < ApplicationController posts.push({ url: s3_post.url, fields: s3_post.fields, - style_option: option, + style_option: option.is_a?(Array) ? option[0] : option, mime_type: asset.file_content_type }) end diff --git a/app/controllers/my_modules_controller.rb b/app/controllers/my_modules_controller.rb index a222066b4..1a57f06a6 100644 --- a/app/controllers/my_modules_controller.rb +++ b/app/controllers/my_modules_controller.rb @@ -334,7 +334,6 @@ class MyModulesController < ApplicationController private def load_vars - @direct_upload = ENV['PAPERCLIP_DIRECT_UPLOAD'] == "true" @my_module = MyModule.find_by_id(params[:id]) if @my_module @experiment = @my_module.experiment diff --git a/app/controllers/result_assets_controller.rb b/app/controllers/result_assets_controller.rb index 80adafce1..29b90d316 100644 --- a/app/controllers/result_assets_controller.rb +++ b/app/controllers/result_assets_controller.rb @@ -3,7 +3,6 @@ class ResultAssetsController < ApplicationController before_action :load_vars, only: [:edit, :update, :download] before_action :load_vars_nested, only: [:new, :create] - before_action :load_paperclip_vars before_action :check_create_permissions, only: [:new, :create] before_action :check_edit_permissions, only: [:edit, :update] @@ -18,16 +17,11 @@ class ResultAssetsController < ApplicationController ) respond_to do |format| - format.json { + format.json do render json: { - html: render_to_string({ - partial: "new.html.erb", - locals: { - direct_upload: @direct_upload - } - }) + html: render_to_string(partial: 'new.html.erb') }, status: :ok - } + end end end @@ -87,16 +81,11 @@ class ResultAssetsController < ApplicationController def edit respond_to do |format| - format.json { + format.json do render json: { - html: render_to_string({ - partial: "edit.html.erb", - locals: { - direct_upload: @direct_upload - } - }) + html: render_to_string(partial: 'edit.html.erb') }, status: :ok - } + end end end @@ -188,10 +177,6 @@ class ResultAssetsController < ApplicationController private - def load_paperclip_vars - @direct_upload = ENV['PAPERCLIP_DIRECT_UPLOAD'] == "true" - end - def load_vars @result_asset = ResultAsset.find_by_id(params[:id]) diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index b0d46a564..21a4b6f38 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -1,7 +1,6 @@ class StepsController < ApplicationController before_action :load_vars, only: [:edit, :update, :destroy, :show] before_action :load_vars_nested, only: [:new, :create] - before_action :load_paperclip_vars before_action :convert_table_contents_to_utf8, only: [:create, :update] before_action :check_view_permissions, only: [:show] @@ -15,41 +14,16 @@ class StepsController < ApplicationController @step = Step.new respond_to do |format| - format.json { + format.json do render json: { - html: render_to_string({ - partial: "new.html.erb", - locals: { - direct_upload: @direct_upload - } - }) + html: render_to_string(partial: 'new.html.erb') } - } + end end end def create - if @direct_upload - new_assets = [] - step_data = step_params.except(:assets_attributes) - step_assets = step_params.slice(:assets_attributes) - @step = Step.new(step_data) - - unless step_assets[:assets_attributes].nil? - step_assets[:assets_attributes].each do |_i, data| - # Ignore destroy requests on create - next if data[:_destroy].present? - - asset = Asset.new(data) - asset.created_by = current_user - asset.last_modified_by = current_user - new_assets << asset - end - end - @step.assets << new_assets - else - @step = Step.new(step_params) - end + @step = Step.new(step_params) @step.completed = false @step.position = @protocol.number_of_steps @@ -107,12 +81,7 @@ class StepsController < ApplicationController format.json { render json: { - html: render_to_string({ - partial: "new.html.erb", - locals: { - direct_upload: @direct_upload - } - }) + html: render_to_string(partial: 'new.html.erb') }, status: :bad_request } end @@ -121,26 +90,26 @@ class StepsController < ApplicationController def show respond_to do |format| - format.json { + format.json do render json: { - html: render_to_string({ - partial: "steps/step.html.erb", locals: {step: @step} - })}, status: :ok - } + html: render_to_string( + partial: 'steps/step.html.erb', + locals: { step: @step } + ) + }, + status: :ok + end end end def edit respond_to do |format| - format.json { + format.json do render json: { - html: render_to_string({ - partial: "edit.html.erb", - locals: { - direct_upload: @direct_upload - } - })}, status: :ok - } + html: render_to_string(partial: 'edit.html.erb') + }, + status: :ok + end end end @@ -155,23 +124,6 @@ class StepsController < ApplicationController # NOTE - step_params_all variable is updated destroy_attributes(step_params_all) - if @direct_upload - step_data = step_params_all.except(:assets_attributes) - step_assets = step_params_all.slice(:assets_attributes) - step_params_all = step_data - - if step_assets.include? :assets_attributes - step_assets[:assets_attributes].each do |i, data| - asset = Asset.new(data) - unless @step.assets.include? asset or not asset - asset.created_by = current_user - asset.last_modified_by = current_user - @step.assets << asset - end - end - end - end - @step.assign_attributes(step_params_all) @step.last_modified_by = current_user @@ -566,10 +518,6 @@ class StepsController < ApplicationController end end - def load_paperclip_vars - @direct_upload = ENV['PAPERCLIP_DIRECT_UPLOAD'] == "true" - end - def load_vars @step = Step.find_by_id(params[:id]) @protocol = @step.protocol diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 11d35d4ea..388267a4b 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -1,5 +1,4 @@ class Users::RegistrationsController < Devise::RegistrationsController - before_action :load_paperclip_vars prepend_before_action :check_captcha, only: [:create] def avatar @@ -43,15 +42,6 @@ class Users::RegistrationsController < Devise::RegistrationsController def update_resource(resource, params) @user_avatar_url = avatar_path(current_user, :thumb) - if @direct_upload - if params.include? :avatar_file_name - file_name = params[:avatar_file_name] - file_ext = file_name.split(".").last - params[:avatar_content_type] = Rack::Mime.mime_type(".#{file_ext}") - resource.avatar.destroy - end - end - if params.include? :change_password # Special handling if changing password params.delete(:change_password) @@ -169,10 +159,6 @@ class Users::RegistrationsController < Devise::RegistrationsController protected - def load_paperclip_vars - @direct_upload = ENV['PAPERCLIP_DIRECT_UPLOAD'] == "true" - end - # Called upon creating User (before .save). Permits parameters and extracts # initials from :full_name (takes at most 4 chars). If :full_name is empty, it # uses "PLCH" as a placeholder (user won't get error complaining about diff --git a/app/models/asset.rb b/app/models/asset.rb index ad2832c56..80cee0ec4 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -6,7 +6,8 @@ class Asset < ActiveRecord::Base require 'tempfile' # Paperclip validation - has_attached_file :file, styles: { medium: Constants::MEDIUM_PIC_FORMAT } + has_attached_file :file, + styles: { medium: [Constants::MEDIUM_PIC_FORMAT, :jpg] } validates_attachment :file, presence: true, @@ -20,6 +21,7 @@ class Asset < ActiveRecord::Base do_not_validate_attachment_file_type :file before_file_post_process :is_image? + process_in_background :file # Asset validation # This could cause some problems if you create empty asset and want to diff --git a/app/views/my_modules/results.html.erb b/app/views/my_modules/results.html.erb index 6afe560a2..13439cbfc 100644 --- a/app/views/my_modules/results.html.erb +++ b/app/views/my_modules/results.html.erb @@ -43,7 +43,7 @@ data-module-protocols-step-text="<%=t 'tutorial.module_results_html' %>" data-module-protocols-click-samples-step-text="<%=t 'tutorial.module_results_click_samples_html' %>"> <% ordered_result_of(@my_module).each do |result| %> - <%= render partial: "result", locals: {result: result, direct_upload: @direct_upload} %> + <%= render partial: "result", locals: { result: result } %> <% end %> diff --git a/app/views/result_assets/_edit.html.erb b/app/views/result_assets/_edit.html.erb index b1bf833fd..4735a89e4 100644 --- a/app/views/result_assets/_edit.html.erb +++ b/app/views/result_assets/_edit.html.erb @@ -7,7 +7,7 @@ <%= ff.file_field :file %> <% end %>
- <%= f.submit t("result_assets.edit.update"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.FILE, true, #{direct_upload});" %> + <%= f.submit t("result_assets.edit.update"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.FILE, true);" %> diff --git a/app/views/result_assets/_new.html.erb b/app/views/result_assets/_new.html.erb index e96a8190f..d938ac740 100644 --- a/app/views/result_assets/_new.html.erb +++ b/app/views/result_assets/_new.html.erb @@ -4,7 +4,7 @@ <%= f.fields_for :asset do |ff| %> <%= ff.file_field :file %> <% end %> - <%= f.submit t("result_assets.new.create"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.FILE, false, #{direct_upload});" %> + <%= f.submit t("result_assets.new.create"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.FILE, false);" %> diff --git a/app/views/steps/_edit.html.erb b/app/views/steps/_edit.html.erb index e84b24b04..b22b003a7 100644 --- a/app/views/steps/_edit.html.erb +++ b/app/views/steps/_edit.html.erb @@ -4,7 +4,7 @@
<%= render partial: "empty_step.html.erb", locals: {step: @step, f: f} %>
- <%= f.submit t("protocols.steps.edit.edit_step"), class: 'btn btn-primary step-save', onclick: "processStep(event, true, #{direct_upload});" %> + <%= f.submit t("protocols.steps.edit.edit_step"), class: 'btn btn-primary step-save', onclick: "processStep(event, true);" %> <%= t("general.cancel")%> diff --git a/app/views/steps/_new.html.erb b/app/views/steps/_new.html.erb index 5b88dc305..c6803582e 100644 --- a/app/views/steps/_new.html.erb +++ b/app/views/steps/_new.html.erb @@ -4,7 +4,7 @@
<%= render partial: "empty_step.html.erb", locals: {step: @step, f: f} %>
- <%= f.submit t("protocols.steps.new.add_step"), class: 'btn btn-primary step-save', onclick: "processStep(event, false, #{direct_upload});" %> + <%= f.submit t("protocols.steps.new.add_step"), class: 'btn btn-primary step-save', onclick: "processStep(event, false);" %> diff --git a/app/views/users/registrations/edit.html.erb b/app/views/users/registrations/edit.html.erb index 975586017..af6020987 100644 --- a/app/views/users/registrations/edit.html.erb +++ b/app/views/users/registrations/edit.html.erb @@ -40,7 +40,7 @@
<%=t "general.cancel" %> - <%= f.submit t("users.registrations.edit.avatar_submit"), class: 'btn btn-primary', onclick: "processFile(event, #{@direct_upload});" %> + <%= f.submit t("users.registrations.edit.avatar_submit"), class: 'btn btn-primary', onclick: "processFile(event);" %>
diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index f0857307b..c967bdb3e 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -202,7 +202,7 @@ class Constants ].freeze WHITELISTED_IMAGE_TYPES = [ - 'gif', 'jpeg', 'pjpeg', 'png', 'x-png', 'svg+xml', 'bmp' + 'gif', 'jpeg', 'pjpeg', 'png', 'x-png', 'svg+xml', 'bmp', 'tiff' ].freeze # Very basic regex to check for validity of emails