diff --git a/apps/api/app/admin/images.rb b/apps/api/app/admin/images.rb index 4d06c4c..2bcfe61 100644 --- a/apps/api/app/admin/images.rb +++ b/apps/api/app/admin/images.rb @@ -4,7 +4,7 @@ ActiveAdmin.register Image do menu parent: "🌀 WarpEngine", priority: 5, label: "🖼️ Images" used_ids = -> { - WarpEngine::SoftwareImage.unscope(:order).distinct.pluck(:image_id) + + WarpEngine::SoftwareImage.distinct.pluck(:image_id) + Member.where.not(image_id: nil).distinct.pluck(:image_id) } @@ -28,7 +28,7 @@ ActiveAdmin.register Image do column :content_type column(:preview) do |img| if File.exist?(img.file_path) - image_tag("/api/image/#{img.id}", style: "max-height:60px;max-width:120px;object-fit:contain;") + image_tag(img.url, style: "max-height:60px;max-width:120px;object-fit:contain;") end end column(:usage) do |img| @@ -52,7 +52,7 @@ ActiveAdmin.register Image do row :content_type row(:preview) do |img| if File.exist?(img.file_path) - image_tag("/api/image/#{img.id}", style: "max-height:300px;max-width:100%;object-fit:contain;") + image_tag(img.url, style: "max-height:300px;max-width:100%;object-fit:contain;") else "File not found on disk" end diff --git a/apps/api/app/models/image.rb b/apps/api/app/models/image.rb index 07c80b6..23dfa8b 100644 --- a/apps/api/app/models/image.rb +++ b/apps/api/app/models/image.rb @@ -1,6 +1,6 @@ class Image < ApplicationRecord def self.upload_path - ENV.fetch("IMAGE_CONTAINER_PATH", "/images") + Rails.configuration.x.images.container_path end has_many :software_images, class_name: "WarpEngine::SoftwareImage", dependent: :restrict_with_error @@ -10,7 +10,8 @@ class Image < ApplicationRecord attr_accessor :file_upload - before_save :process_upload, if: -> { file_upload.present? } + before_validation :assign_upload_attributes, if: -> { file_upload.present? } + after_commit :store_upload_file, on: [ :create, :update ], if: -> { file_upload.present? } def self.ransackable_attributes(auth_object = nil) %w[content_type created_at deleted_at filename id original_filename updated_at] @@ -20,14 +21,18 @@ class Image < ApplicationRecord File.join(self.class.upload_path, filename.to_s) end + def url = "/api/image/#{id}" + private - def process_upload - FileUtils.mkdir_p(self.class.upload_path) + def assign_upload_attributes self.original_filename = file_upload.original_filename self.content_type = file_upload.content_type.presence || "application/octet-stream" - ext = File.extname(file_upload.original_filename) - self.filename = "#{SecureRandom.uuid}#{ext}" + self.filename = "#{SecureRandom.uuid}#{File.extname(file_upload.original_filename)}" + end + + def store_upload_file + FileUtils.mkdir_p(self.class.upload_path) IO.copy_stream(file_upload.to_io, file_path) end end diff --git a/libs/ruby/warp_engine/lib/generators/warp_engine/install/templates/image.rb b/libs/ruby/warp_engine/lib/generators/warp_engine/install/templates/image.rb index b2263dd..94fde24 100644 --- a/libs/ruby/warp_engine/lib/generators/warp_engine/install/templates/image.rb +++ b/libs/ruby/warp_engine/lib/generators/warp_engine/install/templates/image.rb @@ -9,7 +9,8 @@ class Image < ApplicationRecord attr_accessor :file_upload - before_save :process_upload, if: -> { file_upload.present? } + before_validation :assign_upload_attributes, if: -> { file_upload.present? } + after_commit :store_upload_file, on: [ :create, :update ], if: -> { file_upload.present? } def self.ransackable_attributes(auth_object = nil) %w[content_type created_at deleted_at filename id original_filename updated_at] @@ -19,14 +20,18 @@ class Image < ApplicationRecord File.join(self.class.upload_path, filename.to_s) end + def url = "/api/image/#{id}" + private - def process_upload - FileUtils.mkdir_p(self.class.upload_path) + def assign_upload_attributes self.original_filename = file_upload.original_filename self.content_type = file_upload.content_type.presence || "application/octet-stream" - ext = File.extname(file_upload.original_filename) - self.filename = "#{SecureRandom.uuid}#{ext}" + self.filename = "#{SecureRandom.uuid}#{File.extname(file_upload.original_filename)}" + end + + def store_upload_file + FileUtils.mkdir_p(self.class.upload_path) IO.copy_stream(file_upload.to_io, file_path) end end