OSDN Git Service

t#30328:pull porting
[pettanr/pettanr.git] / app / models / original_picture.rb
index d67a581..17eed3a 100644 (file)
 class OriginalPicture < ActiveRecord::Base
   belongs_to :artist
-  belongs_to :lisence
+  belongs_to :original_picture_license_group
   has_one :resource_picture
+  has_many :pictures
   
-  def validate
-    errors.add(:filesize, 'size over(1MB)') if self.filesize > 1000000
+  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 => {:both => false}
+  validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
+  
+  def supply_default
+  end
+  
+  def overwrite ar
+    self.uploaded_at = Time.now
+    self.artist_id = ar.id
   end
   
-  def dext
-    self.ext.downcase
+  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.dext}"
+    "#{self.id}.#{self.ext}"
   end
   
   def mime_type
-    "image/#{self.dext}"
+    "image/#{self.ext}"
   end
   
   def url
     '/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 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
+    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.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 => {:resource_picture => {}, :pictures => {}}}
+  end
+  
+  def self.list_json_opt
+    {:include => {:resource_picture => {}, :pictures => {}}}
+  end
+  
+  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 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_opt
+    {:include => {:resource_picture => {}, :pictures => {}}}
+  end
+  
+  def self.show_json_opt
+    {:include => {:resource_picture => {}, :pictures => {}}}
+  end
+  
+  def store(imager)
+    unless imager
+      self.errors.add :base, I18n.t('errors.invalid_image')
+      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
+        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
     end
     res
   end
@@ -38,10 +160,47 @@ class OriginalPicture < ActiveRecord::Base
     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 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
   
 end