65 lines
2.1 KiB
Ruby
65 lines
2.1 KiB
Ruby
class Screenshot < ActiveRecord::Base
|
|
belongs_to :package, :inverse_of=>:screenshots
|
|
|
|
default_scope {
|
|
order('uploaddatetime DESC')
|
|
}
|
|
|
|
has_attached_file :image,
|
|
styles: { :large => "800x600>", :thumb => "160x120>" },
|
|
default_url: "/images/dummy/no-screenshots-upload-one.svg"
|
|
#path: :rails_root/public/system/:class/:attachment/:id_partition/:style/:filename
|
|
validates_attachment_content_type :image, :content_type => /\Aimage\/png\Z/
|
|
|
|
def image_url(size)
|
|
"#{Rails.configuration.images_path_prefix}/#{self.package.name[0]}/#{self.package.name}/#{self.id}_#{size}.png"
|
|
end
|
|
|
|
# Get the URL leading to a screenshot of this package
|
|
def url(size)
|
|
if self.approved
|
|
# TODO: Make the path configurable
|
|
basepath = "/screenshots/"
|
|
else
|
|
# TODO: Choose a path that makes unapproved screenshots unavailable
|
|
basepath = "/screenshots/unapproved/"
|
|
end
|
|
|
|
File.join(basepath, self.package.name[0], self.package.name, "#{self.id}_#{size}.png")
|
|
end
|
|
|
|
# Return caption for full-screen screenshots.
|
|
# Takes the description of a screenshot if available.
|
|
# Otherwise it falls back to the general description of its package.
|
|
def caption
|
|
if self.description != ''
|
|
self.description
|
|
else
|
|
self.package.description
|
|
end
|
|
end
|
|
|
|
def image_file(size)
|
|
# Screenshot files are at e.g.
|
|
# .../public/screenshots/f/firefox/5871654_large.png
|
|
Rails.configuration.images_path.join(self.package.name[0], self.package.name, "#{self.id}_#{size}.png")
|
|
end
|
|
|
|
# Delete all image files related to this screenshot
|
|
def delete_all_images
|
|
Rails.configuration.image_sizes.each do |size,dimensions|
|
|
image_file = self.image_file(size)
|
|
Rails.logger.info "(before_destroy) Deleting screenshot image '#{image_file}' from disk"
|
|
if File.exist?(image_file)
|
|
File.delete(image_file)
|
|
else
|
|
Rails.logger.error "(before_destroy) File '#{image_file}' not found - could not delete it"
|
|
end
|
|
end
|
|
end
|
|
|
|
# Callback to delete image files from disk when a screenshot record gets destroyed
|
|
before_destroy do |screenshot|
|
|
screenshot.delete_all_images
|
|
end
|
|
end
|