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 scope :find_by_md5, -> (md5) do
52 find_index.where(md5: md5)
55 def self.pickup_item_name
59 def self.pickup_column_name
60 self.pickup_item_name + '_id'
65 self.attributes[self.pickup_column_name]
72 attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize,
73 :original_picture_id => op.id, :artist_id => op.artist_id, :md5 => op.md5,
74 :created_at => Time.now, :updated_at => Time.now
76 self.attributes = attr
79 def visible? operators
81 # content model call to owner checker
82 self.user_visible? operators
86 "#{self.id}.#{self.ext}"
98 '/resource_pictures/' + filename + (subdir.to_s.empty? ? '' : '?subdir=' + subdir.to_s)
102 self.ext == 'png' and self.license_extend.gif_convert >= 0
105 def thumbnail(imager)
106 tw, th = ResourcePicture.fix_size_both(Manifest.manifest.magic_numbers['thumbnail_width'], Manifest.manifest.magic_numbers['thumbnail_height'], rimg.columns, rimg.rows)
107 ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
111 tw, th = PettanImager.thumbnail_size(self.width, self.height)
112 {:src => self.url, :width => tw, :height => th}
116 {:src => self.url('full'), :width => self.width, :height => self.height}
120 self.license.license_group.caption.to_s + '[' + self.license.caption.to_s + ']'
128 {:license => {}, :artist => {}, :picture => {} }
131 def self.list_json_opt
132 {:include => {:license => {}, :artist => {}, :picture => {}} }
136 {:include => {:license => {}, :artist => {}, :picture => {}} }
139 def self.show_json_opt
140 {:include => {:license => {}, :artist => {}, :picture => {}} }
143 def new_picture imager
150 self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
155 return false unless imager
157 self.overwrite self.original_picture
158 ResourcePicture.transaction do
159 self.original_picture.published_at = Time.now
160 self.original_picture.stopped_at = nil
161 raise ActiveRecord::Rollback unless self.original_picture.save
162 pc = self.new_picture imager
164 self.picture_id = pc.id
165 resource_picture_picture = ResourcePicturePicture.new(
166 :original_picture_id => self.original_picture_id,
167 :resource_picture_id => self.id,
170 raise ActiveRecord::Rollback unless resource_picture_picture.save
172 self.original_picture.resource_picture_pictures.each do |resource_picture_picture|
173 resource_picture_picture.resource_picture_id = self.id
174 raise ActiveRecord::Rollback unless resource_picture_picture.save
176 res = self.store_picture_with_gif(imager)
180 raise ActiveRecord::Rollback unless res
185 def store_picture_with_gif(imager)
186 if res = self.store_picture(imager, self.filename)
188 if gifimager = imager.to_gif
189 if res = self.store_picture(gifimager, self.gifname)
191 self.errors.add :base, I18n.t('picture_io.error')
194 self.errors.add :base, I18n.t('errors.not_convert')
199 self.errors.add :base, I18n.t('picture_io.error')
204 def store_picture(imager, fn)
206 thumbnail_imager = self.license_extend.thumbnail >= 0 ? imager.to_thumbnail : imager
207 return false unless thumbnail_imager
209 PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
210 PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
212 rescue PictureIO::Error
218 def restore(subdir = nil)
219 PictureIO.resource_picture_io.get self.filename, subdir
224 ResourcePicture.transaction do
225 self.original_picture.published_at = nil
226 self.original_picture.stopped_at = Time.now
227 raise ActiveRecord::Rollback unless self.original_picture.save
228 self.original_picture.resource_picture_pictures.each do |resource_picture_picture|
229 resource_picture_picture.resource_picture_id = nil
230 raise ActiveRecord::Rollback unless resource_picture_picture.save
233 PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
234 PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
235 rescue PictureIO::Error
237 raise ActiveRecord::Rollback
240 raise ActiveRecord::Rollback unless res
245 def self.visible_count
246 ResourcePicture.count
250 Base64.encode64(self.restore 'full')
254 "#{self.license_group_module_name.tableize}/attributes/new"
258 "#{self.license_group_module_name.tableize}/attributes/credit"
261 def full_credit_template
262 "#{self.license_group_module_name.tableize}/attributes/full_credit"
266 ResourcePicture.find_each do |resource_picture|
267 resource_picture.boosts 'post'
268 full = resource_picture.restore 'full'
269 imager = PettanImager.load full
270 resource_picture.store_picture_with_gif(imager)