-class ResourcePicture < ActiveRecord::Base
+#素材
+class ResourcePicture < Peta::Content
+ load_manifest
belongs_to :artist
+ belongs_to :license_group
belongs_to :license
- has_many :panel_pictures
+ belongs_to :system_picture
+ belongs_to :picture
belongs_to :original_picture
+ has_many :resource_picture_pictures
- validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}\r
+ 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 :artist_id, :presence => true, :numericality => true, :existence => true
- validates :license_id, :presence => true, :numericality => true, :existence => true
- validates :original_picture_id, :presence => true, :numericality => true, :existence => true
+ validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
+ validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :license_group_module_name, :presence => true, :length => {:maximum => 50}
+ validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :license_settings, :boost => {:boost_name => :license}
+ validates :license_group_settings, :boost => {:boost_name => :license_group}
+ validates :credit_picture_settings, :boost => {:boost_name => :credit_picture}
- before_destroy :destroy_with_file
+ def self.pickup_item_name
+ Picture.item_name
+ end
- def destroy_with_file
- PictureIO.resource_picture_io.delete self.filename
- PictureIO.resource_picture_io.class.subdirs.each do |d|
- next if d.empty?
- PictureIO.resource_picture_io.delete(self.filename, d) if PictureIO.resource_picture_io.exist?(self.filename, d)
- end
+ def self.pickup_column_name
+ self.pickup_item_name + '_id'
end
- def self.resize(data, dw, dh)
- Magick::Image.from_blob(data).shift.resize(dw, dh)
+ def pickup_id
+ # get head picture
+ self.attributes[self.pickup_column_name]
end
- #サイズの調整(limw,limhに必ず収まるように合わせる)
- def self.fix_size_both(limw, limh, w, h)
- wr = if w > limw
- limw*100/w
- else
- 100
- end
- hr = if h > limh
- limh*100/h
- else
- 100
- end
- res = if wr < hr
- #幅の方が圧縮率が高い
- [w*wr/100, h*wr/100]
- else
- #高さの方が圧縮率が高い
- [w*hr/100, h*hr/100]
- end
- res
+ def supply_default
end
- def dext
- self.ext.downcase
+ def overwrite op
+ attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
+ :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5,
+ :created_at => Time.now, :updated_at => Time.now
+ }
+ self.attributes = attr
+ end
+
+ def visible? operators
+ # no super
+ # content model call to owner checker
+ self.user_visible? operators
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 url subdir = nil
+ '/resource_pictures/' + filename + (subdir.to_s.empty? ? '' : '?subdir=' + subdir.to_s)
end
- def v(rimg)
- rimg.flip.to_blob
+ def to_gif?
+ self.ext == 'png' and self.license_extend.gif_convert >= 0
end
- def h(rimg)
- rimg.flop.to_blob
+ def thumbnail(imager)
+ tw, th = ResourcePicture.fix_size_both(Manifest.manifest.magic_numbers['thumbnail_width'], Manifest.manifest.magic_numbers['thumbnail_height'], rimg.columns, rimg.rows)
+ ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
end
- def vh(rimg)
- rimg.flip.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 url subdir = nil
- '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
+ def opt_img_tag
+ {:src => self.url('full'), :width => self.width, :height => self.height}
end
- def thumbnail(rimg)
- tw, th = ResourcePicture.fix_size_both(64, 64, rimg.columns, rimg.rows)
- ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
+ def alt_name
+ self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
end
- def self.update_picture(op)
- res = op.resource_picture || ResourcePicture.new
- res.attributes = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
- :original_picture_id => op.id, :artist_id => op.artist_id, :license_id => op.license_id
- }
- res
+ def symbol_option
+ self.tmb_opt_img_tag
end
- def to_gif?
- self.dext == 'png' and self.license.no_convert == 0
+ def self.list_opt
+ {:license => {}, :artist => {}, :picture => {} }
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
+ def self.list_json_opt
+ {:include => {:license => {}, :artist => {}, :picture => {}} }
+ end
+
+ def self.show_opt
+ {:include => {:license => {}, :artist => {}, :picture => {}} }
+ end
+
+ def self.show_json_opt
+ {:include => {:license => {}, :artist => {}, :picture => {}} }
+ end
+
+ def new_picture imager
+ pc = Picture.new
+ pc.supply_default
+ pc.overwrite self
+ pc.boosts 'post'
+ r = pc.store imager
+ return pc if r
+ self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
+ false
+ end
+
+ def store imager
+ return false unless imager
+ res = false
+ self.overwrite self.original_picture
+ ResourcePicture.transaction do
+ self.original_picture.published_at = Time.now
+ self.original_picture.stopped_at = nil
+ raise ActiveRecord::Rollback unless self.original_picture.save
+ pc = self.new_picture imager
+ if pc
+ self.picture_id = pc.id
+ resource_picture_picture = ResourcePicturePicture.new(
+ :original_picture_id => self.original_picture_id,
+ :resource_picture_id => self.id,
+ :picture_id => pc.id
+ )
+ raise ActiveRecord::Rollback unless resource_picture_picture.save
+ if res = self.save
+ self.original_picture.resource_picture_pictures.each do |resource_picture_picture|
+ resource_picture_picture.resource_picture_id = self.id
+ raise ActiveRecord::Rollback unless resource_picture_picture.save
+ end
+ res = self.store_picture_with_gif(imager)
+ end
+ else
+ end
+ raise ActiveRecord::Rollback unless res
end
res
end
- def store(mgk)
- res = false
- if res = self.save
- if res = self.store_picture(mgk)
- if self.to_gif?
- if gifmgk = ResourcePicture.png_to_gif(mgk.to_blob)
- res = self.store_picture(gifmgk)
+ def store_picture_with_gif(imager)
+ if res = self.store_picture(imager, self.filename)
+ if self.to_gif?
+ if gifimager = imager.to_gif
+ if res = self.store_picture(gifimager, self.gifname)
else
- res = false
+ self.errors.add :base, I18n.t('picture_io.error')
end
+ else
+ self.errors.add :base, I18n.t('errors.not_convert')
+ res = false
end
end
+ else
+ self.errors.add :base, I18n.t('picture_io.error')
end
res
end
- def store_picture(mgk)
+ def store_picture(imager, fn)
res = false
- PictureIO.resource_picture_io.class.subdirs.each do |d|
- picdata = d.empty? ? mgk.to_blob : self.__send__(d, mgk)
- res = PictureIO.resource_picture_io.put(picdata, "#{self.id}.#{mgk.format}", d)
- break unless res
+ thumbnail_imager = self.license_extend.thumbnail >= 0 ? imager.to_thumbnail : imager
+ return false unless thumbnail_imager
+ begin
+ PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
+ PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
+ res = true
+ rescue PictureIO::Error
+ res = false
end
res
end
PictureIO.resource_picture_io.get self.filename, subdir
end
- def self.default_page_size
- 25
- end
-
- def self.max_page_size
- 100
- end
-
- def self.page prm = nil
- page = prm.to_i
- page = 1 if page < 1
- page
- end
-
- def self.page_size prm = self.default_page_size
- page_size = prm.to_i
- page_size = self.max_page_size if page_size > self.max_page_size
- page_size = self.default_page_size if page_size < 1
- page_size
- end
-
- def self.offset cnt, prm = nil
- offset = prm.to_i
- offset = cnt - 1 if offset >= cnt
- offset = cnt - offset.abs if offset < 0
- offset = 0 if offset < 0
- offset
- end
-
- def self.list opt = {}, page = 1, page_size = self.default_page_size
- opt.merge!(self.list_opt) unless opt[:include]
- opt.merge!({:order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
- ResourcePicture.find(:all, opt)
+ def unpublish
+ res = false
+ ResourcePicture.transaction do
+ self.original_picture.published_at = nil
+ self.original_picture.stopped_at = Time.now
+ raise ActiveRecord::Rollback unless self.original_picture.save
+ self.original_picture.resource_picture_pictures.each do |resource_picture_picture|
+ resource_picture_picture.resource_picture_id = nil
+ raise ActiveRecord::Rollback unless resource_picture_picture.save
+ end
+ begin
+ PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
+ PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
+ rescue PictureIO::Error
+ res = false
+ raise ActiveRecord::Rollback
+ end
+ res = self.destroy
+ raise ActiveRecord::Rollback unless res
+ end
+ res
end
- def self.list_opt
- {:include => [:artist, :license]}
+ def self.visible_count
+ ResourcePicture.count
end
- def self.list_json_opt
- {:include => [:artist, :license]}
+ def picture_data
+ Base64.encode64(self.restore 'full')
end
- def self.show rid, opt = {}
- r = ResourcePicture.find(rid, :include => self.show_include_opt(opt))
-# raise ActiveRecord::Forbidden unless c.visible?(au)
- r
+ def new_template
+ "#{self.license_group_module_name.tableize}/attributes/new"
end
- def self.show_include_opt opt = {}
- res = [:artist, :license]
- res.push(opt[:include]) if opt[:include]
- res
+ def credit_template
+ "#{self.license_group_module_name.tableize}/attributes/credit"
end
- def self.show_json_include_opt
- {:include => [:artist, :license]}
+ def full_credit_template
+ "#{self.license_group_module_name.tableize}/attributes/full_credit"
end
- def self.visible_count
- ResourcePicture.count
+ def self.remake_all
+ ResourcePicture.find(:all).each do |resource_picture|
+ resource_picture.boosts 'post'
+ full = resource_picture.restore 'full'
+ imager = PettanImager.load full
+ resource_picture.store_picture_with_gif(imager)
+ end
end
end