2019-06-14 16:15:30 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-12 16:52:43 +01:00
|
|
|
class AssetsController < ApplicationController
|
2016-09-29 15:30:55 +02:00
|
|
|
include WopiUtil
|
2019-06-19 15:19:47 +02:00
|
|
|
include AssetsActions
|
2017-03-13 13:20:49 +01:00
|
|
|
# include ActionView::Helpers
|
2019-11-06 15:21:56 +01:00
|
|
|
include ActiveStorage::SetCurrent
|
2017-03-13 13:20:49 +01:00
|
|
|
include ActionView::Helpers::AssetTagHelper
|
2016-12-09 13:59:49 +01:00
|
|
|
include ActionView::Helpers::TextHelper
|
2017-03-13 13:20:49 +01:00
|
|
|
include ActionView::Helpers::UrlHelper
|
|
|
|
include ActionView::Context
|
2019-05-10 16:25:18 +02:00
|
|
|
include ApplicationHelper
|
2017-03-13 13:20:49 +01:00
|
|
|
include InputSanitizeHelper
|
|
|
|
include FileIconsHelper
|
2019-07-03 15:35:49 +02:00
|
|
|
include MyModulesHelper
|
2016-09-29 15:30:55 +02:00
|
|
|
|
2019-03-15 20:59:15 +01:00
|
|
|
before_action :load_vars, except: :create_wopi_file
|
2019-08-07 13:29:04 +02:00
|
|
|
before_action :check_read_permission, except: :edit
|
2016-12-21 16:52:15 +01:00
|
|
|
before_action :check_edit_permission, only: :edit
|
2019-05-13 16:53:58 +02:00
|
|
|
|
2018-03-30 11:50:28 +02:00
|
|
|
def file_preview
|
2019-10-03 11:54:26 +02:00
|
|
|
file_type = @asset.file.metadata[:asset_type] || (@asset.previewable? ? 'previewable' : false)
|
2018-03-30 11:50:28 +02:00
|
|
|
response_json = {
|
2018-10-19 10:00:58 +02:00
|
|
|
'id' => @asset.id,
|
2019-10-01 16:14:28 +02:00
|
|
|
'type' => file_type,
|
2019-07-01 23:30:20 +02:00
|
|
|
'filename' => truncate(escape_input(@asset.file_name),
|
2019-04-02 16:38:53 +02:00
|
|
|
length: Constants::FILENAME_TRUNCATION_LENGTH),
|
2019-08-09 14:56:00 +02:00
|
|
|
'download-url' => asset_file_url_path(@asset)
|
2018-03-30 11:50:28 +02:00
|
|
|
}
|
2018-04-26 17:00:51 +02:00
|
|
|
|
2019-04-02 17:45:26 +02:00
|
|
|
can_edit = if @assoc.class == Step
|
|
|
|
can_manage_protocol_in_module?(@protocol) || can_manage_protocol_in_repository?(@protocol)
|
|
|
|
elsif @assoc.class == Result
|
|
|
|
can_manage_module?(@my_module)
|
2020-05-06 15:17:54 +02:00
|
|
|
elsif @assoc.class == RepositoryCell && !@repository.is_a?(RepositorySnapshot)
|
2019-07-12 16:43:54 +02:00
|
|
|
can_manage_repository_rows?(@repository)
|
2019-04-02 17:45:26 +02:00
|
|
|
end
|
2019-10-01 16:14:28 +02:00
|
|
|
if response_json['type'] == 'previewable'
|
2019-07-02 13:15:57 +02:00
|
|
|
if ['image/jpeg', 'image/pjpeg'].include? @asset.file.content_type
|
2019-10-08 10:42:55 +02:00
|
|
|
response_json['quality'] = @asset.file_image_quality || 80
|
2019-04-15 09:49:44 +02:00
|
|
|
end
|
2018-04-24 13:11:55 +02:00
|
|
|
response_json.merge!(
|
2019-04-02 16:38:53 +02:00
|
|
|
'editable' => @asset.editable_image? && can_edit,
|
2019-04-03 16:31:56 +02:00
|
|
|
'mime-type' => @asset.file.content_type,
|
2019-07-16 13:40:54 +02:00
|
|
|
'large-preview-url' => rails_representation_url(@asset.large_preview)
|
2018-04-24 13:11:55 +02:00
|
|
|
)
|
2019-07-02 13:15:57 +02:00
|
|
|
elsif response_json['type'] == 'marvinjs'
|
|
|
|
response_json.merge!(
|
|
|
|
'editable' => can_edit,
|
2019-07-16 13:40:54 +02:00
|
|
|
'large-preview-url' => rails_representation_url(@asset.large_preview),
|
2019-07-02 13:15:57 +02:00
|
|
|
'update-url' => marvin_js_asset_path(@asset.id),
|
|
|
|
'description' => @asset.file.metadata[:description],
|
|
|
|
'name' => @asset.file.metadata[:name]
|
|
|
|
)
|
2018-03-30 11:50:28 +02:00
|
|
|
else
|
2019-07-12 11:34:41 +02:00
|
|
|
|
2019-07-01 23:30:20 +02:00
|
|
|
response_json['preview-icon'] = render_to_string(partial: 'shared/file_preview_icon.html.erb',
|
|
|
|
locals: { asset: @asset })
|
2018-03-30 11:50:28 +02:00
|
|
|
end
|
|
|
|
|
2019-05-10 16:25:18 +02:00
|
|
|
if wopi_enabled? && wopi_file?(@asset)
|
2019-03-08 12:22:35 +01:00
|
|
|
edit_supported, title = wopi_file_edit_button_status
|
2018-03-30 11:50:28 +02:00
|
|
|
response_json['wopi-controls'] = render_to_string(
|
2019-05-10 16:51:54 +02:00
|
|
|
partial: 'assets/wopi/file_wopi_controls.html.erb',
|
2018-12-31 18:10:12 +01:00
|
|
|
locals: {
|
|
|
|
asset: @asset,
|
|
|
|
can_edit: can_edit,
|
|
|
|
edit_supported: edit_supported,
|
|
|
|
title: title
|
|
|
|
}
|
2018-03-30 11:50:28 +02:00
|
|
|
)
|
|
|
|
end
|
2016-12-20 15:36:07 +01:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2018-03-30 11:50:28 +02:00
|
|
|
render json: response_json
|
2016-12-20 15:36:07 +01:00
|
|
|
end
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-08 12:22:35 +01:00
|
|
|
# Check whether the wopi file can be edited and return appropriate response
|
|
|
|
def wopi_file_edit_button_status
|
2019-07-01 23:30:20 +02:00
|
|
|
file_ext = @asset.file_name.split('.').last
|
2019-03-08 12:22:35 +01:00
|
|
|
if Constants::WOPI_EDITABLE_FORMATS.include?(file_ext)
|
|
|
|
edit_supported = true
|
|
|
|
title = ''
|
|
|
|
else
|
|
|
|
edit_supported = false
|
|
|
|
title = if Constants::FILE_TEXT_FORMATS.include?(file_ext)
|
|
|
|
I18n.t('assets.wopi_supported_text_formats_title')
|
|
|
|
elsif Constants::FILE_TABLE_FORMATS.include?(file_ext)
|
|
|
|
I18n.t('assets.wopi_supported_table_formats_title')
|
|
|
|
else
|
|
|
|
I18n.t('assets.wopi_supported_presentation_formats_title')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return edit_supported, title
|
|
|
|
end
|
|
|
|
|
2019-08-09 14:56:00 +02:00
|
|
|
def file_url
|
|
|
|
return render_404 unless @asset.file.attached?
|
|
|
|
|
|
|
|
render plain: @asset.file.blob.service_url
|
|
|
|
end
|
|
|
|
|
2020-05-27 16:59:28 +02:00
|
|
|
def download
|
|
|
|
redirect_to rails_blob_path(@asset.file, disposition: 'attachment')
|
|
|
|
end
|
|
|
|
|
2016-08-10 17:49:25 +02:00
|
|
|
def edit
|
2019-07-01 23:30:20 +02:00
|
|
|
action = @asset.file_size.zero? && !@asset.locked? ? 'editnew' : 'edit'
|
|
|
|
@action_url = append_wd_params(@asset.get_action_url(current_user, action, false))
|
2016-09-29 12:19:29 +02:00
|
|
|
@favicon_url = @asset.favicon_url('edit')
|
2016-11-30 16:48:42 +01:00
|
|
|
tkn = current_user.get_wopi_token
|
|
|
|
@token = tkn.token
|
|
|
|
@ttl = (tkn.ttl * 1000).to_s
|
2019-05-20 16:09:43 +02:00
|
|
|
@asset.step&.protocol&.update(updated_at: Time.now)
|
|
|
|
|
2016-09-29 15:30:55 +02:00
|
|
|
create_wopi_file_activity(current_user, true)
|
2016-10-03 20:02:13 +02:00
|
|
|
|
|
|
|
render layout: false
|
2016-08-10 17:49:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def view
|
2019-07-01 23:30:20 +02:00
|
|
|
@action_url = append_wd_params(@asset.get_action_url(current_user, 'view', false))
|
2016-09-29 12:19:29 +02:00
|
|
|
@favicon_url = @asset.favicon_url('view')
|
2016-11-30 16:48:42 +01:00
|
|
|
tkn = current_user.get_wopi_token
|
|
|
|
@token = tkn.token
|
|
|
|
@ttl = (tkn.ttl * 1000).to_s
|
2016-10-03 20:02:13 +02:00
|
|
|
|
|
|
|
render layout: false
|
2016-08-10 17:49:25 +02:00
|
|
|
end
|
|
|
|
|
2019-06-19 15:19:47 +02:00
|
|
|
def create_start_edit_image_activity
|
|
|
|
create_edit_image_activity(@asset, current_user, :start_editing)
|
2019-06-14 16:15:30 +02:00
|
|
|
end
|
|
|
|
|
2018-10-19 10:00:58 +02:00
|
|
|
def update_image
|
|
|
|
@asset = Asset.find(params[:id])
|
2019-07-01 23:30:20 +02:00
|
|
|
orig_file_size = @asset.file_size
|
|
|
|
orig_file_name = @asset.file_name
|
2018-10-19 10:00:58 +02:00
|
|
|
return render_403 unless can_read_team?(@asset.team)
|
2019-04-15 09:49:44 +02:00
|
|
|
|
2019-07-01 23:30:20 +02:00
|
|
|
@asset.file.attach(io: params.require(:image), filename: orig_file_name)
|
2018-10-19 10:00:58 +02:00
|
|
|
@asset.save!
|
2019-06-19 15:19:47 +02:00
|
|
|
create_edit_image_activity(@asset, current_user, :finish_editing)
|
2019-04-11 09:20:05 +02:00
|
|
|
# release previous image space
|
|
|
|
@asset.team.release_space(orig_file_size)
|
2018-10-19 10:00:58 +02:00
|
|
|
# Post process file here
|
|
|
|
@asset.post_process_file(@asset.team)
|
2019-05-20 16:09:43 +02:00
|
|
|
@asset.step&.protocol&.update(updated_at: Time.now)
|
2018-11-15 17:52:31 +01:00
|
|
|
|
2019-05-13 10:54:16 +02:00
|
|
|
render_html = if @asset.step
|
2019-07-03 15:35:49 +02:00
|
|
|
assets = @asset.step.assets
|
2019-08-09 14:56:00 +02:00
|
|
|
order_atoz = az_ordered_assets_index(@asset.step, @asset.id)
|
|
|
|
order_ztoa = assets.length - az_ordered_assets_index(@asset.step, @asset.id)
|
2019-05-13 10:54:16 +02:00
|
|
|
asset_position = @asset.step.asset_position(@asset)
|
|
|
|
render_to_string(
|
|
|
|
partial: 'steps/attachments/item.html.erb',
|
|
|
|
locals: {
|
|
|
|
asset: @asset,
|
|
|
|
i: asset_position[:pos],
|
|
|
|
assets_count: asset_position[:count],
|
2019-07-03 15:35:49 +02:00
|
|
|
step: @asset.step,
|
|
|
|
order_atoz: order_atoz,
|
|
|
|
order_ztoa: order_ztoa
|
2019-05-13 10:54:16 +02:00
|
|
|
},
|
|
|
|
formats: :html
|
|
|
|
)
|
2019-09-26 16:49:56 +02:00
|
|
|
elsif @asset.result
|
|
|
|
render_to_string(
|
|
|
|
partial: 'steps/attachments/item.html.erb',
|
|
|
|
locals: {
|
|
|
|
asset: @asset,
|
|
|
|
i: 0,
|
|
|
|
assets_count: 0,
|
|
|
|
step: nil,
|
|
|
|
order_atoz: 0,
|
|
|
|
order_ztoa: 0
|
|
|
|
},
|
|
|
|
formats: :html
|
|
|
|
)
|
2019-05-13 10:54:16 +02:00
|
|
|
else
|
|
|
|
render_to_string(
|
|
|
|
partial: 'shared/asset_link',
|
|
|
|
locals: { asset: @asset, display_image_tag: true },
|
|
|
|
formats: :html
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-11-15 17:52:31 +01:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2019-05-13 10:54:16 +02:00
|
|
|
render json: { html: render_html }
|
2018-11-15 17:52:31 +01:00
|
|
|
end
|
|
|
|
end
|
2018-10-19 10:00:58 +02:00
|
|
|
end
|
|
|
|
|
2019-03-17 19:23:17 +01:00
|
|
|
# POST: create_wopi_file_path
|
2019-03-15 20:59:15 +01:00
|
|
|
def create_wopi_file
|
2019-03-17 19:23:17 +01:00
|
|
|
# Presence validation
|
2019-03-21 08:47:52 +01:00
|
|
|
params.require(%i(element_type element_id file_type))
|
2019-03-19 10:19:27 +01:00
|
|
|
|
2019-03-17 19:23:17 +01:00
|
|
|
# File type validation
|
2019-03-19 10:19:27 +01:00
|
|
|
render_403 && return unless %w(docx xlsx pptx).include?(params[:file_type])
|
2019-03-17 19:23:17 +01:00
|
|
|
|
|
|
|
# Asset validation
|
2019-10-04 14:24:19 +02:00
|
|
|
asset = Asset.new(created_by: current_user, team: current_team)
|
2019-07-01 23:30:20 +02:00
|
|
|
asset.file.attach(io: StringIO.new,
|
|
|
|
filename: "#{params[:file_name]}.#{params[:file_type]}",
|
|
|
|
content_type: wopi_content_type(params[:file_type]))
|
2019-03-17 19:23:17 +01:00
|
|
|
|
2019-03-21 08:47:52 +01:00
|
|
|
unless asset.valid?(:wopi_file_creation)
|
|
|
|
render json: {
|
|
|
|
message: asset.errors
|
|
|
|
}, status: 400 and return
|
2019-03-19 10:19:27 +01:00
|
|
|
end
|
2019-03-17 19:23:17 +01:00
|
|
|
|
2019-03-19 10:19:27 +01:00
|
|
|
# Create file depending on the type
|
2019-03-17 19:23:17 +01:00
|
|
|
if params[:element_type] == 'Step'
|
|
|
|
step = Step.find(params[:element_id].to_i)
|
|
|
|
render_403 && return unless can_manage_protocol_in_module?(step.protocol) ||
|
2019-03-19 10:19:27 +01:00
|
|
|
can_manage_protocol_in_repository?(step.protocol)
|
2019-03-17 19:23:17 +01:00
|
|
|
step_asset = StepAsset.create!(step: step, asset: asset)
|
2019-05-20 16:09:43 +02:00
|
|
|
step.protocol&.update(updated_at: Time.now)
|
2019-03-17 19:23:17 +01:00
|
|
|
|
|
|
|
edit_url = edit_asset_url(step_asset.asset_id)
|
|
|
|
elsif params[:element_type] == 'Result'
|
|
|
|
my_module = MyModule.find(params[:element_id].to_i)
|
|
|
|
render_403 and return unless can_manage_module?(my_module)
|
|
|
|
|
2019-03-19 10:19:27 +01:00
|
|
|
# First create result and then the asset
|
2019-08-21 13:26:11 +02:00
|
|
|
result = Result.create(name: asset.file_name,
|
2019-03-17 19:23:17 +01:00
|
|
|
my_module: my_module,
|
|
|
|
user: current_user)
|
|
|
|
result_asset = ResultAsset.create!(result: result, asset: asset)
|
|
|
|
|
|
|
|
edit_url = edit_asset_url(result_asset.asset_id)
|
|
|
|
else
|
2019-03-21 08:47:52 +01:00
|
|
|
render_404 and return
|
2018-11-15 17:52:31 +01:00
|
|
|
end
|
2019-03-17 19:23:17 +01:00
|
|
|
|
2019-10-10 15:40:50 +02:00
|
|
|
# Prepare file preview in advance
|
|
|
|
asset.medium_preview.processed && asset.large_preview.processed
|
|
|
|
|
2019-03-19 10:19:27 +01:00
|
|
|
# Return edit url
|
2019-03-21 08:47:52 +01:00
|
|
|
render json: {
|
|
|
|
success: true,
|
|
|
|
edit_url: edit_url
|
|
|
|
}, status: :ok
|
2018-10-19 10:00:58 +02:00
|
|
|
end
|
|
|
|
|
2016-02-12 16:52:43 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def load_vars
|
|
|
|
@asset = Asset.find_by_id(params[:id])
|
2018-01-25 14:30:04 +01:00
|
|
|
return render_404 unless @asset
|
2016-02-12 16:52:43 +01:00
|
|
|
|
2019-05-13 16:53:58 +02:00
|
|
|
@assoc ||= @asset.step
|
|
|
|
@assoc ||= @asset.result
|
|
|
|
@assoc ||= @asset.repository_cell
|
2016-02-12 16:52:43 +01:00
|
|
|
|
2016-07-21 13:11:15 +02:00
|
|
|
if @assoc.class == Step
|
|
|
|
@protocol = @asset.step.protocol
|
2018-03-09 17:04:54 +01:00
|
|
|
elsif @assoc.class == Result
|
2016-07-21 13:11:15 +02:00
|
|
|
@my_module = @assoc.my_module
|
2018-05-21 15:48:48 +02:00
|
|
|
elsif @assoc.class == RepositoryCell
|
|
|
|
@repository = @assoc.repository_column.repository
|
2016-07-21 13:11:15 +02:00
|
|
|
end
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_read_permission
|
|
|
|
if @assoc.class == Step
|
2018-02-01 18:41:28 +01:00
|
|
|
render_403 && return unless can_read_protocol_in_module?(@protocol) ||
|
|
|
|
can_read_protocol_in_repository?(@protocol)
|
2016-02-12 16:52:43 +01:00
|
|
|
elsif @assoc.class == Result
|
2018-03-06 06:56:35 +01:00
|
|
|
render_403 and return unless can_read_experiment?(@my_module.experiment)
|
2018-03-09 17:04:54 +01:00
|
|
|
elsif @assoc.class == RepositoryCell
|
2019-07-17 16:00:49 +02:00
|
|
|
render_403 and return unless can_read_repository?(@repository)
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-27 18:14:19 +02:00
|
|
|
def check_edit_permission
|
|
|
|
if @assoc.class == Step
|
2018-02-01 18:41:28 +01:00
|
|
|
render_403 && return unless can_manage_protocol_in_module?(@protocol) ||
|
2018-02-15 18:46:29 +01:00
|
|
|
can_manage_protocol_in_repository?(@protocol)
|
2016-09-27 18:14:19 +02:00
|
|
|
elsif @assoc.class == Result
|
2018-02-09 16:14:40 +01:00
|
|
|
render_403 and return unless can_manage_module?(@my_module)
|
2018-03-09 17:04:54 +01:00
|
|
|
elsif @assoc.class == RepositoryCell
|
2019-07-12 16:43:54 +02:00
|
|
|
render_403 and return unless can_manage_repository_rows?(@repository)
|
2016-09-27 18:14:19 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-04 18:00:08 +02:00
|
|
|
def append_wd_params(url)
|
2019-05-22 10:34:16 +02:00
|
|
|
exclude_params = %w(wdPreviousSession wdPreviousCorrelation)
|
|
|
|
wd_params = params.as_json.select { |key, _value| key[/^wd.*/] && !(exclude_params.include? key) }.to_query
|
2019-05-21 15:53:34 +02:00
|
|
|
url + '&' + wd_params
|
2016-10-04 18:00:08 +02:00
|
|
|
end
|
|
|
|
|
2016-08-05 17:00:29 +02:00
|
|
|
def asset_params
|
2019-07-01 23:30:20 +02:00
|
|
|
params.permit(:file)
|
2016-08-05 17:00:29 +02:00
|
|
|
end
|
2017-03-13 13:20:49 +01:00
|
|
|
|
|
|
|
def asset_data_type(asset)
|
|
|
|
return 'wopi' if wopi_file?(asset)
|
2019-06-28 08:17:09 +02:00
|
|
|
return 'image' if asset.image?
|
2019-05-10 16:25:18 +02:00
|
|
|
|
2017-03-13 13:20:49 +01:00
|
|
|
'file'
|
|
|
|
end
|
2016-07-21 13:11:15 +02:00
|
|
|
end
|