scinote-web/app/services/marvin_js_service.rb

92 lines
2.6 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(current_team)
connect_asset(asset, params, current_user)
2019-07-02 19:15:57 +08:00
end
2019-07-19 20:10:38 +08:00
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)
2019-07-16 19:40:54 +08:00
attach_file(attachment, file, params)
2019-09-23 22:33:57 +08:00
asset.post_process_file(current_team) if asset.class == Asset
2019-07-02 19:15:57 +08:00
asset
end
private
def connect_asset(asset, params, current_user)
if params[:object_type] == 'Step'
object = params[:object_type].constantize.find(params[:object_id])
object.assets << asset
elsif params[:object_type] == 'Result'
2019-09-23 22:33:57 +08:00
my_module = MyModule.find_by(id: params[:object_id])
return unless my_module
object = Result.create(user: current_user,
my_module: my_module,
2019-07-16 19:40:54 +08:00
name: prepare_name(params[:name]),
asset: asset,
last_modified_by: current_user)
end
{ 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)
if !sketch_name.empty?
sketch_name
else
I18n.t('marvinjs.new_sketch')
end
end
end
end