has_many :balloons
has_many :balloon_templates
- 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 => {: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
'/system_pictures/' + filename
end
- def store(rimg)
- bindata = rimg.to_blob
- PictureIO.system_picture_io.put bindata, self.filename
- true
+ def data_to_mgk picture_data
+ begin
+ mgk = Magick::Image.from_blob(picture_data).shift
+ rescue
+ self.errors.add :base, 'magick failed'
+ return false
+ end
+ mgk
+ end
+
+ def store(picture_data)
+ mgk = data_to_mgk picture_data
+ return false unless mgk
+ res = false
+ self.attributes = {:ext => mgk.format.downcase, :width => mgk.columns, :height => mgk.rows,
+ :filesize => mgk.filesize
+ }
+ self.md5 = Digest::MD5.hexdigest(picture_data) unless self.md5
+ SystemPicture.transaction do
+ if res = self.save
+ res = PictureIO.system_picture_io.put(picture_data, self.filename)
+ end
+ end
+ res
+ end
+
+ def self.store(picture_data)
+ md5 = Digest::MD5.hexdigest(picture_data)
+ sp = SystemPicture.find_by_md5(md5)
+ sp = SystemPicture.new() unless sp
+ res = sp.store picture_data
+ res ? sp : nil
end
def restore(subdir = nil)