#実素材
-class Picture < ActiveRecord::Base
+class Picture < Peta::Content
+ load_manifest
belongs_to :original_picture
belongs_to :license
+ belongs_to :system_picture
belongs_to :artist
has_one :resource_picture
+ has_one :resource_picture_picture
- validates :original_picture_id, :presence => true, :numericality => true, :existence => true
+ validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
validates :revision, :presence => true, :numericality => true
validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
validates :width, :presence => true, :numericality => true, :natural_number => true
validates :height, :presence => true, :numericality => true, :natural_number => true
validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
- validates :license_id, :presence => true, :numericality => true, :existence => true
- validates :artist_id, :presence => true, :numericality => true, :existence => true
- validates :artist_name, :presence => true
- validates :classname, :presence => true, :length => {:maximum => 50}
+ validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
+ validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :license_group_module_name, :presence => true, :length => {:maximum => 50}
- def dext
- self.ext.downcase
+ scope :find_index, -> do
+ self
+ end
+
+ scope :find_by_artist, -> (artist_id) do
+ find_index.where(artist_id: artist_id)
+ end
+
+ scope :find_by_md5, -> (md5) do
+ find_index.where(md5: md5).order(updated_at: :desc)
+ end
+
+ scope :find_by_md5_without_original_picture, -> (md5, original_picture_id) do
+ self.find_by_md5(md5).where.not(original_picture_id: original_picture_id).order(updated_at: :desc)
+ end
+
+ scope :find_by_original_picture, -> (original_picture_id) do
+ find_index.where(original_picture_id: original_picture_id)
+ end
+
+ def supply_default
+ end
+
+ def overwrite rp
+ attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize,
+ :original_picture_id => rp.original_picture_id, :license_id => rp.license_id,
+ :system_picture_id => rp.system_picture_id, :artist_id => rp.artist_id,
+ :md5 => rp.md5,
+ :license_group_module_name => rp.license_group_module_name,
+ :license_group_settings => rp.license_group_settings,
+ :credit_picture_settings => rp.credit_picture_settings,
+ :license_settings => rp.license_settings
+ }
+ self.attributes = attr
+ self.revision = self.new_revision #Do not move to attr. new_revision reffernces self.original_picture_id
+ end
+
+ def visible? operators
+ return true
+ end
+
+ def showable? operators = nil
+ return false unless self.original_picture
+ return true if self.own?(operators)
+ self.enable? and self.head?
end
def filename
- "#{self.id}.#{self.dext}"
+ "#{self.id}.#{self.ext}"
+ end
+
+ def gifname
+ "#{self.id}.gif"
end
def mime_type
- "image/#{self.dext}"
+ "image/#{self.ext}"
end
- def new_revision
- Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1
+ def url
+ '/pictures/' + filename
end
- def v(rimg)
- rimg.flip.to_blob
+ def opt_img_tag
+ {:src => self.url, :width => self.width, :height => self.height}
end
- def h(rimg)
- rimg.flop.to_blob
+ def tmb_opt_img_tag
+ tw, th = PettanImager.thumbnail_size(self.width, self.height)
+ {:src => self.url, :width => tw, :height => th}
end
- def vh(rimg)
- rimg.flip.flop.to_blob
+ def tail_opt_img_tag img
+ {:src => img, :width => self.width, :height => self.height}
end
- def to_gif?
- self.dext == 'png' and self.flag_gif_convert >= 0
+ def tail_tmb_opt_img_tag img
+ tw, th = PettanImager.thumbnail_size(self.width, self.height)
+ {:src => img, :width => tw, :height => th}
end
- def self.png_to_gif(data)
- res = nil
- begin
- mgk = Magick::Image.from_blob(data).shift
- mgk.format = 'gif'
- res = mgk
- rescue
- res = false
- end
- res
+ def alt_name
+ self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
end
- def subdirs
- flag_reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
+ def symbol_option
+ self.tmb_opt_img_tag
end
- def copy_data(rp)
- attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize,
- :original_picture_id => rp.original_picture_id, :license_id => rp.license_id, :artist_id => rp.artist_id,
- :artist_name => rp.artist_name, :classname => rp.classname, :credit => rp.credit, :settings => rp.settings
- }
- self.attributes = attr
+ def new_revision
+ Picture.where(original_picture_id: self.original_picture_id).maximum(:revision).to_i + 1
end
- def store(mgk)
- res = false
- self.revision = self.new_revision
- if res = self.save
- if res = self.store_picture(mgk)
- if self.to_gif?
- if gifmgk = Picture.png_to_gif(mgk.to_blob)
- res = self.store_picture(gifmgk)
- else
- res = false
- end
- end
- end
- end
- res
+ def enable?
+ r = self.head.resource_picture
+ r ? true : false
end
- def store_picture(mgk)
- res = false
- subdirs.each do |d|
- picdata = d.empty? ? mgk.to_blob : self.__send__(d, mgk)
- res = PictureIO.picture_io.put(picdata, self.filename, d)
- break unless res
- end
- res
+ def self.head opid
+ Picture.where(original_picture_id: opid).order('pictures.revision desc').first
end
- def restore(subdir = nil)
- PictureIO.resource_picture_io.get self.filename, subdir
+ def head
+ Picture.head(self.original_picture_id)
end
- def self.show pid
- r = Picture.find(pid)
-# raise ActiveRecord::Forbidden unless c.visible?(au)
- r
+ def head?
+ self == head
end
- def credit_template
- "#{self.classname.tableize}/attributes/credit"
+ def to_gif?
+ self.ext == 'png' and self.license_extend.gif_convert >= 0
end
- def credit_data
- begin
- @credit_data = JSON.parse(self.credit) unless @credit_data
- rescue
- end
- @credit_data
+ def subdirs
+ self.license_extend.reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
end
- def flags
- return @flags if @flags
- begin
- @flags = JSON.parse(self.settings)
- rescue
- end
- @flags = {} unless @flags
- @flags
+ def self.exist_by_md5 md5, opid
+ Picture.find_by_md5(md5, opid).empty? ? false : true
+ end
+
+ def self.list_by_original_picture_where original_picture_id
+ ['pictures.original_picture_id = ?', original_picture_id]
+ end
+
+ def self.list_by_original_picture original_picture_id, roles, page = 1, page_size = self.default_page_size
+ self.where(self.list_by_original_picture_where(original_picture_id)).includes(self.list_opt).order('pictures.revision desc').offset((page -1) * page_size).limit(page_size)
end
- def flags=(s)
- @flags = s
+ def self.list_opt
+ {:license => {}, :artist => {}}
end
- def flag_open
- @flag_open = (flags["open"] || -1) unless @flag_open
- @flag_open
+ def self.list_json_opt
+ {:include => {:license => {}, :artist => {}} }
end
- def flag_commercial
- @flag_commercial = (flags["commercial"] || -1) unless @flag_commercial
- @flag_commercial
+ def store(imager)
+ res = false
+ if res = self.save
+ if res = self.store_picture(imager, self.filename)
+ if self.to_gif?
+ if gifimager = imager.to_gif
+ res = self.store_picture(gifimager, self.gifname)
+ else
+ res = false
+ end
+ end
+ end
+ end
+ res
end
- def flag_official
- @flag_official = (flags["official"] || -1) unless @flag_official
- @flag_official
+ def store_picture(imager, fn)
+ res = false
+ subdirs.each do |d|
+ picdata = d.empty? ? imager.binary : imager.__send__(d)
+ res = PictureIO.picture_io.put(picdata, fn, d)
+ break unless res
+ end
+ res
end
- def flag_attribution
- @flag_attribution = (flags["attribution"] || -1) unless @flag_attribution
- @flag_attribution
+ def restore(subdir = nil)
+ PictureIO.picture_io.get self.filename, subdir
end
- def flag_derive
- @flag_derive = (flags["derive"] || -1) unless @flag_derive
- @flag_derive
+ def self.export(dt = nil)
+ pictures = Picture.includes(:artist).where('artists.author_id is not null')
+ pictures = pictures.where(['pictures.updated_at >= ?', dt]) if dt
+ pictures.order('pictures.updated_at desc').references(:artist)
end
- def flag_thumbnail
- @flag_thumbnail = (flags["thumbnail"] || -1) unless @flag_thumbnail
- @flag_thumbnail
+ def self.list_as_json_text ary
+ '[' + ary.map {|i| i.to_json_with_picture_data }.join(',') + ']'
end
- def flag_gif_convert
- @flag_gif_convert = (flags["gif_convert"] || -1) unless @flag_gif_convert
- @flag_gif_convert
+ def picture_data
+ Base64.encode64(self.restore)
end
- def flag_reverse
- @flag_reverse = (flags["reverse"] || -1) unless @flag_reverse
- @flag_reverse
+ def to_json_with_picture_data
+ self.to_json({:methods => :picture_data})
end
- def flag_resize
- @flag_resize = (flags["resize"] || -1) unless @flag_resize
- @flag_resize
+ def unpublish
+ self.boosts 'post'
+ imager = PettanImager.load(File.open(Rails.root + 'app/assets/images/error.png', 'rb').read)
+ return false unless imager
+ self.store imager
end
- def flag_sync_vh
- @flag_sync_vh = (flags["sync_vh"] || -1) unless @flag_sync_vh
- @flag_sync_vh
+ def credit_template
+ "#{self.license_group_module_name.tableize}/attributes/credit"
end
- def flag_overlap
- @flag_overlap = (flags["overlap"] || -1) unless @flag_overlap
- @flag_overlap
+ def full_credit_template
+ "#{self.license_group_module_name.tableize}/attributes/full_credit"
end
end