+ def self.export(dt = nil)
+ opt = {}
+ cond = if dt
+ ['artists.author_id is not null and original_pictures.updated_at >= ?', dt]
+ else
+ 'artists.author_id is not null'
+ end
+ opt.merge!({:conditions => cond})
+ opt.merge!({:include => {:resource_picture => {}, :artist => {}}, :order => 'original_pictures.id'})
+ OriginalPicture.find(:all, opt)
+ end
+
+ def list_as_json_with_resource_picture
+ self.to_json({:include => {:resource_picture => {:methods => :picture_data}}})
+ end
+
+ def self.list_as_json_text ary
+ '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']'
+ end
+
+ def destroy_with_resource_picture
+ res = false
+ OriginalPicture.transaction do
+ begin
+ PictureIO.original_picture_io.delete(self.filename) if PictureIO.original_picture_io.exist?(self.filename)
+ rescue PictureIO::Error
+ res = false
+ raise ActiveRecord::Rollback
+ end
+ if self.resource_picture
+ res = self.resource_picture.unpublish
+ raise ActiveRecord::Rollback unless res
+ end
+ self.pictures.each do |picture|
+ res = picture.unpublish
+ raise ActiveRecord::Rollback unless res
+ end
+ res = self.destroy
+ raise ActiveRecord::Rollback unless res
+ end
+ res
+ end
+
+ def self.publish oid, lsname, attr
+ l = License.find_by_name lsname
+ op = OriginalPicture.find oid
+ lg = l.license_group
+ attr[:license_id] = l.id
+
+ ctl = lg.classname.pluralize + '::Attribute'
+ le = ctl.constantize.new attr
+
+ rp = ResourcePicture.new
+ rp.attributes = le.resource_picture_attributes op
+ rp.overwrite op
+
+ imager = PettanImager.load op.restore
+ rp.store imager
+ end
+
+ def self.upload fn, auth
+ b = Base64.encode64(File.open(fn, 'rb').read)
+ u = 'http://localhost:3000/original_pictures'
+ r = RestClient.post(u,
+ {:original_picture => {:file => b}, :auth_token => auth}.to_json,
+ :content_type => :json, :accept => :json
+ )
+ o = JSON.parse r
+ oid = o['id']
+ oid
+ end
+
+ def self.auto_publish dirname, auth
+ Dir.glob File.expand_path(dirname) + '/*' do |filename|
+ if File.directory?(filename)
+ img = nil
+ lsname = nil
+ attr = nil
+ Dir.glob(filename + '/*') do |fn|
+ ext = File.extname(fn).downcase
+ case ext
+ when '.json'
+ json = JSON.parse(File.open(fn).read)
+ lsname = json["license_name"]
+ attr = json["attributes"]
+ when '.png', '.gif', '.jpeg'
+ img = fn
+ end
+ end
+ oid = OriginalPicture.upload img, auth
+ OriginalPicture.publish oid, lsname, attr
+ end
+ end