OSDN Git Service

f55c06b75d1c605d12e642b393aee268b81cef6d
[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 destroy_with_resource_picture
164     res = false
165     OriginalPicture.transaction do
166       begin
167         PictureIO.original_picture_io.delete(self.filename) if PictureIO.original_picture_io.exist?(self.filename)
168       rescue PictureIO::Error
169         res = false
170         raise ActiveRecord::Rollback
171       end
172       if self.resource_picture
173         res = self.resource_picture.unpublish
174         raise ActiveRecord::Rollback unless res
175       end
176       self.pictures.each do |picture|
177         res = picture.unpublish
178         raise ActiveRecord::Rollback unless res
179       end
180       res = self.destroy
181       raise ActiveRecord::Rollback unless res
182     end
183     res
184   end
185   
186   def self.export ar
187     l = LicenseGroup.list
188     op = OriginalPicture.list ar.id
189     {:license_groups => l, :original_pictures => op}
190   end
191   
192 end