1 class OriginalPicture < Peta::Content
4 belongs_to :original_picture_license_group
5 has_one :resource_picture
7 has_many :resource_picture_pictures
9 validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
10 validates :width, :presence => true, :numericality => true, :natural_number => true
11 validates :height, :presence => true, :numericality => true, :natural_number => true
12 validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
13 validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
14 validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
20 def overwrite operators
21 self.uploaded_at = Time.now
22 return unless operators.artist
23 self.artist_id = operators.artist.id
26 def visible? operators
30 "#{self.id}.#{self.ext}"
38 '/original_pictures/' + filename
42 {:src => self.url, :width => self.width, :height => self.height}
46 tw, th = PettanImager.thumbnail_size(self.width, self.height)
47 {:src => self.url, :width => tw, :height => th}
59 head = self.history.first
60 head ? head.revision : 'unpublished'
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
82 Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
86 {:include => {:resource_picture => {}, :pictures => {}}}
91 self.errors.add :base, I18n.t('errors.invalid_image')
95 self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
96 OriginalPicture.transaction do
99 res = PictureIO.original_picture_io.put(imager.binary, self.filename)
100 rescue PictureIO::Error
102 self.errors.add :base, I18n.t('picture_io.error')
103 raise ActiveRecord::Rollback
110 def restore(subdir = nil)
111 PictureIO.original_picture_io.get self.filename, subdir
114 def self.export(dt = nil)
117 ['artists.author_id is not null and original_pictures.updated_at >= ?', dt]
119 'artists.author_id is not null'
121 opt.merge!({:conditions => cond})
122 opt.merge!({:include => {:resource_picture => {}, :artist => {}}, :order => 'original_pictures.id'})
123 OriginalPicture.find(:all, opt)
126 def list_as_json_with_resource_picture
127 self.to_json({:include => {:resource_picture => {:methods => :picture_data}}})
130 def self.list_as_json_text ary
131 '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']'
134 def destroy_with_resource_picture
136 OriginalPicture.transaction do
138 PictureIO.original_picture_io.delete(self.filename) if PictureIO.original_picture_io.exist?(self.filename)
139 rescue PictureIO::Error
141 raise ActiveRecord::Rollback
143 if self.resource_picture
144 res = self.resource_picture.unpublish
145 raise ActiveRecord::Rollback unless res
147 self.pictures.each do |picture|
148 res = picture.unpublish
149 raise ActiveRecord::Rollback unless res
150 if picture.resource_picture_picture
151 picture.resource_picture_picture.original_picture_id = nil
152 picture.resource_picture_picture.resource_picture_id = nil
153 raise ActiveRecord::Rollback unless picture.resource_picture_picture.save
157 raise ActiveRecord::Rollback unless res
162 def self.publish oid, lsname, attr
163 l = License.find_by_name lsname
164 op = OriginalPicture.find oid
166 attr[:license_id] = l.id
168 ctl = lg.module_name.pluralize + '::Attribute'
169 le = ctl.constantize.new attr
171 rp = ResourcePicture.new
172 rp.attributes = le.resource_picture_attributes op
175 imager = PettanImager.load op.restore
179 def self.upload fn, auth
180 b = Base64.encode64(File.open(fn, 'rb').read)
181 u = 'http://localhost:3000/original_pictures'
182 r = RestClient.post(u,
183 {:original_picture => {:file => b}, :auth_token => auth}.to_json,
184 :content_type => :json, :accept => :json
191 def self.auto_publish dirname, auth
192 Dir.glob File.expand_path(dirname) + '/*' do |filename|
193 if File.directory?(filename)
197 Dir.glob(filename + '/*') do |fn|
198 ext = File.extname(fn).downcase
201 json = JSON.parse(File.open(fn).read)
202 lsname = json["license_name"]
203 attr = json["attributes"]
204 when '.png', '.gif', '.jpeg'
208 oid = OriginalPicture.upload img, auth
209 OriginalPicture.publish oid, lsname, attr