Migration to Shrine attachment handling
This commit is contained in:
parent
2d53397dda
commit
c4f5741b0c
13 changed files with 175 additions and 181 deletions
|
|
@ -2,38 +2,21 @@ class Screenshot < ApplicationRecord
|
|||
belongs_to :package, inverse_of: :screenshots
|
||||
belongs_to :user, inverse_of: :screenshots
|
||||
|
||||
# Use paperclip gem to handle image files related to screenshots
|
||||
# After migration from Paperclip to ActiveStorage:
|
||||
has_one_attached :image
|
||||
|
||||
# Validate using 'active_storage_validations' gem
|
||||
validates :image, attached: true, content_type: { in: 'image/png', message: 'is not a valid PNG image file' }
|
||||
|
||||
# Before migration from Paperclip to ActiveStorage:
|
||||
# has_attached_file :image,
|
||||
# styles: { :large => '800x600>', :thumb => '160x120>' },
|
||||
# default_url: '/images/dummy/no-screenshots-available.svg',
|
||||
# path: ':rails_root/public/screenshots/:id_partition/:style.png',
|
||||
# url: '/screenshots/:id_partition/:style.png'
|
||||
# Paperclip
|
||||
has_attached_file :image,
|
||||
styles: { :large => '800x600>', :thumb => '160x120>' },
|
||||
default_url: '/images/dummy/no-screenshots-available.svg',
|
||||
path: ':rails_root/public/screenshots/:id_partition/:style.png',
|
||||
url: '/screenshots/:id_partition/:style.png'
|
||||
# validates_attachment_content_type :image, :content_type => 'image/png'
|
||||
# validates_with AttachmentSizeValidator, :attributes => :image, :less_than => 5.megabytes
|
||||
# # validate :validate_image_is_unique
|
||||
# validate :validate_image_is_unique
|
||||
# validates :delete_reason, length: { in: 5..100 }, allow_nil: true
|
||||
|
||||
# Validator that checks if the image has already been uploaded.
|
||||
# A generic uniqueness validator does not work because it would attribute
|
||||
# the error to the :image_fingerprint field and not the actual :image field.
|
||||
# Currently this check makes sure that the same screenshot is not uploaded
|
||||
# twice for the same package. However it allows the screenshot to be uploaded
|
||||
# for two different packages. Restricting that further may happen later.
|
||||
def validate_image_is_unique
|
||||
# Look for images with the same checksum / image_fingerprint
|
||||
if screenshot=Screenshot.find_by(image_fingerprint: image_fingerprint, package_id: self.package.id)
|
||||
unless screenshot.id == self.id
|
||||
errors.add(:image, "has already been uploaded for this package")
|
||||
end
|
||||
end
|
||||
end
|
||||
# Shrine
|
||||
# include PaperclipShrineSynchronization # needs to be after `has_attached_file`
|
||||
include ImageUploader::Attachment(:simage) # adds an `simage` virtual attribute
|
||||
|
||||
|
||||
# Calculate how many days ago this screenshot has been uploaded
|
||||
def age_days
|
||||
|
|
@ -180,4 +163,32 @@ class Screenshot < ApplicationRecord
|
|||
).processed
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Validator that checks if the image has already been uploaded.
|
||||
# A generic uniqueness validator does not work because it would attribute
|
||||
# the error to the :image_fingerprint field and not the actual :image field.
|
||||
# Currently this check makes sure that the same screenshot is not uploaded
|
||||
# twice for the same package. However it allows the screenshot to be uploaded
|
||||
# for two different packages. Restricting that further may happen later.
|
||||
def validate_image_is_unique
|
||||
# Look for images with the same checksum / image_fingerprint
|
||||
if screenshot=Screenshot.find_by(image_fingerprint: image_fingerprint, package_id: self.package.id)
|
||||
unless screenshot.id == self.id
|
||||
errors.add(:image, "has already been uploaded for this package")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Check that the uploaded file can be handled by ImageMagick and is not broken
|
||||
# def image_intact?
|
||||
# begin
|
||||
# #MiniMagick::Image.open(self.image.blob.open)
|
||||
# MiniMagick::Image.read(self.image.download)
|
||||
# rescue MiniMagick::Invalid => exc
|
||||
# error.add(:image, 'appears to be a broken image file')
|
||||
# end
|
||||
# end
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue