1 class OriginalPicture < ActiveRecord::Base
3 belongs_to :original_picture_license_group
4 has_one :resource_picture
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}
18 self.uploaded_at = Time.now
19 self.artist_id = ar.id
23 roles = [roles] unless roles.respond_to?(:each)
24 ar = OriginalPicture.get_artist_from_roles roles
25 return false unless ar
26 self.artist_id == ar.id
30 return true if self.admin_role_check roles
35 "#{self.id}.#{self.ext}"
43 '/original_pictures/' + filename
47 {:src => self.url, :width => self.width, :height => self.height}
51 tw, th = PettanImager.thumbnail_size(self.width, self.height)
52 {:src => self.url, :width => tw, :height => th}
56 self.published_at == nil and self.stopped_at == nil
60 self.stopped_at != nil
64 dt = self.published_at || self.stopped_at
65 return false unless dt
70 self.published_at != nil
73 def self.default_page_size
77 def self.max_page_size
81 def self.page prm = nil
87 def self.page_size prm = self.default_page_size
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
94 def self.mylist artist_id, page = 1, page_size = self.default_page_size
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)
103 Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
107 {:include => {:resource_picture => {}, :pictures => {}}}
110 def self.list_json_opt
111 {:include => {:resource_picture => {}, :pictures => {}}}
114 def self.show cid, roles
116 opt.merge!(self.show_opt)
117 res = OriginalPicture.find(cid, opt)
118 raise ActiveRecord::Forbidden unless res.visible?(roles)
122 def self.edit cid, ar
124 opt.merge!(self.show_opt)
125 res = OriginalPicture.find(cid, opt)
126 raise ActiveRecord::Forbidden unless res.own?(ar)
131 {:include => {:resource_picture => {}, :pictures => {}}}
134 def self.show_json_opt
135 {:include => {:resource_picture => {}, :pictures => {}}}
140 self.errors.add :base, I18n.t('errors.invalid_image')
144 self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
145 OriginalPicture.transaction do
148 res = PictureIO.original_picture_io.put(imager.binary, self.filename)
149 rescue PictureIO::Error
151 self.errors.add :base, I18n.t('picture_io.error')
152 raise ActiveRecord::Rollback
159 def restore(subdir = nil)
160 PictureIO.original_picture_io.get self.filename, subdir
163 def self.export(dt = nil)
166 ['artists.author_id is not null and original_pictures.updated_at >= ?', dt]
168 'artists.author_id is not null'
170 opt.merge!({:conditions => cond})
171 opt.merge!({:include => {:resource_picture => {}, :artist => {}}, :order => 'original_pictures.id'})
172 OriginalPicture.find(:all, opt)
175 def list_as_json_with_resource_picture
176 self.to_json({:include => {:resource_picture => {:methods => :picture_data}}})
179 def self.list_as_json_text ary
180 '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']'
183 def destroy_with_resource_picture
185 OriginalPicture.transaction do
187 PictureIO.original_picture_io.delete(self.filename) if PictureIO.original_picture_io.exist?(self.filename)
188 rescue PictureIO::Error
190 raise ActiveRecord::Rollback
192 if self.resource_picture
193 res = self.resource_picture.unpublish
194 raise ActiveRecord::Rollback unless res
196 self.pictures.each do |picture|
197 res = picture.unpublish
198 raise ActiveRecord::Rollback unless res
201 raise ActiveRecord::Rollback unless res
206 def self.publish oid, lsname, attr
207 l = License.find_by_name lsname
208 op = OriginalPicture.find oid
210 attr[:license_id] = l.id
212 ctl = lg.classname.pluralize + '::Attribute'
213 le = ctl.constantize.new attr
215 rp = ResourcePicture.new
216 rp.attributes = le.resource_picture_attributes op
219 imager = PettanImager.load op.restore
223 def self.upload fn, auth
224 b = Base64.encode64(File.open(fn, 'rb').read)
225 u = 'http://localhost:3000/original_pictures'
226 r = RestClient.post(u,
227 {:original_picture => {:file => b}, :auth_token => auth}.to_json,
228 :content_type => :json, :accept => :json
235 def self.auto_publish dirname, auth
236 Dir.glob File.expand_path(dirname) + '/*' do |filename|
237 if File.directory?(filename)
241 Dir.glob(filename + '/*') do |fn|
242 ext = File.extname(fn).downcase
245 json = JSON.parse(File.open(fn).read)
246 lsname = json["license_name"]
247 attr = json["attributes"]
248 when '.png', '.gif', '.jpeg'
252 oid = OriginalPicture.upload img, auth
253 OriginalPicture.publish oid, lsname, attr