mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-01-09 00:49:07 +08:00
92 lines
2.2 KiB
Ruby
92 lines
2.2 KiB
Ruby
class AssetsController < ApplicationController
|
|
include ActionView::Helpers::TextHelper
|
|
before_action :load_vars
|
|
before_action :check_read_permission, except: :file_present
|
|
|
|
def file_present
|
|
respond_to do |format|
|
|
format.json do
|
|
if @asset.file.processing?
|
|
render json: {}, status: 404
|
|
else
|
|
# Only if file is present,
|
|
# check_read_permission
|
|
check_read_permission
|
|
|
|
# If check_read_permission already rendered error,
|
|
# stop execution
|
|
return if performed?
|
|
|
|
# If check permission passes, return :ok
|
|
render json: {
|
|
'preview-url' => @asset.url(:medium),
|
|
'filename' => truncate(@asset.file_file_name,
|
|
length:
|
|
Constants::FILENAME_TRUNCATION_LENGTH),
|
|
'download-url' => download_asset_path(@asset),
|
|
'type' => (@asset.is_image? ? 'image' : 'file')
|
|
}, status: 200
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def preview
|
|
if @asset.is_image?
|
|
redirect_to @asset.url(:medium), status: 307
|
|
else
|
|
render_400
|
|
end
|
|
end
|
|
|
|
def download
|
|
if !@asset.file_present
|
|
render_404 and return
|
|
elsif @asset.file.is_stored_on_s3?
|
|
redirect_to @asset.presigned_url(download: true), status: 307
|
|
else
|
|
send_file @asset.file.path, filename: URI.unescape(@asset.file_file_name),
|
|
type: @asset.file_content_type
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def load_vars
|
|
@asset = Asset.find_by_id(params[:id])
|
|
|
|
unless @asset
|
|
render_404
|
|
end
|
|
|
|
step_assoc = @asset.step
|
|
result_assoc = @asset.result
|
|
|
|
@assoc = step_assoc if not step_assoc.nil?
|
|
@assoc = result_assoc if not result_assoc.nil?
|
|
|
|
if @assoc.class == Step
|
|
@protocol = @asset.step.protocol
|
|
else
|
|
@my_module = @assoc.my_module
|
|
end
|
|
end
|
|
|
|
def check_read_permission
|
|
if @assoc.class == Step
|
|
unless can_view_or_download_step_assets(@protocol)
|
|
render_403 and return
|
|
end
|
|
elsif @assoc.class == Result
|
|
unless can_view_or_download_result_assets(@my_module)
|
|
render_403 and return
|
|
end
|
|
end
|
|
end
|
|
|
|
def asset_params
|
|
params.permit(
|
|
:file
|
|
)
|
|
end
|
|
end
|