1 class OriginalPicture < ActiveRecord::Base
4 has_one :resource_picture
6 before_destroy :destroy_with_file
9 errors.add(:filesize, 'size over(1MB)') if self.filesize > 1000000
12 def supply_default art
13 self.license_id = art.default_license_id if self.license_id.blank?
16 def self.default_page_size
20 def self.max_page_size
24 def self.page prm = nil
30 def self.page_size prm = self.default_page_size
32 page_size = self.max_page_size if page_size > self.max_page_size
33 page_size = self.default_page_size if page_size < 1
37 def self.list artist_id, opt = {}, page = 1, page_size = self.default_page_size
38 opt.merge!(self.list_opt) unless opt[:include]
39 opt.merge!({:conditions => ['artist_id = ?', artist_id],
40 :order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size}
42 OriginalPicture.find(:all, opt)
46 {:include => [:artist, :license, :resource_picture]}
49 def self.list_json_opt
50 {:include => [:resource_picture, :artist, :license]}
53 def self.show cid, author, opt = {}
54 pic = OriginalPicture.find(cid, :include => self.show_include_opt(opt))
55 raise ActiveRecord::Forbidden unless pic.own?(author)
59 def self.show_include_opt opt = {}
61 res.push(opt[:include]) if opt[:include]
65 def self.show_json_include_opt
66 {:include => :license}
70 PictureIO.original_picture_io.delete self.filename
71 self.resource_picture.destroy
79 "#{self.id}.#{self.dext}"
87 '/original_pictures/' + filename
90 def store(picture_data, art, lid = nil)
93 mgk = Magick::Image.from_blob(picture_data).shift
95 self.errors.add :base, 'magick failed'
98 self.attributes = {:ext => mgk.format.downcase, :width => mgk.columns, :height => mgk.rows,
99 :filesize => mgk.filesize, :artist_id => art.id,
100 :license_id => lid.blank? ? art.default_license_id : lid.to_i
102 OriginalPicture.transaction do
104 if res = PictureIO.original_picture_io.put(picture_data, self.filename)
105 rp = ResourcePicture.update_picture(self)
107 PictureIO.original_picture_io.delete(self.filename)
108 self.errors.add :base, 'resource picture copying error'
109 raise ActiveRecord::Rollback
112 self.errors.add :base, 'original picture io does not work'
113 raise ActiveRecord::Rollback
120 def restore(subdir = nil)
121 PictureIO.original_picture_io.get self.filename, subdir
125 return false unless author
126 return false unless author.artist?
127 self.artist_id == author.artist.id