scinote-web/app/services/marvin_js_service.rb

102 lines
2.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class MarvinJsService
2019-07-02 19:15:57 +08:00
class << self
def url
ENV['MARVINJS_URL']
end
def enabled?
!ENV['MARVINJS_URL'].nil? || !ENV['MARVINJS_API_KEY'].nil?
end
2019-07-19 20:10:38 +08:00
def create_sketch(params, current_user, current_team)
2019-07-02 19:15:57 +08:00
file = generate_image(params)
if params[:object_type] == 'TinyMceAsset'
2019-07-19 20:10:38 +08:00
asset = TinyMceAsset.new(team_id: current_team.id)
attach_file(asset.image, file, params)
2019-07-16 19:40:54 +08:00
asset.save!
return { asset: asset }
end
2019-07-16 19:40:54 +08:00
asset = Asset.new(created_by: current_user,
last_modified_by: current_user,
2019-07-19 20:10:38 +08:00
team_id: current_team.id)
2019-07-16 19:40:54 +08:00
attach_file(asset.file, file, params)
2019-07-02 19:15:57 +08:00
asset.save!
asset.post_process_file
connect_asset(asset, params, current_user)
2019-07-02 19:15:57 +08:00
end
def update_sketch(params, current_user, current_team)
2019-07-16 19:40:54 +08:00
if params[:object_type] == 'TinyMceAsset'
2019-07-19 20:10:38 +08:00
asset = current_team.tiny_mce_assets.find(Base62.decode(params[:id]))
2019-07-16 19:40:54 +08:00
attachment = asset&.image
else
2019-07-19 20:10:38 +08:00
asset = current_team.assets.find(params[:id])
2019-07-16 19:40:54 +08:00
attachment = asset&.file
end
return unless attachment
2019-07-02 19:15:57 +08:00
file = generate_image(params)
asset.update(last_modified_by: current_user) if asset.is_a?(Asset)
attach_file(attachment, file, params)
asset.post_process_file if asset.instance_of?(Asset)
2019-07-02 19:15:57 +08:00
asset
end
def update_file_name(new_name, asset_id, current_user, current_team)
asset = current_team.assets.find(asset_id)
prepared_name = prepare_name(new_name)
ActiveRecord::Base.transaction do
asset.last_modified_by = current_user
asset.rename_file(prepared_name)
asset.save!
end
asset
end
2019-07-02 19:15:57 +08:00
private
def connect_asset(asset, params, current_user)
2023-09-06 20:36:26 +08:00
object = case params[:object_type]
when 'Step'
Step.find(params[:object_id])
when 'Result'
Result.find(params[:object_id])
end
asset.update!(view_mode: object.assets_view_mode)
object.assets << asset
{ asset: asset, object: object }
2019-07-02 19:15:57 +08:00
end
def generate_image(params)
2019-07-18 20:29:24 +08:00
StringIO.new(Base64.decode64(params[:image].split(',')[1]))
2019-07-02 19:15:57 +08:00
end
2019-07-02 19:15:57 +08:00
def attach_file(asset, file, params)
asset.attach(
2019-07-02 19:15:57 +08:00
io: file,
2019-07-16 19:40:54 +08:00
filename: "#{prepare_name(params[:name])}.jpg",
2019-07-02 19:15:57 +08:00
content_type: 'image/jpeg',
metadata: {
2019-07-16 19:40:54 +08:00
name: prepare_name(params[:name]),
2019-07-02 19:15:57 +08:00
description: params[:description],
asset_type: 'marvinjs'
}
)
end
2019-07-16 19:40:54 +08:00
def prepare_name(sketch_name)
2021-07-23 17:56:28 +08:00
if !sketch_name.blank?
2019-07-16 19:40:54 +08:00
sketch_name
else
I18n.t('marvinjs.new_sketch')
end
end
end
end