From fcf9c72012e493d20e5f6dc15cd0a093356f85fb Mon Sep 17 00:00:00 2001 From: Alex Kriuchykhin Date: Wed, 2 Jul 2025 15:44:33 +0200 Subject: [PATCH] Fix asset preview generation, fix paths in production dockerfile [SCI-12075] (#8625) --- Dockerfile.production | 8 ++++---- app/helpers/active_storage_helper.rb | 4 ++-- app/jobs/active_storage/preview_job.rb | 7 ++++--- app/models/asset.rb | 4 ++-- config/application.rb | 3 --- 5 files changed, 12 insertions(+), 14 deletions(-) diff --git a/Dockerfile.production b/Dockerfile.production index 0c5f7c116..a209090b8 100644 --- a/Dockerfile.production +++ b/Dockerfile.production @@ -16,7 +16,7 @@ ENV APP_HOME /usr/src/app ENV RAILS_ENV=production ENV RAILS_SERVE_STATIC_FILES=true ENV GEM_HOME=$APP_HOME/vendor/bundle/ruby/3.2.0 -ENV PATH=$GEM_HOME/bin:/usr/share/nodejs/yarn/bin:$PATH +ENV PATH=$APP_HOME/bin:$GEM_HOME/bin:/usr/share/nodejs/yarn/bin:$PATH ENV BUNDLE_APP_CONFIG=.bundle ENV BUNDLE_BUILD__SASSC=--disable-march-tune-native @@ -39,7 +39,7 @@ RUN \ DATABASE_URL=postgresql://postgres@db/scinote_production \ SECRET_KEY_BASE=dummy \ DEFACE_ENABLED=true \ - bash -c "bundle exec rake assets:precompile && bundle exec rake deface:precompile && rm -rf ./tmp/cache" + bash -c "rails assets:precompile && rails deface:precompile && rm -rf ./tmp/cache" # Final stage FROM ruby:3.3-bookworm AS runner @@ -105,7 +105,7 @@ ENV APP_HOME /usr/src/app ENV RAILS_ENV=production ENV RAILS_SERVE_STATIC_FILES=true ENV GEM_HOME=$APP_HOME/vendor/bundle/ruby/3.2.0 -ENV PATH=$GEM_HOME/bin:$PATH +ENV PATH=$APP_HOME/bin:$GEM_HOME/bin:$PATH ENV BUNDLE_APP_CONFIG=.bundle RUN adduser --uid 1000 scinote @@ -115,4 +115,4 @@ COPY --from=builder --chown=scinote:scinote $APP_HOME $APP_HOME WORKDIR $APP_HOME -CMD bin/rails s -b 0.0.0.0 +CMD rails s -b 0.0.0.0 diff --git a/app/helpers/active_storage_helper.rb b/app/helpers/active_storage_helper.rb index 58ee7f4d6..02d06ce26 100644 --- a/app/helpers/active_storage_helper.rb +++ b/app/helpers/active_storage_helper.rb @@ -3,9 +3,9 @@ module ActiveStorageHelper def image_preview_format(blob) if ['image/jpeg', 'image/jpg'].include?(blob&.content_type) - :jpeg + 'jpeg' else - :png + 'png' end end end diff --git a/app/jobs/active_storage/preview_job.rb b/app/jobs/active_storage/preview_job.rb index d0843993b..0549455c5 100644 --- a/app/jobs/active_storage/preview_job.rb +++ b/app/jobs/active_storage/preview_job.rb @@ -20,16 +20,17 @@ class ActiveStorage::PreviewJob < ActiveStorage::BaseJob def perform(blob_id) blob = ActiveStorage::Blob.find(blob_id) - preview = blob.representation(resize_to_limit: Constants::MEDIUM_PIC_FORMAT, format: image_preview_format(blob)).processed + asset = blob.attachments.take.record + preview = asset.medium_preview.processed Rails.logger.info "Preview for the Blod with id: #{blob.id} - successfully generated.\n" \ "Transformations applied: #{preview.variation.transformations}" - preview = blob.representation(resize_to_limit: Constants::LARGE_PIC_FORMAT, format: image_preview_format(blob)).processed + preview = asset.large_preview.processed Rails.logger.info "Preview for the Blod with id: #{blob.id} - successfully generated.\n" \ "Transformations applied: #{preview.variation.transformations}" ActiveRecord::Base.no_touching do - blob.attachments.take.record.update(file_processing: false) + asset.update(file_processing: false) end end end diff --git a/app/models/asset.rb b/app/models/asset.rb index 071cb90e2..dfaee7281 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -105,11 +105,11 @@ class Asset < ApplicationRecord end def medium_preview - preview_attachment.representation(resize_to_limit: Constants::MEDIUM_PIC_FORMAT, format: image_preview_format(blob)) + preview_attachment.variant(resize_to_limit: Constants::MEDIUM_PIC_FORMAT, format: image_preview_format(blob)) end def large_preview - preview_attachment.representation(resize_to_limit: Constants::LARGE_PIC_FORMAT, format: image_preview_format(blob)) + preview_attachment.variant(resize_to_limit: Constants::LARGE_PIC_FORMAT, format: image_preview_format(blob)) end def file_name diff --git a/config/application.rb b/config/application.rb index 8ef8938cf..fd973052e 100644 --- a/config/application.rb +++ b/config/application.rb @@ -81,9 +81,6 @@ module Scinote config.x.export_all_limit_24h = (ENV['EXPORT_ALL_LIMIT_24_HOURS'] || 3).to_i - # Fallback to old variant behaviour (pre 7.2) - config.active_storage.track_variants = false - # SciNote Core Application version VERSION = File.read(Rails.root.join('VERSION')).strip.freeze