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}
60 self.history.first.revision
64 self.published_at == nil and self.stopped_at == nil
68 self.stopped_at != nil
72 dt = self.published_at || self.stopped_at
73 return false unless dt
78 self.published_at != nil
81 def self.default_page_size
85 def self.max_page_size
89 def self.page prm = nil
95 def self.page_size prm = self.default_page_size
97 page_size = self.max_page_size if page_size > self.max_page_size
98 page_size = self.default_page_size if page_size < 1
102 def self.mylist_where ar
103 ['original_pictures.artist_id = ?', ar.id]
106 def self.mylist ar, page = 1, page_size = self.default_page_size
107 OriginalPicture.where(self.mylist_where(ar)).includes(OriginalPicture.list_opt).order('original_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
110 def self.mylist_paginate ar, page = 1, page_size = self.default_page_size
111 Kaminari.paginate_array(Array.new(OriginalPicture.where(self.mylist_where(ar)).count, nil)).page(page).per(page_size)
115 {:resource_picture => {}, :pictures => {} }
118 def self.list_json_opt
119 {:include => {:resource_picture => {}, :pictures => {}}}
123 Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
126 def self.show cid, roles
128 opt.merge!(self.show_opt)
129 res = OriginalPicture.find(cid, opt)
130 raise ActiveRecord::Forbidden unless res.visible?(roles)
134 def self.edit cid, ar
136 opt.merge!(self.show_opt)
137 res = OriginalPicture.find(cid, opt)
138 raise ActiveRecord::Forbidden unless res.own?(ar)
143 {:include => {:resource_picture => {}, :pictures => {}}}
146 def self.show_json_opt
147 {:include => {:resource_picture => {}, :pictures => {}}}
152 self.errors.add :base, I18n.t('errors.invalid_image')
156 self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
157 OriginalPicture.transaction do
160 res = PictureIO.original_picture_io.put(imager.binary, self.filename)
161 rescue PictureIO::Error
163 self.errors.add :base, I18n.t('picture_io.error')
164 raise ActiveRecord::Rollback
171 def restore(subdir = nil)
172 PictureIO.original_picture_io.get self.filename, subdir
175 def self.export(dt = nil)
178 ['artists.author_id is not null and original_pictures.updated_at >= ?', dt]
180 'artists.author_id is not null'
182 opt.merge!({:conditions => cond})
183 opt.merge!({:include => {:resource_picture => {}, :artist => {}}, :order => 'original_pictures.id'})
184 OriginalPicture.find(:all, opt)
187 def list_as_json_with_resource_picture
188 self.to_json({:include => {:resource_picture => {:methods => :picture_data}}})
191 def self.list_as_json_text ary
192 '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']'
195 def destroy_with_resource_picture
197 OriginalPicture.transaction do
199 PictureIO.original_picture_io.delete(self.filename) if PictureIO.original_picture_io.exist?(self.filename)
200 rescue PictureIO::Error
202 raise ActiveRecord::Rollback
204 if self.resource_picture
205 res = self.resource_picture.unpublish
206 raise ActiveRecord::Rollback unless res
208 self.pictures.each do |picture|
209 res = picture.unpublish
210 raise ActiveRecord::Rollback unless res
213 raise ActiveRecord::Rollback unless res
218 def self.publish oid, lsname, attr
219 l = License.find_by_name lsname
220 op = OriginalPicture.find oid
222 attr[:license_id] = l.id
224 ctl = lg.classname.pluralize + '::Attribute'
225 le = ctl.constantize.new attr
227 rp = ResourcePicture.new
228 rp.attributes = le.resource_picture_attributes op
231 imager = PettanImager.load op.restore
235 def self.upload fn, auth
236 b = Base64.encode64(File.open(fn, 'rb').read)
237 u = 'http://localhost:3000/original_pictures'
238 r = RestClient.post(u,
239 {:original_picture => {:file => b}, :auth_token => auth}.to_json,
240 :content_type => :json, :accept => :json
247 def self.auto_publish dirname, auth
248 Dir.glob File.expand_path(dirname) + '/*' do |filename|
249 if File.directory?(filename)
253 Dir.glob(filename + '/*') do |fn|
254 ext = File.extname(fn).downcase
257 json = JSON.parse(File.open(fn).read)
258 lsname = json["license_name"]
259 attr = json["attributes"]
260 when '.png', '.gif', '.jpeg'
264 oid = OriginalPicture.upload img, auth
265 OriginalPicture.publish oid, lsname, attr