OSDN Git Service

t#29400
[pettanr/pettanr.git] / app / models / resource_picture.rb
index 5f5c9e6..7bd555c 100644 (file)
@@ -1,16 +1,22 @@
+#素材
 class ResourcePicture < ActiveRecord::Base
   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']}\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 :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
   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 :artist_name, :presence => true
+  validates :classname, :presence => true, :length => {:maximum => 50}
+  validates :picture_id, :presence => true, :numericality => true, :existence => true
   
   before_destroy :destroy_with_file
   
@@ -22,105 +28,52 @@ class ResourcePicture < ActiveRecord::Base
     end
   end
   
-  def self.resize(data, dw, dh)
-    Magick::Image.from_blob(data).shift.resize(dw, dh)
+  def supply_default
   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 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
+    }
+    self.attributes = attr
   end
   
-  def dext
-    self.ext.downcase
+  def own? author
+    return false
   end
   
-  def filename
-    "#{self.id}.#{self.dext}"
+  def visible? author
+    true
   end
   
-  def mime_type
-    "image/#{self.dext}"
+  def filename
+    "#{self.id}.#{self.ext}"
   end
   
-  def url subdir = nil
-    '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
+  def gifname
+    "#{self.id}.gif"
   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 mime_type
+    "image/#{self.ext}"
   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 url subdir = nil
+    '/resource_pictures/' + (subdir.to_s.empty? ? '' : subdir.to_s + '/' ) + filename
   end
   
   def to_gif?
-    self.dext == 'png' and self.license.no_convert == 0
-  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
+    self.ext == 'png' and self.flag_gif_convert >= 0
   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)
-          else
-            res = false
-          end
-        end
-      end
-    end
-    res
-  end
-  
-  def store_picture(mgk)
-    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
-    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 restore(subdir = nil)
-    PictureIO.resource_picture_io.get self.filename, subdir
+  def opt_img_tag
+    tw, th = PettanImager.thumbnail_size(self.width, self.height)
+    {:src => self.url, :width => tw, :height => th}
   end
   
   def self.default_page_size
@@ -144,46 +97,176 @@ 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
-  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})
+  def self.list page = 1, page_size = self.default_page_size
+    opt = {}
+    opt.merge!(self.list_opt)
+    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
+    opt.merge!({:order => 'updated_at desc'})
     ResourcePicture.find(:all, opt)
   end
   
   def self.list_opt
-    {:include => [:license, :artist]}
+    {:include => {: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, au
+    opt = {}
+    opt.merge!(self.show_opt)
+    r = ResourcePicture.find(rid, opt)
+    raise ActiveRecord::Forbidden unless r.visible?(au)
     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 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 does not create' + pc.errors.full_messages.join("\n")
+    false
+  end
+  
+  def store imager
+    return false unless imager
+    res = false
+    self.overwrite self.original_picture
+    ResourcePicture.transaction do
+      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
+          res = self.store_picture(gifimager, self.gifname)
+        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
   end
   
-  def self.show_json_include_opt
-    {:include => [:license, :artist]}
+  def store_picture(imager, fn)
+    res = false
+    thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
+    return false unless thumbnail_imager
+    return false unless PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
+    return false unless PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
+    true
+  end
+  
+  def restore(subdir = nil)
+    PictureIO.resource_picture_io.get self.filename, subdir
   end
   
   def self.visible_count
     ResourcePicture.count
   end
   
+  def credit_template
+    "#{self.classname.tableize}/attributes/credit"
+  end
+  
+  def credit_data
+    begin
+      @credit_data = JSON.parse(self.credit) unless @credit_data
+    rescue 
+    end
+    @credit_data
+  end
+  
+  def flags
+    begin
+      @flags = JSON.parse(self.settings) unless @flags
+    rescue 
+    end
+    @flags
+  end
+  
+  def flags=(s)
+    @flags = s
+  end
+  
+  def flag_open
+    @flag_open = flags["open"] unless @flag_open
+    @flag_open
+  end
+  
+  def flag_commercial
+    @flag_commercial = flags["commercial"] unless @flag_commercial
+    @flag_commercial
+  end
+  
+  def flag_official
+    @flag_official = flags["official"] unless @flag_official
+    @flag_official
+  end
+  
+  def flag_attribution
+    @flag_attribution = flags["attribution"] unless @flag_attribution
+    @flag_attribution
+  end
+  
+  def flag_derive
+    @flag_derive = flags["derive"] unless @flag_derive
+    @flag_derive
+  end
+  
+  def flag_thumbnail
+    @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
+    @flag_thumbnail
+  end
+  
+  def flag_gif_convert
+    @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
+    @flag_gif_convert
+  end
+  
+  def flag_reverse
+    @flag_reverse = flags["reverse"] unless @flag_reverse
+    @flag_reverse
+  end
+  
+  def flag_resize
+    @flag_resize = flags["resize"] unless @flag_resize
+    @flag_resize
+  end
+  
+  def flag_sync_vh
+    @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
+    @flag_sync_vh
+  end
+  
+  def flag_overlap
+    @flag_overlap = flags["overlap"] unless @flag_overlap
+    @flag_overlap
+  end
+  
 end