2 class ResourcePicture < Peta::Content
5 belongs_to :license_group
7 belongs_to :system_picture
9 belongs_to :original_picture
10 has_many :resource_picture_pictures
12 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
13 validates :width, :presence => true, :numericality => true, :natural_number => true
14 validates :height, :presence => true, :numericality => true, :natural_number => true
15 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
16 validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
17 validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
18 validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
19 validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
20 validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
21 validates :license_group_module_name, :presence => true, :length => {:maximum => 50}
22 validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
23 validates :license_settings, :boost => {:boost_name => :license}
24 validates :license_group_settings, :boost => {:boost_name => :license_group}
25 validates :credit_picture_settings, :boost => {:boost_name => :credit_picture}
27 scope :find_index, -> do
31 scope :find_private, -> (operators) do
32 where(artist_id: operators.artist.id)
35 scope :find_by_original_picture, -> (original_picture_id) do
36 find_index.where(original_picture_id: original_picture_id)
39 scope :find_by_license_group, -> (license_group_id) do
40 find_index.where(license_group_id: license_group_id)
43 scope :find_by_license, -> (license_id) do
44 find_index.where(license_id: license_id)
47 scope :find_by_artist, -> (artist_id) do
48 find_index.where(artist_id: artist_id)
51 def self.pickup_item_name
55 def self.pickup_column_name
56 self.pickup_item_name + '_id'
61 self.attributes[self.pickup_column_name]
68 attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
69 :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5,
70 :created_at => Time.now, :updated_at => Time.now
72 self.attributes = attr
75 def visible? operators
77 # content model call to owner checker
78 self.user_visible? operators
82 "#{self.id}.#{self.ext}"
94 '/resource_pictures/' + filename + (subdir.to_s.empty? ? '' : '?subdir=' + subdir.to_s)
98 self.ext == 'png' and self.license_extend.gif_convert >= 0
101 def thumbnail(imager)
102 tw, th = ResourcePicture.fix_size_both(Manifest.manifest.magic_numbers['thumbnail_width'], Manifest.manifest.magic_numbers['thumbnail_height'], rimg.columns, rimg.rows)
103 ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
107 tw, th = PettanImager.thumbnail_size(self.width, self.height)
108 {:src => self.url, :width => tw, :height => th}
112 {:src => self.url('full'), :width => self.width, :height => self.height}
116 self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
124 {:license => {}, :artist => {}, :picture => {} }
127 def self.list_json_opt
128 {:include => {:license => {}, :artist => {}, :picture => {}} }
132 {:include => {:license => {}, :artist => {}, :picture => {}} }
135 def self.show_json_opt
136 {:include => {:license => {}, :artist => {}, :picture => {}} }
139 def new_picture imager
146 self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
151 return false unless imager
153 self.overwrite self.original_picture
154 ResourcePicture.transaction do
155 self.original_picture.published_at = Time.now
156 self.original_picture.stopped_at = nil
157 raise ActiveRecord::Rollback unless self.original_picture.save
158 pc = self.new_picture imager
160 self.picture_id = pc.id
161 resource_picture_picture = ResourcePicturePicture.new(
162 :original_picture_id => self.original_picture_id,
163 :resource_picture_id => self.id,
166 raise ActiveRecord::Rollback unless resource_picture_picture.save
168 self.original_picture.resource_picture_pictures.each do |resource_picture_picture|
169 resource_picture_picture.resource_picture_id = self.id
170 raise ActiveRecord::Rollback unless resource_picture_picture.save
172 res = self.store_picture_with_gif(imager)
176 raise ActiveRecord::Rollback unless res
181 def store_picture_with_gif(imager)
182 if res = self.store_picture(imager, self.filename)
184 if gifimager = imager.to_gif
185 if res = self.store_picture(gifimager, self.gifname)
187 self.errors.add :base, I18n.t('picture_io.error')
190 self.errors.add :base, I18n.t('errors.not_convert')
195 self.errors.add :base, I18n.t('picture_io.error')
200 def store_picture(imager, fn)
202 thumbnail_imager = self.license_extend.thumbnail >= 0 ? imager.to_thumbnail : imager
203 return false unless thumbnail_imager
205 PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
206 PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
208 rescue PictureIO::Error
214 def restore(subdir = nil)
215 PictureIO.resource_picture_io.get self.filename, subdir
220 ResourcePicture.transaction do
221 self.original_picture.published_at = nil
222 self.original_picture.stopped_at = Time.now
223 raise ActiveRecord::Rollback unless self.original_picture.save
224 self.original_picture.resource_picture_pictures.each do |resource_picture_picture|
225 resource_picture_picture.resource_picture_id = nil
226 raise ActiveRecord::Rollback unless resource_picture_picture.save
229 PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
230 PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
231 rescue PictureIO::Error
233 raise ActiveRecord::Rollback
236 raise ActiveRecord::Rollback unless res
241 def self.visible_count
242 ResourcePicture.count
246 Base64.encode64(self.restore 'full')
250 "#{self.license_group_module_name.tableize}/attributes/new"
254 "#{self.license_group_module_name.tableize}/attributes/credit"
257 def full_credit_template
258 "#{self.license_group_module_name.tableize}/attributes/full_credit"
262 ResourcePicture.find_each do |resource_picture|
263 resource_picture.boosts 'post'
264 full = resource_picture.restore 'full'
265 imager = PettanImager.load full
266 resource_picture.store_picture_with_gif(imager)