2 class Picture < ActiveRecord::Base
3 belongs_to :original_picture
6 has_one :resource_picture
8 validates :original_picture_id, :presence => true, :numericality => true, :existence => true
9 validates :revision, :presence => true, :numericality => true
10 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
11 validates :width, :presence => true, :numericality => true, :natural_number => true
12 validates :height, :presence => true, :numericality => true, :natural_number => true
13 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
14 validates :license_id, :presence => true, :numericality => true, :existence => true
15 validates :artist_id, :presence => true, :numericality => true, :existence => true
16 validates :artist_name, :presence => true
17 validates :classname, :presence => true, :length => {:maximum => 50}
24 "#{self.id}.#{self.dext}"
32 Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1
44 rimg.flip.flop.to_blob
48 self.dext == 'png' and self.flag_gif_convert >= 0
51 def self.png_to_gif(data)
54 mgk = Magick::Image.from_blob(data).shift
64 flag_reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
68 attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize,
69 :original_picture_id => rp.original_picture_id, :license_id => rp.license_id, :artist_id => rp.artist_id,
70 :artist_name => rp.artist_name, :classname => rp.classname, :credit => rp.credit, :settings => rp.settings
72 self.attributes = attr
77 self.revision = self.new_revision
79 if res = self.store_picture(mgk)
81 if gifmgk = Picture.png_to_gif(mgk.to_blob)
82 res = self.store_picture(gifmgk)
92 def store_picture(mgk)
95 picdata = d.empty? ? mgk.to_blob : self.__send__(d, mgk)
96 res = PictureIO.picture_io.put(picdata, "#{self.id}.#{mgk.format}", d)
102 def restore(subdir = nil)
103 PictureIO.resource_picture_io.get self.filename, subdir
107 r = Picture.find(pid)
108 # raise ActiveRecord::Forbidden unless c.visible?(au)
113 "#{self.classname.tableize}/credit"
118 @credit_data = JSON.parse(self.credit) unless @credit_data
126 @flags = JSON.parse(self.settings) unless @flags
137 @flag_open = flags["open"] unless @flag_open
142 @flag_commercial = flags["commercial"] unless @flag_commercial
147 @flag_official = flags["official"] unless @flag_official
152 @flag_attribution = flags["attribution"] unless @flag_attribution
157 @flag_derive = flags["derive"] unless @flag_derive
162 @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
167 @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
172 @flag_reverse = flags["reverse"] unless @flag_reverse
177 @flag_resize = flags["resize"] unless @flag_resize
182 @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
187 @flag_overlap = flags["overlap"] unless @flag_overlap