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 :artist_id, :presence => true, :numericality => true, :existence => true
- validates :md5, :presence => true, :length => {:maximum => 32}
+ validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
- before_destroy :destroy_with_file
+ def supply_default
+ end
+
+ def overwrite ar
+ self.artist_id = ar.id
+ end
+
+ def own? ar
+ if ar.is_a?(Author)
+ self.artist_id == ar.artist.id
+ elsif ar.is_a?(Artist)
+ self.artist_id == ar.id
+ else
+ false
+ end
+ end
+
+ def visible? ar
+ return true if ar.is_a?(Admin)
+ self.own?(ar)
+ end
+
+ def filename
+ "#{self.id}.#{self.ext}"
+ end
+
+ def mime_type
+ "image/#{self.ext}"
+ end
+
+ def url
+ '/original_pictures/' + filename
+ end
+
+ def opt_img_tag
+ {:src => self.url, :width => self.width, :height => self.height}
+ end
- def supply_default art
+ def tmb_opt_img_tag
+ tw, th = PettanImager.thumbnail_size(self.width, self.height)
+ {:src => self.url, :width => tw, :height => th}
end
- def overwrite art
- return false unless art
- self.artist_id = art.id
+ def unpublished?
+ self.pictures.empty?
+ end
+
+ def stopped?
+ self.pictures.any? and self.resource_picture == nil
+ end
+
+ def unlicensed?
+ self.pictures.any? and self.resource_picture and self.updated_at > self.pictures.first.head.updated_at
+ end
+
+ def published?
+ self.pictures.any? and self.resource_picture and self.updated_at < self.pictures.first.head.updated_at
end
def self.default_page_size
page_size
end
- def self.list artist_id, opt = {}, page = 1, page_size = self.default_page_size
- opt.merge!(self.list_opt) unless opt[:include]
- opt.merge!({:conditions => ['artist_id = ?', artist_id],
- :order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size}
- )
+ def self.mylist artist_id, page = 1, page_size = self.default_page_size
+ opt = {}
+ opt.merge!(self.list_opt)
+ opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
+ opt.merge!({:conditions => ['original_pictures.artist_id = ?', artist_id], :order => 'original_pictures.updated_at desc'})
OriginalPicture.find(:all, opt)
end
{:include => {:resource_picture => {}, :pictures => {}}}
end
- def self.show cid, artist, opt = {}
- pic = OriginalPicture.find(cid, self.show_include_opt)
- raise ActiveRecord::Forbidden unless pic.own?(artist)
- pic
+ def self.show cid, ar
+ opt = {}
+ opt.merge!(self.show_opt)
+ res = OriginalPicture.find(cid, opt)
+ raise ActiveRecord::Forbidden unless res.visible?(ar)
+ res
end
- def self.edit cid, artist, opt = {}
- pic = OriginalPicture.find(cid, self.show_include_opt)
- raise ActiveRecord::Forbidden unless pic.own?(artist)
- pic
+ def self.edit cid, ar
+ opt = {}
+ opt.merge!(self.show_opt)
+ res = OriginalPicture.find(cid, opt)
+ raise ActiveRecord::Forbidden unless res.own?(ar)
+ res
end
- def self.show_include_opt
+ def self.show_opt
{:include => {:resource_picture => {}, :pictures => {}}}
end
- def self.show_json_include_opt
+ def self.show_json_opt
{:include => {:resource_picture => {}, :pictures => {}}}
end
- def destroy_with_file
- PictureIO.original_picture_io.delete self.filename
- self.resource_picture.destroy
- end
-
- def dext
- self.ext.downcase
- end
-
- def filename
- "#{self.id}.#{self.dext}"
- end
-
- def mime_type
- "image/#{self.dext}"
- end
-
- def url
- '/original_pictures/' + filename
- end
-
- def opt_img_tag
- tw, th = ResourcePicture.fix_size_both(MagicNumber['thumbnail_width'], MagicNumber['thumbnail_height'], self.width, self.height)
- {:src => self.url, :width => tw, :height => th}
- end
-
- def data_to_mgk picture_data
- begin
- mgk = Magick::Image.from_blob(picture_data).shift
- rescue
- self.errors.add :base, 'magick failed'
+ def store(imager)
+ unless imager
+ self.errors.add :base, I18n.t('errors.invalid_image')
return false
end
- mgk
- end
-
- def store(picture_data, art)
- mgk = data_to_mgk picture_data
- return false unless mgk
res = false
- self.attributes = {:ext => mgk.format.downcase, :width => mgk.columns, :height => mgk.rows,
- :filesize => mgk.filesize, :artist_id => art.id
- }
- self.md5 = Digest::MD5.hexdigest(picture_data)
- self.overwrite art
+ self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
OriginalPicture.transaction do
if res = self.save
- if res = PictureIO.original_picture_io.put(picture_data, self.filename)
- res = true
- else
- self.errors.add :base, 'original picture io does not work'
+ begin
+ res = PictureIO.original_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
PictureIO.original_picture_io.get self.filename, subdir
end
- def own? art
- return false unless art
- self.artist_id == art.id
+ def destroy_with_resource_picture
+ res = false
+ OriginalPicture.transaction do
+ begin
+ PictureIO.original_picture_io.delete(self.filename) if PictureIO.original_picture_io.exist?(self.filename)
+ rescue PictureIO::Error
+ res = false
+ raise ActiveRecord::Rollback
+ end
+ if self.resource_picture
+ res = self.resource_picture.unpublish
+ raise ActiveRecord::Rollback unless res
+ end
+ self.pictures.each do |picture|
+ res = picture.unpublish
+ raise ActiveRecord::Rollback unless res
+ end
+ res = self.destroy
+ raise ActiveRecord::Rollback unless res
+ end
+ res
+ end
+
+ def self.export ar
+ l = LicenseGroup.list
+ op = OriginalPicture.list ar.id
+ {:license_groups => l, :original_pictures => op}
end
end