mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-10-09 13:28:53 +08:00
Add client for ZPL preview generation service [SCI-7049]
This commit is contained in:
parent
4e768bec04
commit
f02f625e1e
4 changed files with 47 additions and 2 deletions
1
Gemfile
1
Gemfile
|
@ -89,6 +89,7 @@ gem 'underscore-rails'
|
||||||
gem 'wicked_pdf'
|
gem 'wicked_pdf'
|
||||||
gem 'wkhtmltopdf-heroku', '2.12.5'
|
gem 'wkhtmltopdf-heroku', '2.12.5'
|
||||||
|
|
||||||
|
gem 'aws-sdk-lambda'
|
||||||
gem 'aws-sdk-rails'
|
gem 'aws-sdk-rails'
|
||||||
gem 'aws-sdk-s3'
|
gem 'aws-sdk-s3'
|
||||||
gem 'delayed_job_active_record'
|
gem 'delayed_job_active_record'
|
||||||
|
|
|
@ -137,6 +137,9 @@ GEM
|
||||||
aws-sdk-kms (1.41.0)
|
aws-sdk-kms (1.41.0)
|
||||||
aws-sdk-core (~> 3, >= 3.109.0)
|
aws-sdk-core (~> 3, >= 3.109.0)
|
||||||
aws-sigv4 (~> 1.1)
|
aws-sigv4 (~> 1.1)
|
||||||
|
aws-sdk-lambda (1.57.0)
|
||||||
|
aws-sdk-core (~> 3, >= 3.109.0)
|
||||||
|
aws-sigv4 (~> 1.1)
|
||||||
aws-sdk-rails (3.6.0)
|
aws-sdk-rails (3.6.0)
|
||||||
aws-record (~> 2)
|
aws-record (~> 2)
|
||||||
aws-sdk-ses (~> 1)
|
aws-sdk-ses (~> 1)
|
||||||
|
@ -634,6 +637,7 @@ DEPENDENCIES
|
||||||
auto_strip_attributes (~> 2.1)
|
auto_strip_attributes (~> 2.1)
|
||||||
autosize-rails
|
autosize-rails
|
||||||
awesome_print
|
awesome_print
|
||||||
|
aws-sdk-lambda
|
||||||
aws-sdk-rails
|
aws-sdk-rails
|
||||||
aws-sdk-s3
|
aws-sdk-s3
|
||||||
base62
|
base62
|
||||||
|
|
|
@ -4,10 +4,10 @@ class LabelTemplatesController < ApplicationController
|
||||||
include InputSanitizeHelper
|
include InputSanitizeHelper
|
||||||
|
|
||||||
before_action :check_feature_enabled
|
before_action :check_feature_enabled
|
||||||
before_action :check_view_permissions, only: %i(index datatable)
|
before_action :check_view_permissions, except: %i(create duplicate set_default delete update)
|
||||||
before_action :check_manage_permissions, only: %i(create duplicate set_default delete update)
|
before_action :check_manage_permissions, only: %i(create duplicate set_default delete update)
|
||||||
before_action :load_label_templates, only: %i(index datatable)
|
before_action :load_label_templates, only: %i(index datatable)
|
||||||
before_action :load_label_template, only: %i(show set_default update)
|
before_action :load_label_template, only: %i(show set_default update zpl_preview)
|
||||||
|
|
||||||
layout 'fluid'
|
layout 'fluid'
|
||||||
|
|
||||||
|
@ -89,6 +89,17 @@ class LabelTemplatesController < ApplicationController
|
||||||
render json: LabelTemplates::TagService.new(current_team).tags
|
render json: LabelTemplates::TagService.new(current_team).tags
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def zpl_preview
|
||||||
|
service = LabelTemplatesPreviewService.new(@label_template.content, current_user, {})
|
||||||
|
payload = service.generate_zpl_preview!
|
||||||
|
|
||||||
|
if service.error.blank?
|
||||||
|
render json: { base64_preview: payload.string }
|
||||||
|
else
|
||||||
|
render json: { error: I18n.t('errors.general') }, status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def check_feature_enabled
|
def check_feature_enabled
|
||||||
|
|
29
app/services/label_templates_preview_service.rb
Normal file
29
app/services/label_templates_preview_service.rb
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class LabelTemplatesPreviewService
|
||||||
|
extend Service
|
||||||
|
|
||||||
|
attr_reader :error, :preview
|
||||||
|
|
||||||
|
def initialize(zpl, user, params)
|
||||||
|
@zpl = zpl
|
||||||
|
@user = user
|
||||||
|
@params = params
|
||||||
|
end
|
||||||
|
|
||||||
|
def generate_zpl_preview!
|
||||||
|
client = Aws::Lambda::Client.new(region: ENV['AWS_REGION'])
|
||||||
|
resp = client.invoke(
|
||||||
|
function_name: 'BinaryKitsZplViewer',
|
||||||
|
invocation_type: 'RequestResponse',
|
||||||
|
log_type: 'Tail',
|
||||||
|
payload: "{ \"content\": #{@zpl.to_json} }"
|
||||||
|
)
|
||||||
|
|
||||||
|
if resp.function_error.nil?
|
||||||
|
@preview = resp.payload
|
||||||
|
else
|
||||||
|
@error = resp.function_error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Reference in a new issue