X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Foriginal_picture.rb;h=28bf7b6138187baa82e32ba0ac937fb58da0167c;hb=e6ae2d4d0e9cec1828ef7e4bec617d7f925d8133;hp=1363cc9a67680cc154325ee526f1afbc4ba7d448;hpb=f8cd0347c685363a6ab296ca5bcf42c158e5e25f;p=pettanr%2Fpettanr.git diff --git a/app/models/original_picture.rb b/app/models/original_picture.rb index 1363cc9a..28bf7b61 100644 --- a/app/models/original_picture.rb +++ b/app/models/original_picture.rb @@ -1,16 +1,73 @@ class OriginalPicture < ActiveRecord::Base belongs_to :artist + 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 :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.uploaded_at = Time.now + self.artist_id = ar.id + end + + def own? roles + roles = [roles] unless roles.respond_to?(:each) + ar = OriginalPicture.get_artist_from_roles roles + return false unless ar + self.artist_id == ar.id + end + + def visible? roles + return true if self.admin_role_check roles + self.own?(roles) + end + + def filename + "#{self.id}.#{self.ext}" + end - def supply_default art + 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 tmb_opt_img_tag + tw, th = PettanImager.thumbnail_size(self.width, self.height) + {:src => self.url, :width => tw, :height => th} + end + + def unpublished? + self.published_at == nil and self.stopped_at == nil + end + + def stopped? + self.stopped_at != nil + end + + def unlicensed? + dt = self.published_at || self.stopped_at + return false unless dt + self.uploaded_at > dt + end + + def published? + self.published_at != nil end def self.default_page_size @@ -34,82 +91,68 @@ 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} - ) - OriginalPicture.find(:all, opt) - end - - def self.list_opt - {:include => {:resource_picture => {}}} + def self.mylist_where ar + ['original_pictures.artist_id = ?', ar.id] end - def self.list_json_opt - {:include => {:resource_picture => {}}} + def self.mylist ar, page = 1, page_size = self.default_page_size + OriginalPicture.where(self.mylist_where(ar)).includes(OriginalPicture.list_opt).order('original_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size) 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.mylist_paginate ar, page = 1, page_size = self.default_page_size + Kaminari.paginate_array(Array.new(OriginalPicture.where(self.mylist_where(ar)).count, nil)).page(page).per(page_size) end - def self.show_include_opt opt = {} - res = {:resource_picture => {}} - res.merge!(opt[:include]) if opt[:include] - res + def self.list_opt + {:resource_picture => {}, :pictures => {} } end - def self.show_json_include_opt - {:include => {:resource_picture => {}}} + def self.list_json_opt + {:include => {:resource_picture => {}, :pictures => {}}} end - def destroy_with_file - PictureIO.original_picture_io.delete self.filename - self.resource_picture.destroy + def history + Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} ) end - def dext - self.ext.downcase + def self.show cid, roles + opt = {} + opt.merge!(self.show_opt) + res = OriginalPicture.find(cid, opt) + raise ActiveRecord::Forbidden unless res.visible?(roles) + res end - def filename - "#{self.id}.#{self.dext}" + 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 mime_type - "image/#{self.dext}" + def self.show_opt + {:include => {:resource_picture => {}, :pictures => {}}} end - def url - '/original_pictures/' + filename + def self.show_json_opt + {:include => {:resource_picture => {}, :pictures => {}}} 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.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 @@ -121,9 +164,99 @@ 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(dt = nil) + opt = {} + cond = if dt + ['artists.author_id is not null and original_pictures.updated_at >= ?', dt] + else + 'artists.author_id is not null' + end + opt.merge!({:conditions => cond}) + opt.merge!({:include => {:resource_picture => {}, :artist => {}}, :order => 'original_pictures.id'}) + OriginalPicture.find(:all, opt) + end + + def list_as_json_with_resource_picture + self.to_json({:include => {:resource_picture => {:methods => :picture_data}}}) + end + + def self.list_as_json_text ary + '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']' + end + + 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.publish oid, lsname, attr + l = License.find_by_name lsname + op = OriginalPicture.find oid + lg = l.license_group + attr[:license_id] = l.id + + ctl = lg.classname.pluralize + '::Attribute' + le = ctl.constantize.new attr + + rp = ResourcePicture.new + rp.attributes = le.resource_picture_attributes op + rp.overwrite op + + imager = PettanImager.load op.restore + rp.store imager + end + + def self.upload fn, auth + b = Base64.encode64(File.open(fn, 'rb').read) + u = 'http://localhost:3000/original_pictures' + r = RestClient.post(u, + {:original_picture => {:file => b}, :auth_token => auth}.to_json, + :content_type => :json, :accept => :json + ) + o = JSON.parse r + oid = o['id'] + oid + end + + def self.auto_publish dirname, auth + Dir.glob File.expand_path(dirname) + '/*' do |filename| + if File.directory?(filename) + img = nil + lsname = nil + attr = nil + Dir.glob(filename + '/*') do |fn| + ext = File.extname(fn).downcase + case ext + when '.json' + json = JSON.parse(File.open(fn).read) + lsname = json["license_name"] + attr = json["attributes"] + when '.png', '.gif', '.jpeg' + img = fn + end + end + oid = OriginalPicture.upload img, auth + OriginalPicture.publish oid, lsname, attr + end + end end end