192 lines
5.7 KiB
Ruby
192 lines
5.7 KiB
Ruby
class Screenshot < ApplicationRecord
|
|
belongs_to :package, inverse_of: :screenshots
|
|
belongs_to :user, inverse_of: :screenshots
|
|
|
|
# Default sorting:
|
|
# - unapproved first (to see them at the top when moderating)
|
|
# - otherwise show newest first
|
|
default_scope { order(approved: :asc, created_at: :desc) }
|
|
|
|
# Paperclip
|
|
has_attached_file :image,
|
|
styles: { :large => '800x600>', :thumb => '160x120>' },
|
|
default_url: '/images/dummy/no-screenshots-available.svg',
|
|
path: ':rails_root/public/screenshots/:id_partition/:style.png',
|
|
url: '/screenshots/:id_partition/:style.png'
|
|
# validates_attachment_content_type :image, :content_type => 'image/png'
|
|
# validates_with AttachmentSizeValidator, :attributes => :image, :less_than => 5.megabytes
|
|
# validate :validate_image_is_unique
|
|
# validates :delete_reason, length: { in: 5..100 }, allow_nil: true
|
|
|
|
# Shrine
|
|
include ImageUploader::Attachment(:simage) # adds an `simage` virtual attribute
|
|
|
|
|
|
# Calculate how many days ago this screenshot has been uploaded
|
|
def age
|
|
time_ago_in_words(self.created_at)
|
|
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.present?
|
|
"#{self.description}"
|
|
else
|
|
"#{self.package.description}"
|
|
end
|
|
end
|
|
|
|
# Future use…
|
|
# def uploader
|
|
# if self.user
|
|
# self.user.name
|
|
# else
|
|
# 'Anonymous'
|
|
# end
|
|
# end
|
|
|
|
# Return Debshots 1.x path to allow migration of images into Paperclip filesystem schema
|
|
# def image_url(size)
|
|
# "live/screenshots/approved/#{self.package.name[0]}/#{self.package.name}/#{self.id}_#{size}.png"
|
|
# end
|
|
|
|
# Long status text for public users
|
|
def status
|
|
text = 'This image '
|
|
|
|
if self.approved
|
|
text << 'is public'
|
|
else
|
|
text << 'has to be moderated before being publicly visible'
|
|
end
|
|
|
|
if self.markedfordelete
|
|
text << ' (and was requested to be removed)'
|
|
end
|
|
|
|
return text
|
|
end
|
|
|
|
# Brief text describing the status of this screenshots (for admins)
|
|
def adminstatus
|
|
if self.markedfordelete
|
|
"Removal requested > #{self.delete_reason}"
|
|
elsif self.approved
|
|
'Public'
|
|
else
|
|
fa_icon('hourglass') + 'Waiting for approval'
|
|
end
|
|
end
|
|
|
|
# Publish a screenshot from the moderation queue
|
|
def approve!
|
|
self.delete_reason = nil
|
|
self.markedfordelete = false
|
|
self.approved = true
|
|
self.save!
|
|
end
|
|
|
|
# Get the newest screenshot regardless of the package it belongs to
|
|
def self.newest_upload
|
|
self.order(created_at: :desc).where(approved: true).first
|
|
end
|
|
|
|
# Returns true if the current user has administrative permissions
|
|
# def can_admin?
|
|
# self.admin == 1
|
|
# end
|
|
|
|
# Returns true if the current user can upload screenshots without moderation
|
|
# def can_upload_without_moderation?
|
|
# # Admins can upload without moderation
|
|
# true if self.can_admin
|
|
|
|
# # Authenticated users with at least one approved screenshot
|
|
# #true if self.screenshots.where(approved: true).count >= 1
|
|
|
|
# # Other visitors require moderation
|
|
# false
|
|
# end
|
|
|
|
# Returns true if the current user can delete screenshots
|
|
# def can_delete?
|
|
# # Admins can delete screenshots
|
|
# true if self.can_admin
|
|
|
|
# # Authenticated users with at least one approved screenshot
|
|
# #true if self.screenshots.where(approved: true).count >= 1
|
|
|
|
# # Other visitors require moderation
|
|
# false
|
|
# end
|
|
|
|
# Return the part of the version up to the first - or +
|
|
def upstream_version
|
|
self.version.split(/[\-\+]/).first
|
|
end
|
|
|
|
# Returns the path to this screenshot's image on disk
|
|
def disk_path
|
|
ActiveStorage::Blob.service.send(:path_for, self.image.key)
|
|
end
|
|
|
|
# Returns the MD5 hex digest of the current screenshot's data
|
|
def checksum
|
|
# local files stored on disk:
|
|
# url = "#{Rails.root}/public/#{attachment.path}"
|
|
Digest::MD5.hexdigest(disk_path)
|
|
|
|
# remote files stored on another person's computer:
|
|
#url = attachment.url
|
|
#Digest::MD5.base64digest(Net::HTTP.get(URI(url)))
|
|
end
|
|
|
|
# Returns a medium sized image (Papeclip-style)
|
|
# def medium_image
|
|
# if self.image.attached?
|
|
# self.image.variant(
|
|
# resize_to_limit: [670, 600]
|
|
# ).processed
|
|
# end
|
|
# end
|
|
|
|
# # Returns the image variant that links to a medium-size screenshot for the details page
|
|
# def large_image_watermarked
|
|
# if self.image.attached?
|
|
# self.image.variant(
|
|
# gravity: 'East',
|
|
# draw: 'image Over 0,0 0,0 "public/logo/watermark.png"'
|
|
# ).processed
|
|
# end
|
|
# end
|
|
|
|
private
|
|
|
|
# Validator that checks if the image has already been uploaded.
|
|
# A generic uniqueness validator does not work because it would attribute
|
|
# the error to the :image_fingerprint field and not the actual :image field.
|
|
# Currently this check makes sure that the same screenshot is not uploaded
|
|
# twice for the same package. However it allows the screenshot to be uploaded
|
|
# for two different packages. Restricting that further may happen later.
|
|
def validate_image_is_unique
|
|
# Look for images with the same checksum / image_fingerprint
|
|
if screenshot=Screenshot.find_by(image_fingerprint: image_fingerprint, package_id: self.package.id)
|
|
unless screenshot.id == self.id
|
|
errors.add(:image, "has already been uploaded for this package")
|
|
end
|
|
end
|
|
end
|
|
|
|
# Check that the uploaded file can be handled by ImageMagick and is not broken
|
|
# def image_intact?
|
|
# begin
|
|
# #MiniMagick::Image.open(self.image.blob.open)
|
|
# MiniMagick::Image.read(self.image.download)
|
|
# rescue MiniMagick::Invalid => exc
|
|
# error.add(:image, 'appears to be a broken image file')
|
|
# end
|
|
# end
|
|
|
|
end
|