X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Foriginal_picture.rb;h=bd776a6890ad41aa8c38b5adb5d4fa42af82542c;hb=a0e7869980e9d497d325fddc7d4f5a3feea56738;hp=722e1104c640054a291ddec60995d35054e2d873;hpb=aec34f8b39f39ec0794d39c878659487909f0136;p=pettanr%2Fpettanr.git diff --git a/app/models/original_picture.rb b/app/models/original_picture.rb index 722e1104..bd776a68 100644 --- a/app/models/original_picture.rb +++ b/app/models/original_picture.rb @@ -1,19 +1,48 @@ class OriginalPicture < ActiveRecord::Base belongs_to :artist - belongs_to :license + belongs_to :original_picture_license_group has_one :resource_picture + has_many :pictures - validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']} + validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']} 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 + validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32} before_destroy :destroy_with_file - def supply_default art - self.license_id = art.default_license_id if self.license_id.blank? + def supply_default + end + + def overwrite ar + self.artist_id = ar.id + end + + def own? ar + self.artist_id == ar.id + end + + def visible? ar + 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 + 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 self.default_page_size @@ -37,36 +66,48 @@ class OriginalPicture < ActiveRecord::Base 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 + def history + Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} ) + end + def self.list_opt - {:include => [:license, :resource_picture]} + {:include => {:resource_picture => {}, :pictures => {}}} end def self.list_json_opt - {:include => [:license, :resource_picture]} + {:include => {:resource_picture => {}, :pictures => {}}} 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 + 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.show_include_opt opt = {} - res = [:license, :resource_picture] - res.push(opt[:include]) if opt[:include] + 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_json_include_opt - {:include => [:license, :resource_picture]} + def self.show_opt + {:include => {:resource_picture => {}, :pictures => {}}} + end + + def self.show_json_opt + {:include => {:resource_picture => {}, :pictures => {}}} end def destroy_with_file @@ -74,50 +115,16 @@ class OriginalPicture < ActiveRecord::Base 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 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, 'illegal picture data' 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 - } + 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) - 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 + if res = PictureIO.original_picture_io.put(imager.binary, self.filename) res = true else self.errors.add :base, 'original picture io does not work' @@ -132,9 +139,10 @@ class OriginalPicture < ActiveRecord::Base PictureIO.original_picture_io.get self.filename, subdir end - def own? art - return false unless art - self.artist_id == art.id + def self.export ar + l = LicenseGroup.list + op = OriginalPicture.list ar.id + {:license_groups => l, :original_pictures => op} end end