OSDN Git Service

t#29050:fix edit permissin
[pettanr/pettanr.git] / app / models / original_picture.rb
1 class OriginalPicture < ActiveRecord::Base
2   belongs_to :artist
3   belongs_to :license
4   has_one :resource_picture
5   
6   validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}\r
7   validates :width, :presence => true, :numericality => true, :natural_number => true
8   validates :height, :presence => true, :numericality => true, :natural_number => true
9   validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
10   validates :artist_id, :presence => true, :numericality => true, :existence => true
11   validates :license_id, :presence => true, :numericality => true, :existence => true
12   
13   before_destroy :destroy_with_file
14   
15   def supply_default art
16     self.license_id = art.default_license_id if self.license_id.blank?
17   end
18   
19   def self.default_page_size
20     25
21   end
22   
23   def self.max_page_size
24     100
25   end
26   
27   def self.page prm = nil
28     page = prm.to_i
29     page = 1 if page < 1
30     page
31   end
32   
33   def self.page_size prm = self.default_page_size
34     page_size = prm.to_i
35     page_size = self.max_page_size if page_size > self.max_page_size
36     page_size = self.default_page_size if page_size < 1
37     page_size
38   end
39   
40   def self.list artist_id, opt = {}, page = 1, page_size = self.default_page_size
41     opt.merge!(self.list_opt) unless opt[:include]
42     opt.merge!({:conditions => ['artist_id = ?', artist_id], 
43       :order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size}
44     )
45     OriginalPicture.find(:all, opt)
46   end
47   
48   def self.list_opt
49     {:include => [:license, :resource_picture]}
50   end
51   
52   def self.list_json_opt
53     {:include => [:license, :resource_picture]}
54   end
55   
56   def self.show cid, artist, opt = {}
57     pic = OriginalPicture.find(cid, :include => self.show_include_opt(opt))
58     raise ActiveRecord::Forbidden unless pic.own?(artist)
59     pic
60   end
61   
62   def self.edit cid, artist, opt = {}
63     pic = OriginalPicture.find(cid, :include => self.show_include_opt(opt))
64     raise ActiveRecord::Forbidden unless pic.own?(artist)
65     pic
66   end
67   
68   def self.show_include_opt opt = {}
69     res = [:license, :resource_picture]
70     res.push(opt[:include]) if opt[:include]
71     res
72   end
73   
74   def self.show_json_include_opt
75     {:include => [:license, :resource_picture]}
76   end
77   
78   def destroy_with_file
79     PictureIO.original_picture_io.delete self.filename
80     self.resource_picture.destroy
81   end
82   
83   def dext
84     self.ext.downcase
85   end
86   
87   def filename
88     "#{self.id}.#{self.dext}"
89   end
90   
91   def mime_type
92     "image/#{self.dext}"
93   end
94   
95   def url
96     '/original_pictures/' + filename
97   end
98   
99   def data_to_mgk picture_data
100     begin
101       mgk = Magick::Image.from_blob(picture_data).shift
102     rescue 
103       self.errors.add :base, 'magick failed'
104       return false
105     end
106     mgk
107   end
108   
109   def store(picture_data, art, lid = nil)
110     mgk = data_to_mgk picture_data
111     return false unless mgk
112     res = false
113     self.attributes = {:ext => mgk.format.downcase, :width => mgk.columns, :height => mgk.rows, 
114       :filesize => mgk.filesize, :artist_id => art.id, 
115       :license_id => lid.blank? ? art.default_license_id : lid.to_i
116     }
117     OriginalPicture.transaction do
118       if res = self.save
119         if res = PictureIO.original_picture_io.put(picture_data, self.filename)
120           rp = ResourcePicture.update_picture(self)
121           unless rp.store(mgk)
122             res = false
123             PictureIO.original_picture_io.delete(self.filename)
124             self.errors.add :base, 'resource picture copying error'
125             raise ActiveRecord::Rollback
126           end
127           res = true
128         else
129           self.errors.add :base, 'original picture io does not work'
130           raise ActiveRecord::Rollback
131         end
132       end
133     end
134     res
135   end
136   
137   def restore(subdir = nil)
138     PictureIO.original_picture_io.get self.filename, subdir
139   end
140   
141   def own? art
142     return false unless art
143     self.artist_id == art.id
144   end
145   
146 end