OSDN Git Service

add: license publisher
[pettanr/pettanr.git] / app / models / system_picture.rb
index 9d01831..8b25d19 100644 (file)
@@ -1,37 +1,82 @@
-class SystemPicture < ActiveRecord::Base
+class SystemPicture < Peta::SystemResource
+  load_manifest
   has_many :balloons
-  has_many :balloon_templates
+  has_many :speech_balloon_templates
+  has_many :licenses
+  has_many :writing_formats
   
-  before_destroy :destroy_with_file
+  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}
   
-  def validate
-    errors.add(:filesize, 'size over(1MB)') if self.filesize > 1000000
-  end
+  before_destroy :destroy_with_file
   
   def destroy_with_file
     PictureIO.system_picture_io.delete self.filename
   end
   
-  def dext
-    self.ext.downcase
+  def supply_default
+  end
+  
+  def overwrite
   end
   
   def filename
-    "#{self.id}.#{self.dext}"
+    "#{self.id}.#{self.ext}"
   end
   
   def mime_type
-    "image/#{self.dext}"
+    "image/#{self.ext}"
   end
   
   def url
     '/system_pictures/' + filename
   end
   
-  def store(rimg)
-    bindata = rimg.to_blob
-    PictureIO.system_picture_io.put bindata, self.filename
-    true
+  def opt_img_tag
+    {:src => self.url, :width => self.width, :height => self.height}
+  end
+  
+  def tmb_opt_img_tag
+    tw, th = PettanImager.thumbnail_size(self.width, self.height)
+    {:src => self.url, :width => tw, :height => th}
+  end
+  
+  def symbol_option
+    self.tmb_opt_img_tag
+  end
+  
+  def self.show_opt
+    {}
+  end
+  
+  def store(imager)
+    unless imager
+      self.errors.add :base, 'illegal picture data'
+      return false
+    end
+    res = false
+    SystemPicture.transaction do
+      if res = self.save
+        begin
+          res = PictureIO.system_picture_io.put(imager.binary, self.filename)
+        rescue PictureIO::Error
+          res = false
+          self.errors.add :base, I18n.t('picture_io.error')
+          raise ActiveRecord::Rollback
+        end
+      end
+    end
+    res
+  end
+  
+  def self.store(imager)
+    attr = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
+    sp = SystemPicture.modify_object(imager.md5, attr, 'md5')
+    res = sp.store imager
+    res ? sp : nil
   end
   
   def restore(subdir = nil)