2017-09-14 07:04:30 +09:00
|
|
|
# frozen_string_literal: true
|
2023-02-20 14:58:28 +09:00
|
|
|
|
2017-09-14 07:04:30 +09:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: site_uploads
|
|
|
|
#
|
2018-04-23 18:29:17 +09:00
|
|
|
# id :bigint(8) not null, primary key
|
2017-09-14 07:04:30 +09: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 18:29:19 +09:00
|
|
|
# blurhash :string
|
2017-09-14 07:04:30 +09:00
|
|
|
#
|
|
|
|
|
|
|
|
class SiteUpload < ApplicationRecord
|
2022-10-13 18:29:19 +09:00
|
|
|
include Attachmentable
|
|
|
|
|
2024-05-07 00:06:52 +09:00
|
|
|
FAVICON_SIZES = [16, 32, 48].freeze
|
|
|
|
APPLE_ICON_SIZES = [57, 60, 72, 76, 114, 120, 144, 152, 167, 180, 1024].freeze
|
|
|
|
ANDROID_ICON_SIZES = [36, 48, 72, 96, 144, 192, 256, 384, 512].freeze
|
|
|
|
|
|
|
|
APP_ICON_SIZES = (APPLE_ICON_SIZES + ANDROID_ICON_SIZES).uniq.freeze
|
|
|
|
|
2022-10-13 18:29:19 +09:00
|
|
|
STYLES = {
|
2024-05-08 03:15:17 +09:00
|
|
|
app_icon:
|
|
|
|
APP_ICON_SIZES.to_h do |size|
|
|
|
|
[:"#{size}", { format: 'png', geometry: "#{size}x#{size}#", file_geometry_parser: FastGeometryParser }]
|
|
|
|
end.freeze,
|
|
|
|
|
2024-06-26 22:44:08 +09:00
|
|
|
favicon:
|
2024-05-08 03:15:17 +09:00
|
|
|
FAVICON_SIZES.to_h do |size|
|
|
|
|
[:"#{size}", { format: 'png', geometry: "#{size}x#{size}#", file_geometry_parser: FastGeometryParser }]
|
2024-06-26 22:44:08 +09:00
|
|
|
end.freeze,
|
2024-05-08 03:15:17 +09:00
|
|
|
|
2022-10-13 18:29:19 +09:00
|
|
|
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
|
|
|
|
|
2023-09-26 02:21:07 +09:00
|
|
|
has_attached_file :file, styles: ->(file) { STYLES[file.instance.var.to_sym] }, convert_options: { all: '-coalesce +profile "!icc,*" +set date:modify +set date:create +set date:timestamp' }, processors: [:lazy_thumbnail, :blurhash_transcoder, :type_corrector]
|
2017-09-14 07:04:30 +09:00
|
|
|
|
2023-06-06 21:50:51 +09:00
|
|
|
validates_attachment_content_type :file, content_type: %r{\Aimage/.*\z}
|
2019-04-01 14:30:46 +09:00
|
|
|
validates :file, presence: true
|
2017-09-14 07:04:30 +09: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 11:40:12 +09:00
|
|
|
width, height = FastImage.size(tempfile.path)
|
|
|
|
self.meta = { width: width, height: height }
|
2017-09-14 07:04:30 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cache
|
|
|
|
Rails.cache.delete(cache_key)
|
|
|
|
end
|
|
|
|
end
|