Class: Image::UploadPdfToImagekit
- Inherits:
-
BaseService
- Object
- BaseService
- Image::UploadPdfToImagekit
- Defined in:
- app/services/image/upload_pdf_to_imagekit.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ UploadPdfToImagekit
constructor
A new instance of UploadPdfToImagekit.
- #process(upload, target_format: 'png') ⇒ Object
Methods inherited from BaseService
#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger
Constructor Details
#initialize(options = {}) ⇒ UploadPdfToImagekit
Returns a new instance of UploadPdfToImagekit.
12 13 14 |
# File 'app/services/image/upload_pdf_to_imagekit.rb', line 12 def initialize( = {}) super end |
Class Method Details
.migrate ⇒ Object
2 3 4 5 6 7 8 9 10 |
# File 'app/services/image/upload_pdf_to_imagekit.rb', line 2 def self.migrate iku = new uploads = Upload.joins(:item).includes(:item).where.not('items.id IS NULL').where(thumbnail_asset: nil) uploads.each do |u| next unless u.item iku.process(u) end end |
Instance Method Details
#process(upload, target_format: 'png') ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/services/image/upload_pdf_to_imagekit.rb', line 16 def process(upload, target_format: 'png') return unless upload.is_pdf? return if upload.thumbnail_asset.present? puts "Processing upload #{upload.id}" orig_file_name = "upload_#{upload.id}.pdf" tn_file_name = if upload.item "#{upload.item.sku.downcase}.#{target_format}" else "upload_#{upload.id}.#{target_format}" end # gs_path = `which gs`.gsub("\n",'') Dir.mktmpdir do |dir| Dir.chdir(dir) do puts "temp directory #{dir} created" upload..to_file(orig_file_name) puts "Original pdf downloaded to #{dir}/#{orig_file_name}" # Extract cmd_format = case target_format.to_s when 'jpg', 'jpeg' '-sDEVICE=jpeg -r300 -dDownScaleFactor=2' else '-sDEVICE=png16m -r300 -dDownScaleFactor=2' end cmd = %(gs -dBATCH -dNOPAUSE #{cmd_format} -dLastPage=1 -sOutputFile=#{tn_file_name} #{orig_file_name}) puts "Executing command: #{cmd}" require 'open3' Open3.popen2e(cmd) do |_, stdout_and_stderr, wait_thr| puts stdout_and_stderr.read status = wait_thr.value end local_file_path = "#{dir}/#{tn_file_name}" if File.exist?(local_file_path) puts "Uploading #{local_file_path} to ik" File.open(local_file_path, 'rb') do |file| # Use ImageKitFactory helper method to upload file res = ImageKitFactory.upload_file( file: file, file_name: tn_file_name, use_unique_file_name: false, folder: 'pdf/' ) upload.thumbnail_asset = res&.to_h&.deep_symbolize_keys rescue StandardError => e puts "Upload failed: #{e.}" end else puts 'Error creating file' end end end upload.update_column(:thumbnail_asset, upload.thumbnail_asset) if upload.thumbnail_asset.present? upload.thumbnail_asset end |