X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Foriginal_picture.rb;h=110987a1cd4bd58b32e5e4149fca23f4dd7e8d2a;hb=fb0b22756b8a05bec524267aaec99c5935dc6803;hp=c0032f14e5c626a77a3ffeeb4419fca847d7a303;hpb=0c5d8c259856ee45e5395247ef8906a5c0a2daaf;p=pettanr%2Fpettanr.git diff --git a/app/models/original_picture.rb b/app/models/original_picture.rb index c0032f14..110987a1 100644 --- a/app/models/original_picture.rb +++ b/app/models/original_picture.rb @@ -1,16 +1,59 @@ 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 :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 => {:minimum => 32, :maximum => 32} before_destroy :destroy_with_file - def validate - errors.add(:filesize, 'size over(1MB)') if self.filesize > 1000000 + 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 supply_default art - self.license_id = art.default_license_id if self.license_id.blank? + def url + '/original_pictures/' + filename + end + + def opt_img_tag + {:src => self.url, :width => self.width, :height => self.height} + end + + def tmb_opt_img_tag + tw, th = PettanImager.thumbnail_size(self.width, self.height) + {:src => self.url, :width => tw, :height => th} end def self.default_page_size @@ -34,36 +77,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 => [:artist, :license, :resource_picture]} + {:include => {:resource_picture => {}, :pictures => {}}} end def self.list_json_opt - {:include => [:resource_picture, :artist, :license]} + {:include => {:resource_picture => {}, :pictures => {}}} end - def self.show cid, author, opt = {} - pic = OriginalPicture.find(cid, :include => self.show_include_opt(opt)) - raise ActiveRecord::Forbidden unless pic.own?(author) - 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] - 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} + def self.show_opt + {:include => {:resource_picture => {}, :pictures => {}}} + end + + def self.show_json_opt + {:include => {:resource_picture => {}, :pictures => {}}} end def destroy_with_file @@ -71,41 +126,48 @@ 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}" + def store(imager) + unless imager + self.errors.add :base, 'illegal picture data' + return false + end + res = false + 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(imager.binary, self.filename) + res = true + else + self.errors.add :base, 'original picture io does not work' + raise ActiveRecord::Rollback + end + end + end + res end - def url - '/original_pictures/' + filename + def restore(subdir = nil) + PictureIO.original_picture_io.get self.filename, subdir 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 + def self.export(dt = nil) + opt = {} + cond = if dt + ['artists.author_id is not null and original_pictures.updated_at >= ?', dt] else - ResourcePicture.store(rimg, self) + 'artists.author_id is not null' end - res + opt.merge!({:conditions => cond}) + opt.merge!({:include => {:resource_picture => {}, :artist => {}}, :order => 'original_pictures.id'}) + OriginalPicture.find(:all, opt) end - def restore(subdir = nil) - PictureIO.original_picture_io.get self.filename, subdir + def list_as_json_with_resource_picture + self.to_json({:include => {:resource_picture => {:methods => :picture_data}}}) end - def own? author - return false unless author - return false unless author.artist? - self.artist_id == author.artist.id + def self.list_as_json_text ary + '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']' end end