OSDN Git Service

temp
[pettanr/pettanr.git] / app / models / resource_picture.rb
index 13a83fe..92da32b 100644 (file)
 #素材
-class ResourcePicture < ActiveRecord::Base
+class ResourcePicture < Pettanr::Item
   belongs_to :artist
   belongs_to :license
   belongs_to :picture
-  has_many :panel_pictures
   belongs_to :original_picture
   
   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 :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :artist_name, :presence => true
   validates :classname, :presence => true, :length => {:maximum => 50}
-  validates :picture_id, :presence => true, :numericality => true, :existence => true
+  validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
   
-  before_destroy :destroy_with_file
-  
-  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.singular
+    'ResourcePicture'
   end
   
-  def self.resize(data, dw, dh)
-    Magick::Image.from_blob(data).shift.resize(dw, dh)
+  def self.plural
+    'ResourcePictures'
   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 self.owner_type
+    :author
   end
   
-  def dext
-    self.ext.downcase
+  def self.valid_encode_columns
+    super.merge ['artist_name', 'classname', 'credit', 'settings']
   end
   
-  def filename
-    "#{self.id}.#{self.dext}"
-  end
-  
-  def mime_type
-    "image/#{self.dext}"
-  end
-  
-  def url subdir = nil
-    '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
+  def supply_default
   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
-  end
-  
-  def copy_data(op)
+  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
+      :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5
     }
     self.attributes = attr
   end
   
-  def data_to_mgk picture_data
-    begin
-      mgk = Magick::Image.from_blob(picture_data).shift
-    rescue 
-      self.errors.add :base, 'magick failed'
-      return false
+  def own? roles
+    roles = [roles] unless roles.respond_to?(:each)
+    ar = ResourcePicture.get_artist_from_roles roles
+    return false unless ar
+    self.artist_id == ar.id
+  end
+  
+  def visible? roles
+    if MagicNumber['run_mode'] == 0
+      return false unless guest_role_check(roles)
+    else
+      return false unless resource_reader_role_check(roles)
     end
-    mgk
+    true
   end
   
-  def op_mgk
-    d = self.original_picture.restore
-    return false unless d
-    self.data_to_mgk d
+  def filename
+    "#{self.id}.#{self.ext}"
   end
   
-  def new_picture mgk
-    pc = Picture.new
-    pc.copy_data self
-    pc.store mgk
-    pc
+  def gifname
+    "#{self.id}.gif"
   end
   
+  def mime_type
+    "image/#{self.ext}"
+  end
   
-  def to_gif?
-    self.dext == 'png' and self.flag_gif_convert >= 0
+  def url subdir = nil
+    '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
   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 to_gif?
+    self.ext == 'png' and self.flag_gif_convert >= 0
   end
   
-  def store
-    res = false
-    self.copy_data self.original_picture
-    mgk = self.op_mgk
-    return false unless mgk
-    OriginalPicture.transaction do
-      pc = self.new_picture mgk
-      if res = pc.valid?
-        self.picture_id = pc.id
-        if res = self.save
-          res = self.store_picture_with_gif(mgk)
-        end
-      else
-        self.errors.add :base, 'picture does not create'
-      end
-      raise ActiveRecord::Rollback unless res
-    end
-    res
+  def thumbnail(imager)
+    tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], rimg.columns, rimg.rows)
+    ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
   end
   
-  def store_picture_with_gif(mgk)
-    if res = self.store_picture(mgk)
-      if self.to_gif?
-        if gifmgk = ResourcePicture.png_to_gif(mgk.to_blob)
-          res = self.store_picture(gifmgk)
-        else
-          self.errors.add :base, 'picture data can not conv to gif'
-          res = false
-        end
-      end
-    else
-      self.errors.add :base, 'resource picture io does not work'
-    end
-    res
+  def tmb_opt_img_tag
+    tw, th = PettanImager.thumbnail_size(self.width, self.height)
+    {:src => self.url, :width => tw, :height => th}
   end
   
-  def store_picture(mgk)
-    res = false
-    tdata = self.flag_thumbnail >= 0 ? thumbnail(mgk) : mgk.to_blob
-    fdata = mgk.to_blob
-    return false unless PictureIO.resource_picture_io.put(tdata, "#{self.id}.#{mgk.format}")
-    PictureIO.resource_picture_io.put(fdata, "#{self.id}.#{mgk.format}", 'full')
+  def opt_img_tag
+    {:src => self.url('full'), :width => self.width, :height => self.height}
   end
   
-  def restore(subdir = nil)
-    PictureIO.resource_picture_io.get self.filename, subdir
+  def symbol_option
+    self.tmb_opt_img_tag
   end
   
   def self.default_page_size
@@ -190,50 +118,195 @@ class ResourcePicture < ActiveRecord::Base
     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
+  def self.mylist_where ar
+    ['resource_pictures.artist_id = ?', ar.id]
+  end
+  
+  def self.himlist_where ar
+    ['resource_pictures.artist_id = ?', ar.id]
+  end
+  
+  def self.list page = 1, page_size = self.default_page_size
+    ResourcePicture.includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.mylist ar, page = 1, page_size = Author.default_resource_picture_page_size
+    ResourcePicture.where(self.mylist_where(ar)).includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.himlist ar, page = 1, page_size = Author.default_resource_picture_page_size
+    ResourcePicture.where(self.himlist_where(ar)).includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.list_paginate page = 1, page_size = self.default_page_size
+    Kaminari.paginate_array(Array.new(ResourcePicture.count, nil)).page(page).per(page_size)
+  end
+  
+  def self.mylist_paginate ar, page = 1, page_size = Author.default_resource_picture_page_size
+    Kaminari.paginate_array(Array.new(ResourcePicture.where(self.mylist_where(ar)).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.himlist_paginate ar, page = 1, page_size = Author.default_resource_picture_page_size
+    Kaminari.paginate_array(Array.new(ResourcePicture.where(self.himlist_where(ar)).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.list_by_original_picture_where original_picture_id
+    ['resource_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('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.list_by_license_where license_id
+    ['resource_pictures.license_id = ?', license_id]
+  end
+  
+  def self.list_by_license license_id, roles, page = 1, page_size = self.default_page_size
+    self.where(self.list_by_license_where(license_id)).includes(self.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.list_by_artist_where artist_id
+    ['resource_pictures.artist_id = ?', artist_id]
   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 self.list_by_artist artist_id, roles, page = 1, page_size = self.default_page_size
+    self.where(self.list_by_artist_where(artist_id)).includes(self.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
   end
   
   def self.list_opt
-    {:include => [:license, :artist]}
+    {:license => {}, :artist => {}, :picture => {} }
   end
   
   def self.list_json_opt
-    {:include => [:license, :artist]}
+    {:include => {:license => {}, :artist => {}, :picture => {}} }
   end
   
-  def self.show rid, opt = {}
-    r = ResourcePicture.find(rid, :include => self.show_include_opt(opt))
-#    raise ActiveRecord::Forbidden unless c.visible?(au)
+  def self.show rid, roles
+    opt = {}
+    opt.merge!(self.show_opt)
+    r = ResourcePicture.find(rid, opt)
+    raise ActiveRecord::Forbidden unless r.visible?(roles)
     r
   end
   
-  def self.show_include_opt opt = {}
-    res = [:license, :artist]
-    res.push(opt[:include]) if opt[:include]
+  def self.show_opt
+    {:include => {:license => {}, :artist => {}, :picture => {}} }
+  end
+  
+  def self.show_json_opt
+    {:include => {:license => {}, :artist => {}, :picture => {}} }
+  end
+  
+  def self.edit rid, ar
+    opt = {}
+    opt.merge!(self.show_opt)
+    r = ResourcePicture.find(rid, opt)
+    raise ActiveRecord::Forbidden unless r.own?(ar)
+    r
+  end
+  
+  def new_picture imager
+    pc = Picture.new
+    pc.supply_default
+    pc.overwrite self
+    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
+        if res = self.save
+          res = self.store_picture_with_gif(imager)
+        end
+      else
+      end
+      raise ActiveRecord::Rollback unless res
+    end
+    res
+  end
+  
+  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
+            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(imager, fn)
+    res = false
+    thumbnail_imager = self.flag_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
   
-  def self.show_json_include_opt
-    {:include => [:license, :artist]}
+  def restore(subdir = nil)
+    PictureIO.resource_picture_io.get self.filename, subdir
+  end
+  
+  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
+      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.visible_count
     ResourcePicture.count
   end
   
+  def picture_data
+    Base64.encode64(self.restore 'full')
+  end
+  
   def credit_template
-    "#{self.classname.tableize}/credit"
+    "#{self.classname.tableize}/attributes/credit"
+  end
+  
+  def full_credit_template
+    "#{self.classname.tableize}/attributes/full_credit"
   end
   
   def credit_data