cleanup direct upload

This commit is contained in:
zmagod 2016-12-08 11:26:13 +01:00
parent 310b61ec12
commit ac52e643be
18 changed files with 59 additions and 144 deletions

View file

@ -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'

View file

@ -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)

View file

@ -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:

View file

@ -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();
}
}

View file

@ -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();
}
}

View file

@ -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

View file

@ -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

View file

@ -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])

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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 %>
</div>

View file

@ -7,7 +7,7 @@
<%= ff.file_field :file %>
<% end %>
<hr>
<%= 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);" %>
<button type="button" class="btn btn-default cancel-edit">
<%= t("general.cancel")%>
</button>

View file

@ -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);" %>
<button type="button" class="btn btn-default cancel-new">
<%= t("general.cancel")%>
</button>

View file

@ -4,7 +4,7 @@
<hr>
<%= render partial: "empty_step.html.erb", locals: {step: @step, f: f} %>
<hr>
<%= 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);" %>
<a type="button" data-action="cancel-edit" class="btn btn-default" href="<%= step_path(id: @step, format: :json) %>" data-remote="true">
<%= t("general.cancel")%>
</a>

View file

@ -4,7 +4,7 @@
<hr>
<%= render partial: "empty_step.html.erb", locals: {step: @step, f: f} %>
<hr>
<%= 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);" %>
<button type="button" data-action="cancel-new" class="btn btn-default">
<%= t("general.cancel")%>
</button>

View file

@ -40,7 +40,7 @@
</div>
<div>
<a href="#" class="btn btn-default" data-action="cancel"><%=t "general.cancel" %></a>
<%= 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);" %>
</div>
</div>
</div>

View file

@ -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