2019-03-11 20:43:50 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-19 15:13:16 +08:00
|
|
|
class TinyMceAssetsController < ApplicationController
|
2019-08-09 15:47:07 +08:00
|
|
|
include MarvinJsActions
|
2019-11-19 22:35:31 +08:00
|
|
|
include ActiveStorage::SetCurrent
|
2019-08-09 15:47:07 +08:00
|
|
|
|
2019-07-19 20:10:38 +08:00
|
|
|
before_action :load_vars, only: %i(marvinjs_show marvinjs_update download)
|
2019-07-16 21:51:44 +08:00
|
|
|
|
2019-07-19 20:10:38 +08:00
|
|
|
before_action :check_read_permission, only: %i(marvinjs_show marvinjs_update download)
|
2019-07-16 21:51:44 +08:00
|
|
|
before_action :check_edit_permission, only: %i(marvinjs_update)
|
|
|
|
|
2017-04-19 15:13:16 +08:00
|
|
|
def create
|
2017-04-21 22:09:04 +08:00
|
|
|
image = params.fetch(:file) { render_404 }
|
2019-12-16 23:05:40 +08:00
|
|
|
unless image.content_type.match?(%r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES)}})
|
|
|
|
return render json: {
|
|
|
|
errors: [I18n.t('tiny_mce.unsupported_image_format')]
|
|
|
|
}, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
2020-03-03 22:55:17 +08:00
|
|
|
if image.size > Rails.configuration.x.file_max_size_mb.megabytes
|
|
|
|
return render json: {
|
|
|
|
errors: [t('general.file.size_exceeded', file_size: Rails.configuration.x.file_max_size_mb)]
|
|
|
|
}, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
2019-07-05 22:56:05 +08:00
|
|
|
tiny_img = TinyMceAsset.new(team_id: current_team.id, saved: false)
|
|
|
|
|
|
|
|
tiny_img.transaction do
|
|
|
|
tiny_img.save!
|
|
|
|
tiny_img.image.attach(io: image, filename: image.original_filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
if tiny_img.persisted?
|
2017-04-21 22:09:04 +08:00
|
|
|
render json: {
|
2017-04-24 22:22:25 +08:00
|
|
|
image: {
|
2019-07-05 22:56:05 +08:00
|
|
|
url: url_for(tiny_img.image),
|
2017-04-24 22:22:25 +08:00
|
|
|
token: Base62.encode(tiny_img.id)
|
|
|
|
}
|
2017-04-21 22:09:04 +08:00
|
|
|
}, content_type: 'text/html'
|
|
|
|
else
|
2017-04-24 22:22:25 +08:00
|
|
|
render json: {
|
2019-12-16 23:05:40 +08:00
|
|
|
errors: tiny_img.errors.full_messages
|
2017-04-24 22:22:25 +08:00
|
|
|
}, status: :unprocessable_entity
|
2017-04-21 22:09:04 +08:00
|
|
|
end
|
2017-04-19 15:13:16 +08:00
|
|
|
end
|
2019-07-16 19:40:54 +08:00
|
|
|
|
|
|
|
def download
|
2019-07-19 20:10:38 +08:00
|
|
|
if @asset&.image&.attached?
|
|
|
|
redirect_to rails_blob_path(@asset.image, disposition: 'attachment')
|
2019-07-16 19:40:54 +08:00
|
|
|
else
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|
2019-07-16 21:51:44 +08:00
|
|
|
|
|
|
|
def marvinjs_show
|
2019-07-19 15:27:03 +08:00
|
|
|
asset = current_team.tiny_mce_assets.find_by_id(Base62.decode(params[:id]))
|
2019-07-16 21:51:44 +08:00
|
|
|
return render_404 unless asset
|
|
|
|
|
2019-08-09 15:47:07 +08:00
|
|
|
create_edit_marvinjs_activity(asset, current_user, :start_editing) if params[:show_action] == 'start_edit'
|
|
|
|
|
2019-07-16 21:51:44 +08:00
|
|
|
render json: {
|
|
|
|
name: asset.image.metadata[:name],
|
|
|
|
description: asset.image.metadata[:description]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def marvinjs_create
|
2019-07-19 20:10:38 +08:00
|
|
|
result = MarvinJsService.create_sketch(marvin_params, current_user, current_team)
|
2019-07-16 21:51:44 +08:00
|
|
|
if result[:asset]
|
|
|
|
render json: {
|
|
|
|
image: {
|
|
|
|
url: rails_representation_url(result[:asset].preview),
|
|
|
|
token: Base62.encode(result[:asset].id),
|
|
|
|
source_type: result[:asset].image.metadata[:asset_type]
|
|
|
|
}
|
|
|
|
}, content_type: 'text/html'
|
|
|
|
else
|
|
|
|
render json: result[:asset].errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def marvinjs_update
|
2019-07-19 20:10:38 +08:00
|
|
|
asset = MarvinJsService.update_sketch(marvin_params, current_user, current_team)
|
2019-07-16 21:51:44 +08:00
|
|
|
if asset
|
2019-08-09 15:47:07 +08:00
|
|
|
create_edit_marvinjs_activity(asset, current_user, :finish_editing)
|
2019-07-16 21:51:44 +08:00
|
|
|
render json: { url: rails_representation_url(asset.preview), id: asset.id }
|
|
|
|
else
|
|
|
|
render json: { error: t('marvinjs.no_sketches_found') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_vars
|
2019-07-19 15:27:03 +08:00
|
|
|
@asset = current_team.tiny_mce_assets.find_by_id(Base62.decode(params[:id]))
|
2019-07-16 21:51:44 +08:00
|
|
|
return render_404 unless @asset
|
|
|
|
|
2019-07-19 20:10:38 +08:00
|
|
|
@assoc = @asset.object
|
2019-07-16 21:51:44 +08:00
|
|
|
|
|
|
|
if @assoc.class == Step
|
|
|
|
@protocol = @assoc.protocol
|
|
|
|
elsif @assoc.class == Protocol
|
|
|
|
@protocol = @assoc
|
|
|
|
elsif @assoc.class == MyModule
|
|
|
|
@my_module = @assoc
|
2019-07-19 20:10:38 +08:00
|
|
|
elsif @assoc.class == ResultText
|
|
|
|
@my_module = @assoc.result.my_module
|
2019-07-16 21:51:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_read_permission
|
|
|
|
if @assoc.class == Step || @assoc.class == Protocol
|
2019-07-19 20:10:38 +08:00
|
|
|
return render_403 unless can_read_protocol_in_module?(@protocol) ||
|
|
|
|
can_read_protocol_in_repository?(@protocol)
|
|
|
|
elsif @assoc.class == ResultText || @assoc.class == MyModule
|
|
|
|
return render_403 unless can_read_experiment?(@my_module.experiment)
|
2019-08-07 16:28:42 +08:00
|
|
|
elsif @assoc.nil?
|
|
|
|
return render_403 unless current_team == @asset.team
|
2019-07-19 20:10:38 +08:00
|
|
|
else
|
|
|
|
render_403
|
2019-07-16 21:51:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_edit_permission
|
|
|
|
if @assoc.class == Step || @assoc.class == Protocol
|
2019-07-19 20:10:38 +08:00
|
|
|
return render_403 unless can_manage_protocol_in_module?(@protocol) ||
|
|
|
|
can_manage_protocol_in_repository?(@protocol)
|
|
|
|
elsif @assoc.class == ResultText || @assoc.class == MyModule
|
|
|
|
return render_403 unless can_manage_module?(@my_module)
|
2019-08-07 16:28:42 +08:00
|
|
|
elsif @assoc.nil?
|
|
|
|
return render_403 unless current_team == @asset.team
|
2019-07-19 20:10:38 +08:00
|
|
|
else
|
|
|
|
render_403
|
2019-07-16 21:51:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def marvin_params
|
|
|
|
params.permit(:id, :description, :object_id, :object_type, :name, :image)
|
|
|
|
end
|
2017-04-19 15:13:16 +08:00
|
|
|
end
|