OSDN Git Service

7bdaccaa1629326f19d3cff1465d3449dc5a4b9a
[pettanr/pettanr.git] / app / models / original_picture.rb
1 class OriginalPicture < ActiveRecord::Base
2   belongs_to :artist
3   belongs_to :original_picture_license_group
4   has_one :resource_picture
5   has_many :pictures
6   
7   validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
8   validates :width, :presence => true, :numericality => true, :natural_number => true
9   validates :height, :presence => true, :numericality => true, :natural_number => true
10   validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
11   validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
12   validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
13   
14   def supply_default
15   end
16   
17   def overwrite ar
18     self.artist_id = ar.id
19   end
20   
21   def own? ar
22     if ar.is_a?(Author)
23       self.artist_id == ar.artist.id
24     elsif ar.is_a?(Artist)
25       self.artist_id == ar.id
26     else
27       false
28     end
29   end
30   
31   def visible? ar
32     return true if ar.is_a?(Admin)
33     self.own?(ar)
34   end
35   
36   def filename
37     "#{self.id}.#{self.ext}"
38   end
39   
40   def mime_type
41     "image/#{self.ext}"
42   end
43   
44   def url
45     '/original_pictures/' + filename
46   end
47   
48   def opt_img_tag
49     {:src => self.url, :width => self.width, :height => self.height}
50   end
51   
52   def tmb_opt_img_tag
53     tw, th = PettanImager.thumbnail_size(self.width, self.height)
54     {:src => self.url, :width => tw, :height => th}
55   end
56   
57   def unpublished?
58     self.pictures.empty?
59   end
60   
61   def stopped?
62     self.pictures.any? and self.resource_picture == nil
63   end
64   
65   def unlicensed?
66     self.pictures.any? and self.resource_picture and self.updated_at > self.pictures.first.head.updated_at
67   end
68   
69   def published?
70     self.pictures.any? and self.resource_picture and self.updated_at < self.pictures.first.head.updated_at
71   end
72   
73   def self.default_page_size
74     25
75   end
76   
77   def self.max_page_size
78     100
79   end
80   
81   def self.page prm = nil
82     page = prm.to_i
83     page = 1 if page < 1
84     page
85   end
86   
87   def self.page_size prm = self.default_page_size
88     page_size = prm.to_i
89     page_size = self.max_page_size if page_size > self.max_page_size
90     page_size = self.default_page_size if page_size < 1
91     page_size
92   end
93   
94   def self.mylist artist_id, page = 1, page_size = self.default_page_size
95     opt = {}
96     opt.merge!(self.list_opt)
97     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
98     opt.merge!({:conditions => ['original_pictures.artist_id = ?', artist_id], :order => 'original_pictures.updated_at desc'})
99     OriginalPicture.find(:all, opt)
100   end
101   
102   def history 
103     Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
104   end
105   
106   def self.list_opt
107     {:include => {:resource_picture => {}, :pictures => {}}}
108   end
109   
110   def self.list_json_opt
111     {:include => {:resource_picture => {}, :pictures => {}}}
112   end
113   
114   def self.show cid, ar
115     opt = {}
116     opt.merge!(self.show_opt)
117     res = OriginalPicture.find(cid, opt)
118     raise ActiveRecord::Forbidden unless res.visible?(ar)
119     res
120   end
121   
122   def self.edit cid, ar
123     opt = {}
124     opt.merge!(self.show_opt)
125     res = OriginalPicture.find(cid, opt)
126     raise ActiveRecord::Forbidden unless res.own?(ar)
127     res
128   end
129   
130   def self.show_opt
131     {:include => {:resource_picture => {}, :pictures => {}}}
132   end
133   
134   def self.show_json_opt
135     {:include => {:resource_picture => {}, :pictures => {}}}
136   end
137   
138   def store(imager)
139     unless imager
140       self.errors.add :base, I18n.t('errors.invalid_image')
141       return false
142     end
143     res = false
144     self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
145     OriginalPicture.transaction do
146       if res = self.save
147         begin
148           res = PictureIO.original_picture_io.put(imager.binary, self.filename)
149         rescue PictureIO::Error
150           res = false
151           self.errors.add :base, I18n.t('picture_io.error')
152           raise ActiveRecord::Rollback
153         end
154       end
155     end
156     res
157   end
158   
159   def restore(subdir = nil)
160     PictureIO.original_picture_io.get self.filename, subdir
161   end
162   
163   def self.export(dt = nil)
164     opt = {}
165     cond = if dt
166       ['artists.author_id is not null and original_pictures.updated_at >= ?', dt]
167     else
168       'artists.author_id is not null'
169     end
170     opt.merge!({:conditions => cond}) 
171     opt.merge!({:include => {:resource_picture => {}, :artist => {}}, :order => 'original_pictures.id'})
172     OriginalPicture.find(:all, opt)
173   end
174   
175   def list_as_json_with_resource_picture
176     self.to_json({:include => {:resource_picture => {:methods => :picture_data}}})
177   end
178   
179   def self.list_as_json_text ary
180     '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']'
181   end
182   
183   def destroy_with_resource_picture
184     res = false
185     OriginalPicture.transaction do
186       begin
187         PictureIO.original_picture_io.delete(self.filename) if PictureIO.original_picture_io.exist?(self.filename)
188       rescue PictureIO::Error
189         res = false
190         raise ActiveRecord::Rollback
191       end
192       if self.resource_picture
193         res = self.resource_picture.unpublish
194         raise ActiveRecord::Rollback unless res
195       end
196       self.pictures.each do |picture|
197         res = picture.unpublish
198         raise ActiveRecord::Rollback unless res
199       end
200       res = self.destroy
201       raise ActiveRecord::Rollback unless res
202     end
203     res
204   end
205   
206 end