2017-09-14 00:04:30 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: site_uploads
|
|
|
|
#
|
2018-04-23 11:29:17 +02:00
|
|
|
# id :bigint(8) not null, primary key
|
2017-09-14 00:04:30 +02:00
|
|
|
# var :string default(""), not null
|
|
|
|
# file_file_name :string
|
|
|
|
# file_content_type :string
|
2020-12-30 17:54:44 +01:00
|
|
|
# file_file_size :bigint(8)
|
2017-09-14 00:04:30 +02:00
|
|
|
# file_updated_at :datetime
|
|
|
|
# meta :json
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
|
|
|
|
class SiteUpload < ApplicationRecord
|
|
|
|
has_attached_file :file
|
|
|
|
|
|
|
|
validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/
|
2019-04-01 07:30:46 +02:00
|
|
|
validates :file, presence: true
|
2017-09-14 00:04:30 +02: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 03:40:12 +01:00
|
|
|
width, height = FastImage.size(tempfile.path)
|
|
|
|
self.meta = { width: width, height: height }
|
2017-09-14 00:04:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cache
|
|
|
|
Rails.cache.delete(cache_key)
|
|
|
|
end
|
|
|
|
end
|