-class SystemPicture < ActiveRecord::Base
+class SystemPicture < Peta::Item
+ load_manifest
has_many :balloons
- has_many :balloon_templates
+ has_many :speech_balloon_templates
has_many :licenses
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}
+ validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
before_destroy :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 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
+ 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.list_where
+ nil
+ end
+
+ def self.list_order
+ 'system_pictures.updated_at desc'
+ end
+
+ def self.list_opt
+ {}
end
- def store(picture_data)
- mgk = data_to_mgk picture_data
- return false unless mgk
+ def self.list_json_opt
+ {}
+ end
+
+ def self.show_opt
+ {}
+ end
+
+ def self.show_json_opt
+ {}
+ end
+
+ def store(imager)
+ unless imager
+ self.errors.add :base, 'illegal picture data'
+ return false
+ end
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)
+ 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(picture_data)
- md5 = Digest::MD5.hexdigest(picture_data)
- sp = SystemPicture.find_by_md5(md5)
- sp = SystemPicture.new() unless sp
- res = sp.store picture_data
+ 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