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 => {:minimum => 32, :maximum => 32}
- validates :artist_id, :presence => true, :numericality => true, :existence => true
- validates :license_id, :presence => true, :numericality => true, :existence => true
- validates :original_picture_id, :presence => true, :numericality => true, :existence => true
+ validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :license_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :original_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
validates :artist_name, :presence => true
validates :classname, :presence => true, :length => {:maximum => 50}
- validates :picture_id, :presence => true, :numericality => true, :existence => true
+ validates :picture_id, :presence => true, :numericality => true, :existence => {:both => false}
- before_destroy :destroy_with_file
+ before_validation :valid_encode
- def destroy_with_file
- PictureIO.resource_picture_io.delete self.filename
- PictureIO.resource_picture_io.class.subdirs.each do |d|
- next if d.empty?
- PictureIO.resource_picture_io.delete(self.filename, d) if PictureIO.resource_picture_io.exist?(self.filename, d)
+ def valid_encode
+ ['artist_name', 'classname', 'credit', 'settings'].each do |a|
+ next if attributes[a] == nil
+ raise Pettanr::BadRequest unless attributes[a].valid_encoding?
end
end
self.attributes = attr
end
- def own? author
- return false
+ def own? roles
+ roles = [roles] unless roles.respond_to?(:each)
+ ar = ResourcePicture.get_artist_from_roles roles
+ return false unless ar
+ self.artist_id == ar.id
end
- def visible? au
- if au == nil
- return false if MagicNumber['run_mode'] == 1
- elsif au.is_a?(Author)
- return true
+ def visible? roles
+ if MagicNumber['run_mode'] == 0
+ return false unless guest_role_check(roles)
else
- return false
+ return false unless resource_reader_role_check(roles)
end
true
end
ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
end
- def opt_img_tag
+ def tmb_opt_img_tag
tw, th = PettanImager.thumbnail_size(self.width, self.height)
{:src => self.url, :width => tw, :height => th}
end
+ def opt_img_tag
+ {:src => self.url('full'), :width => self.width, :height => self.height}
+ end
+
def self.default_page_size
- 25
+ 125
end
def self.max_page_size
{:include => {:license => {}, :artist => {}, :picture => {}} }
end
- def self.show rid, au
+ def self.mylist ar, page = 1, page_size = Author.default_resource_picture_page_size
+ opt = {}
+ opt.merge!(ResourcePicture.list_opt)
+ opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
+ opt.merge!({:conditions => ['resource_pictures.artist_id = ?', ar.id], :order => 'resource_pictures.updated_at desc'})
+ ResourcePicture.find(:all, opt)
+ end
+
+ def self.show rid, roles
opt = {}
opt.merge!(self.show_opt)
r = ResourcePicture.find(rid, opt)
- raise ActiveRecord::Forbidden unless r.visible?(au)
+ raise ActiveRecord::Forbidden unless r.visible?(roles)
r
end
{:include => {:license => {}, :artist => {}, :picture => {}} }
end
+ def self.edit rid, ar
+ opt = {}
+ opt.merge!(self.show_opt)
+ r = ResourcePicture.find(rid, opt)
+ raise ActiveRecord::Forbidden unless r.own?(ar)
+ r
+ end
+
def new_picture imager
pc = Picture.new
pc.supply_default
pc.overwrite self
r = pc.store imager
return pc if r
- self.errors.add :base, 'picture does not create' + pc.errors.full_messages.join("\n")
+ self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
false
end
res = false
self.overwrite self.original_picture
ResourcePicture.transaction do
+ self.original_picture.published_at = Time.now
+ self.original_picture.stopped_at = nil
+ raise ActiveRecord::Rollback unless self.original_picture.save
pc = self.new_picture imager
if pc
self.picture_id = pc.id
if res = self.store_picture(imager, self.filename)
if self.to_gif?
if gifimager = imager.to_gif
- res = self.store_picture(gifimager, self.gifname)
+ if res = self.store_picture(gifimager, self.gifname)
+ else
+ self.errors.add :base, I18n.t('picture_io.error')
+ end
else
- self.errors.add :base, 'picture data can not conv to gif'
+ self.errors.add :base, I18n.t('errors.not_convert')
res = false
end
end
else
- self.errors.add :base, 'resource picture io does not work'
+ self.errors.add :base, I18n.t('picture_io.error')
end
res
end
res = false
thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
return false unless thumbnail_imager
- return false unless PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
- return false unless PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
- true
+ begin
+ PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
+ PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
+ res = true
+ rescue PictureIO::Error
+ res = false
+ end
+ res
end
def restore(subdir = nil)
PictureIO.resource_picture_io.get self.filename, subdir
end
+ def unpublish
+ res = false
+ ResourcePicture.transaction do
+ self.original_picture.published_at = nil
+ self.original_picture.stopped_at = Time.now
+ raise ActiveRecord::Rollback unless self.original_picture.save
+ begin
+ PictureIO.resource_picture_io.delete(self.filename) if PictureIO.resource_picture_io.exist?(self.filename)
+ PictureIO.resource_picture_io.delete(self.filename, 'full') if PictureIO.resource_picture_io.exist?(self.filename, 'full')
+ rescue PictureIO::Error
+ res = false
+ raise ActiveRecord::Rollback
+ end
+ res = self.destroy
+ raise ActiveRecord::Rollback unless res
+ end
+ res
+ end
+
def self.visible_count
ResourcePicture.count
end
+ def picture_data
+ Base64.encode64(self.restore 'full')
+ end
+
def credit_template
"#{self.classname.tableize}/attributes/credit"
end