scinote-web/app/models/tiny_mce_asset.rb

79 lines
2.5 KiB
Ruby
Raw Normal View History

2017-04-21 22:09:04 +08:00
class TinyMceAsset < ActiveRecord::Base
attr_accessor :reference
before_create :set_reference
after_create :update_estimated_size
2017-04-25 19:44:31 +08:00
after_destroy :release_team_space
2017-04-21 22:09:04 +08:00
belongs_to :team, inverse_of: :tiny_mce_assets
belongs_to :step, inverse_of: :tiny_mce_assets
belongs_to :result_text, inverse_of: :tiny_mce_assets
2017-04-21 22:09:04 +08:00
has_attached_file :image,
2017-04-24 22:22:25 +08:00
styles: { large: [Constants::LARGE_PIC_FORMAT, :jpg] },
convert_options: { large: '-quality 100 -strip' }
2017-04-21 22:09:04 +08:00
validates_attachment_content_type :image,
content_type: %r{^image/#{ Regexp.union(
Constants::WHITELISTED_IMAGE_TYPES
) }}
2017-04-24 22:22:25 +08:00
validates_attachment :image,
presence: true,
size: {
less_than: Constants::FILE_MAX_SIZE_MB.megabytes
}
validates :estimated_size, presence: true
2017-04-21 22:09:04 +08:00
# When using S3 file upload, we can limit file accessibility with url signing
2017-04-24 22:22:25 +08:00
def presigned_url(style = :large,
2017-04-21 22:09:04 +08:00
download: false,
timeout: Constants::URL_LONG_EXPIRE_TIME)
2017-04-21 22:09:04 +08:00
if stored_on_s3?
if download
download_arg = 'attachment; filename=' + URI.escape(image_file_name)
else
download_arg = nil
end
signer = Aws::S3::Presigner.new(client: S3_BUCKET.client)
signer.presigned_url(:get_object,
bucket: S3_BUCKET.name,
key: image.path(style)[1..-1],
expires_in: timeout,
response_content_disposition: download_arg)
end
end
def stored_on_s3?
image.options[:storage].to_sym == :s3
end
def url(style = :large, timeout: Constants::URL_LONG_EXPIRE_TIME)
2017-04-21 22:09:04 +08:00
if image.is_stored_on_s3?
presigned_url(style, timeout: timeout)
else
image.url(style)
end
end
private
def update_estimated_size
return if image_file_size.blank?
es = image_file_size * Constants::ASSET_ESTIMATED_SIZE_FACTOR
update(estimated_size: es)
Rails.logger.info "Asset #{id}: Estimated size successfully calculated"
2017-04-25 19:44:31 +08:00
# update team space taken
self.team.take_space(es)
self.team.save
2017-04-25 19:44:31 +08:00
end
def release_team_space
self.team.release_space(estimated_size)
self.team.save
end
2017-04-21 22:09:04 +08:00
def set_reference
obj_type = "#{@reference.class.to_s.underscore}=".to_sym
2017-04-24 22:22:25 +08:00
public_send(obj_type, @reference) if @reference
2017-04-21 22:09:04 +08:00
end
end