2019-07-01 17:51:13 +08:00
|
|
|
# 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)
|
2019-07-15 23:17:14 +08:00
|
|
|
if params[:object_type] == 'TinyMceAsset'
|
2019-07-19 20:10:38 +08:00
|
|
|
asset = TinyMceAsset.new(team_id: current_team.id)
|
2019-07-15 23:17:14 +08:00
|
|
|
attach_file(asset.image, file, params)
|
2019-07-16 19:40:54 +08:00
|
|
|
asset.save!
|
|
|
|
return { asset: asset }
|
2019-07-15 23:17:14 +08:00
|
|
|
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!
|
2019-08-02 21:17:28 +08:00
|
|
|
asset.post_process_file(current_team)
|
2019-07-15 23:17:14 +08:00
|
|
|
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
|
|
|
|
|
2019-07-15 23:17:14 +08:00
|
|
|
def connect_asset(asset, params, current_user)
|
2023-09-05 22:29:15 +08:00
|
|
|
object = params[:object_type].constantize.find(params[:object_id])
|
|
|
|
asset.update!(view_mode: object.assets_view_mode)
|
|
|
|
object.assets << asset
|
2019-07-15 23:17:14 +08:00
|
|
|
|
|
|
|
{ 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-01 17:51:13 +08:00
|
|
|
|
2019-07-02 19:15:57 +08:00
|
|
|
def attach_file(asset, file, params)
|
2019-07-15 23:17:14 +08:00
|
|
|
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
|
2019-07-01 17:51:13 +08:00
|
|
|
end
|
|
|
|
end
|