mirror of
https://github.com/lunaisnotaboy/mastodon.git
synced 2024-11-14 10:53:17 +00:00
85973f4f37
* Check if image value is nil? before creating an image Check if uploaded images aren't nil before creating SiteUpload models for them. * Validate presence of file in SiteUpload * Fix file presence validation * Fabricate SiteUpload#file * Add link to Creative Commons license
46 lines
1 KiB
Ruby
46 lines
1 KiB
Ruby
# frozen_string_literal: true
|
|
# == Schema Information
|
|
#
|
|
# Table name: site_uploads
|
|
#
|
|
# id :bigint(8) not null, primary key
|
|
# 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
|
|
#
|
|
|
|
class SiteUpload < ApplicationRecord
|
|
has_attached_file :file
|
|
|
|
validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/
|
|
validates :file, presence: true
|
|
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?
|
|
|
|
width, height = FastImage.size(tempfile.path)
|
|
self.meta = { width: width, height: height }
|
|
end
|
|
|
|
def clear_cache
|
|
Rails.cache.delete(cache_key)
|
|
end
|
|
end
|