2017-09-14 01:04:30 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: site_uploads
|
|
|
|
#
|
2018-04-23 12:29:17 +03:00
|
|
|
# id :bigint(8) not null, primary key
|
2017-09-14 01:04:30 +03:00
|
|
|
# var :string default(""), not null
|
|
|
|
# file_file_name :string
|
|
|
|
# file_content_type :string
|
|
|
|
# file_file_size :integer
|
|
|
|
# file_updated_at :datetime
|
|
|
|
# meta :json
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2022-10-13 12:29:19 +03:00
|
|
|
# blurhash :string
|
2017-09-14 01:04:30 +03:00
|
|
|
#
|
|
|
|
|
|
|
|
class SiteUpload < ApplicationRecord
|
2022-10-13 12:29:19 +03:00
|
|
|
include Attachmentable
|
|
|
|
|
|
|
|
STYLES = {
|
|
|
|
thumbnail: {
|
|
|
|
'@1x': {
|
|
|
|
format: 'png',
|
|
|
|
geometry: '1200x630#',
|
|
|
|
file_geometry_parser: FastGeometryParser,
|
|
|
|
blurhash: {
|
|
|
|
x_comp: 4,
|
|
|
|
y_comp: 4,
|
|
|
|
}.freeze,
|
|
|
|
},
|
|
|
|
|
|
|
|
'@2x': {
|
|
|
|
format: 'png',
|
|
|
|
geometry: '2400x1260#',
|
|
|
|
file_geometry_parser: FastGeometryParser,
|
|
|
|
}.freeze,
|
|
|
|
}.freeze,
|
|
|
|
|
|
|
|
mascot: {}.freeze,
|
|
|
|
}.freeze
|
|
|
|
|
2022-11-11 10:20:10 +02:00
|
|
|
has_attached_file :file, styles: ->(file) { STYLES[file.instance.var.to_sym] }, convert_options: { all: '-coalesce +profile "!icc,*" +set modify-date +set create-date' }, processors: [:lazy_thumbnail, :blurhash_transcoder, :type_corrector]
|
2017-09-14 01:04:30 +03:00
|
|
|
|
|
|
|
validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/
|
2019-04-01 08:30:46 +03:00
|
|
|
validates :file, presence: true
|
2017-09-14 01:04:30 +03:00
|
|
|
validates :var, presence: true, uniqueness: true
|
|
|
|
|
|
|
|
before_save :set_meta
|
|
|
|
after_commit :clear_cache
|
|
|
|
|
|
|
|
def cache_key
|
|
|
|
"site_uploads/#{var}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_meta
|
|
|
|
tempfile = file.queued_for_write[:original]
|
|
|
|
|
|
|
|
return if tempfile.nil?
|
|
|
|
|
2018-02-21 04:40:12 +02:00
|
|
|
width, height = FastImage.size(tempfile.path)
|
|
|
|
self.meta = { width: width, height: height }
|
2017-09-14 01:04:30 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cache
|
|
|
|
Rails.cache.delete(cache_key)
|
|
|
|
end
|
|
|
|
end
|