class OriginalPicture < ActiveRecord::Base
belongs_to :artist
- belongs_to :lisence
+ belongs_to :license
has_one :resource_picture
+ validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}\r
+ 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 :license_id, :presence => true, :numericality => true, :existence => true
+
before_destroy :destroy_with_file
def validate
errors.add(:filesize, 'size over(1MB)') if self.filesize > 1000000
end
+ def supply_default art
+ self.license_id = art.default_license_id if self.license_id.blank?
+ end
+
+ def self.default_page_size
+ 25
+ end
+
+ def self.max_page_size
+ 100
+ end
+
+ def self.page prm = nil
+ page = prm.to_i
+ page = 1 if page < 1
+ page
+ end
+
+ def self.page_size prm = self.default_page_size
+ page_size = prm.to_i
+ page_size = self.max_page_size if page_size > self.max_page_size
+ page_size = self.default_page_size if page_size < 1
+ 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}
+ )
+ OriginalPicture.find(:all, opt)
+ end
+
+ def self.list_opt
+ {:include => [:license, :resource_picture]}
+ end
+
+ def self.list_json_opt
+ {:include => [:license, :resource_picture]}
+ end
+
+ def self.show cid, artist, opt = {}
+ pic = OriginalPicture.find(cid, :include => self.show_include_opt(opt))
+ raise ActiveRecord::Forbidden unless pic.own?(artist)
+ pic
+ end
+
+ def self.show_include_opt opt = {}
+ res = [:license, :resource_picture]
+ res.push(opt[:include]) if opt[:include]
+ res
+ end
+
+ def self.show_json_include_opt
+ {:include => [:license, :resource_picture]}
+ end
+
def destroy_with_file
PictureIO.original_picture_io.delete self.filename
self.resource_picture.destroy
'/original_pictures/' + filename
end
- def store(rimg)
- bindata = rimg.to_blob
- PictureIO.original_picture_io.put bindata, self.filename
- res = if self.resource_picture
- self.resource_picture.store rimg
- else
- ResourcePicture.store(rimg, self)
+ def data_to_mgk picture_data
+ begin
+ mgk = Magick::Image.from_blob(picture_data).shift
+ rescue
+ self.errors.add :base, 'magick failed'
+ return false
+ end
+ mgk
+ end
+
+ def store(picture_data, art, lid = nil)
+ 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,
+ :license_id => lid.blank? ? art.default_license_id : lid.to_i
+ }
+ OriginalPicture.transaction do
+ if res = self.save
+ if res = PictureIO.original_picture_io.put(picture_data, self.filename)
+ rp = ResourcePicture.update_picture(self)
+ unless rp.store(mgk)
+ res = false
+ PictureIO.original_picture_io.delete(self.filename)
+ self.errors.add :base, 'resource picture copying error'
+ raise ActiveRecord::Rollback
+ end
+ res = true
+ else
+ self.errors.add :base, 'original picture io does not work'
+ raise ActiveRecord::Rollback
+ end
+ end
end
res
end
PictureIO.original_picture_io.get self.filename, subdir
end
- def own? author
- return false unless author
- return false unless author.artist?
- self.artist_id == author.artist.id
+ def own? art
+ return false unless art
+ self.artist_id == art.id
end
end