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 :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
validates :license_id, :presence => true, :numericality => true, :existence => true
validates :artist_id, :presence => true, :numericality => true, :existence => true
validates :artist_name, :presence => true
validates :classname, :presence => true, :length => {:maximum => 50}
- def dext
- self.ext.downcase
+ def supply_default
+ end
+
+ def overwrite rp
+ attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize,
+ :original_picture_id => rp.original_picture_id, :license_id => rp.license_id, :artist_id => rp.artist_id,
+ :md5 => rp.md5, :artist_name => rp.artist_name, :classname => rp.classname, :credit => rp.credit, :settings => rp.settings
+ }
+ self.attributes = attr
+ self.revision = self.new_revision #Do not move to attr. new_revision reffernces self.original_picture_id
+ end
+
+ def own? author
+ return false
+ end
+
+ def visible? author
+ true
end
def filename
- "#{self.id}.#{self.dext}"
+ "#{self.id}.#{self.ext}"
+ end
+
+ def gifname
+ "#{self.id}.gif"
end
def mime_type
- "image/#{self.dext}"
+ "image/#{self.ext}"
+ end
+
+ def url
+ '/pictures/' + filename
+ end
+
+ def opt_img_tag
+ tw, th = PettanImager.thumbnail_size(self.width, self.height)
+ {:src => self.url, :width => tw, :height => th}
end
def new_revision
Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1
end
- def v(rimg)
- rimg.flip.to_blob
+ def head?
+ r = self.resource_picture
+ r ? true : false
end
- def h(rimg)
- rimg.flop.to_blob
+ def to_gif?
+ self.ext == 'png' and self.flag_gif_convert >= 0
end
- def vh(rimg)
- rimg.flip.flop.to_blob
+ def subdirs
+ flag_reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
end
- def to_gif?
- self.dext == 'png' and self.flag_gif_convert >= 0
+ def self.find_by_md5 md5
+ r = Picture.find :all, :conditions => ['pictures.md5 = ?', md5], :order => 'pictures.updated_at desc'
end
- def self.png_to_gif(data)
- res = nil
- begin
- mgk = Magick::Image.from_blob(data).shift
- mgk.format = 'gif'
- res = mgk
- rescue
- res = false
- end
- res
+ def self.list_by_md5 md5, opid
+ r = Picture.find :all, :conditions => ['pictures.md5 = ? and pictures.original_picture_id <> ?', md5, opid], :order => 'pictures.updated_at desc'
end
- def subdirs
- flag_reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
+ def self.exist_by_md5 md5, opid
+ Picture.list_by_md5(md5, opid).empty? ? false : true
end
- def copy_data(rp)
- attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize,
- :original_picture_id => rp.original_picture_id, :license_id => rp.license_id, :artist_id => rp.artist_id,
- :artist_name => rp.artist_name, :classname => rp.classname, :credit => rp.credit, :settings => rp.settings
- }
- self.attributes = attr
+ def self.show rid, au
+ opt = {}
+ r = Picture.find(rid, opt)
+ raise ActiveRecord::Forbidden unless r.visible?(au)
+ r
end
- def store(mgk)
+ def store(imager)
res = false
- self.revision = self.new_revision
if res = self.save
- if res = self.store_picture(mgk)
+ if res = self.store_picture(imager, self.filename)
if self.to_gif?
- if gifmgk = Picture.png_to_gif(mgk.to_blob)
- res = self.store_picture(gifmgk)
+ if gifimager = imager.to_gif
+ res = self.store_picture(gifimager, self.gifname)
else
res = false
end
res
end
- def store_picture(mgk)
+ def store_picture(imager, fn)
res = false
subdirs.each do |d|
- picdata = d.empty? ? mgk.to_blob : self.__send__(d, mgk)
- res = PictureIO.picture_io.put(picdata, self.filename, d)
+ picdata = d.empty? ? imager.binary : imager.__send__(d)
+ res = PictureIO.picture_io.put(picdata, fn, d)
break unless res
end
res
end
def restore(subdir = nil)
- PictureIO.resource_picture_io.get self.filename, subdir
- end
-
- def self.show pid
- r = Picture.find(pid)
-# raise ActiveRecord::Forbidden unless c.visible?(au)
- r
+ PictureIO.picture_io.get self.filename, subdir
end
def credit_template
"#{self.classname.tableize}/attributes/credit"
end
+ def full_credit_template
+ "#{self.classname.tableize}/attributes/full_credit"
+ end
+
def credit_data
begin
@credit_data = JSON.parse(self.credit) unless @credit_data