Skip to content

Instantly share code, notes, and snippets.

@anoldguy
Created December 12, 2012 02:19
Show Gist options
  • Save anoldguy/4264322 to your computer and use it in GitHub Desktop.
Save anoldguy/4264322 to your computer and use it in GitHub Desktop.
Example code from past projects. The ruby is from 2008-ish. The puppet is current, but sparse, as I'm just learning it.
#!/usr/bin/env ruby
$:.push("lib")
require 'image_science.rb'
require 'deep_zoom.rb'
file_options = { :format => 'png',
:quality => 80 }
slice_options = { :tile_size => 256,
:overlap => 4 }
exit if ARGV.length < 2
file = ARGV[0]
degrees = ARGV[1] ? ARGV[1].to_i : 0
extension = File.extname(file).gsub('.', '')
filename = File.basename(file, '.' + extension)
dirname = File.dirname(file)
new_filename = dirname + '/' + 'image.'+ file_options[:format]
new_thumb_filename = dirname + '/' + 'image_thumb.'+ file_options[:format]
xml_filename = dirname + '/' + 'image.xml'
dzi_filename = dirname + '/' + 'image.dzi'
#puts "#{file} #{degrees}"
ImageScience.with_image(file) do |i|
i.rotate(degrees) do |rot|
rot.save(new_filename)
rot.thumbnail(256) do |thumb|
thumb.save(new_thumb_filename)
end
end
end
DeepZoom.new(new_filename, file_options).slice!(slice_options)
`cp #{xml_filename} #{dzi_filename}`
class Drawing < ActiveRecord::Base
belongs_to :project
default_scope :order => 'filename ASC'
has_attachment :content_type => ['image/tiff','image/png','image/tif','image/x-png', 'application/octet-stream'],
:storage => :file_system,
:thumbnails => { :small => '200x200>' },
:size => 0.megabytes..100.megabytes,
:processor => :OpenZoom
before_save :create_zoomable
#after_save :create_zoomable
after_destroy :remove_all_detrius
validates_as_attachment
acts_as_versioned :limit => 10, :if_changed => [:filename, :size, :width, :height]
non_versioned_columns << 'rotate_now' << 'rotate' << 'updated_at'
# Map file extensions to mime types.
# Thanks to bug in Flash 8 the content type is always set to application/octet-stream.
# From: http://blog.airbladesoftware.com/2007/8/8/uploading-files-with-swfupload
def swf_uploaded_data=(data)
#logger.debug(data.content_type)
data.content_type = MIME::Types.type_for(data.original_filename)
#logger.debug(data.content_type)
self.uploaded_data = data
end
def status
status = "New"
#self.dzi_filename.nil? ? false : true
#logger.debug("In Drawing.zoomed?: #{job.inspect}")
begin
if self.job
status = self.job.state
end
rescue
end
status
end
def dzi_filename
full_path = "#{self.attachment_path}/image.dzi"
path = nil
if File.exists?(full_path)
path = full_path.gsub %r(^#{Regexp.escape(self.base_path)}), ''
logger.debug("#{path}")
end
path
end
def zoomable_thumbnail_path
full_path = "#{self.attachment_path}/image_thumb.png"
path = nil
if File.exists?(full_path)
path = full_path.gsub %r(^#{Regexp.escape(self.base_path)}), ''
logger.debug("#{path}")
end
path
end
def dzi_xml_path
full_path = "#{self.attachment_path}/image.xml"
path = nil
if File.exists?(full_path)
path = full_path.gsub %r(^#{Regexp.escape(self.base_path)}), ''
#logger.debug("#{path}")
end
path
end
def dzi_xml
full_path = "#{self.attachment_path}/image.xml"
xml = nil
if File.exists?(full_path)
File.open(full_path, "r") { |f|
xml = f.read
}
#logger.debug("#{xml}")
end
xml.gsub("\n","") unless xml.nil?
end
def attachment_path
File.dirname(self.full_filename)
end
# Monkey Patch for using acts_as_versioned with attachment_fu. This should override the
# attachment_fu function.
def rename_file
end
def remove_zoomable
self.zoomable_files.each do |f|
target = "#{self.attachment_path}/#{f}"
FileUtils.remove_entry_secure(target, force = true)
end
end
def zoomable_files
["image.xml", "image.dzi", "image_files", "image_thumb.png"]
end
def clear_old_version(version)
logger.debug("Version: #{version} - #{self.id}")
if version != self.version
self.class.versioned_class.delete_all ["#{self.class.version_column} = ? and #{self.class.versioned_foreign_key} = ?", version, self.id]
else
false
end
end
def root_dir
self.attachment_path
end
def job
job = nil
if self.job_id
job = Bj.table.job.find(self.job_id) ? Bj.table.job.find(self.job_id) : Bj.table.job_archive.find(self.job_id)
end
#logger.debug("In Drawing.job(): #{job.inspect}")
job
end
def set_thumbnailable
#logger.debug(self.content_type);
self.skip_thumbnail_processing = true unless self.new_record?
end
private
def attachment_path_id
"/#{id}/v#{version}/"
end
def partitioned_path(*args)
attachment_path_id + args.to_s
end
def create_zoomable
#logger.debug("In Create Zoomable: #{self.inspect}")
self.set_thumbnailable
if self.rotate_now
if self.rotate == 90 || self.rotate == 270
self.zoomable_height = self.width
self.zoomable_width = self.height
else
self.zoomable_height = self.height
self.zoomable_width = self.width
end
logger.debug("Rotate_now set, unsetting and spawining job!")
self.rotate_now = false
jobs = Bj.submit "jobs/create_dzi.rb #{self.full_filename} #{self.rotate}"
if jobs.size > 0
jobs.each do |job|
#logger.debug("#{job.bj_job_id}")
self.job_id = job.bj_job_id
end
end
end
end
def remove_all_detrius
#logger.debug("#{full_filename}")
target = File.expand_path(RAILS_ROOT + "/public/drawings/#{id}")
logger.debug("#{target}")
FileUtils.remove_entry_secure(target, force = true) if File.directory?(target)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment