/usr/lib/ruby/1.9.1/ramaze/contrib/sequel/image.rb is in libramaze-ruby1.9.1 2010.06.18-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | # Copyright (c) 2008 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the Ruby license.
# Scaffold image models utilizing thumbnailing and Ramaze integration.
# Resizing is done by ImageScience.
#
# Usage:
# class Avatar < Sequel::Model
# IMAGE = {
# # specifies many_to_one, will create relation and foreign key
#
# :owner => :User,
#
#
# # Remove original and thumbnails on Avatar#destroy
#
# :cleanup => true,
#
#
# # Algorithm to use in ImageScience
# #
# # * resize(width, height)
# # Resizes the image to +width+ and +height+ using a cubic-bspline
# # filter.
# #
# # * thumbnail(size)
# # Creates a proportional thumbnail of the image scaled so its
# # longest edge is resized to +size+.
# #
# # * cropped_thumbnail(size)
# # Creates a square thumbnail of the image cropping the longest edge
# # to match the shortest edge, resizes to +size+.
#
# :algorithm => :thumbnail,
#
#
# # Key specifies the filename and accessors, value are arguments to the
# # algorithm
#
# :sizes => {
# :small => 150,
# :medium => 300,
# :large => 600
# }
# }
#
# # Perform the scaffold
# include SequelImage
# end
module SequelImage
def self.included(model)
args = model::IMAGE
set_foreign_key = args[:foreign_key] || "#{args[:owner]}_id".downcase.to_sym
set_many_to_one = args[:many_to_one] || args[:owner].to_s.downcase.to_sym
# Define schema
model.set_schema do
primary_key :id
varchar :original # path to the original image
varchar :mime, :size => 22 # average of /etc/mime.types
time :created_at
time :updated_at
foreign_key set_foreign_key
end
# Define Relations
model.many_to_one set_many_to_one
# Define Hooks
model.before_create :generate_thumbnails do
generate_thumbnails
self.created_at = Time.now
end
model.before_save :update_time do
self.updated_at = Time.now
end
model.before_destroy :cleanup do
cleanup if conf[:cleanup]
end
# Define singleton methods
model.extend(SingletonMethods)
# Define instance methods
model.send(:include,
InstanceMethods,
Ramaze::Helper::CGI,
Ramaze::Helper::Link)
args[:sizes].each do |size, *args|
model.send(:define_method, size){ public_file(size) }
model.send(:define_method, "#{size}_url"){ file(size) }
end
end
module SingletonMethods
def store(file, uid, hash = {})
image = new(hash)
type = file[:type]
filename = file[:filename]
tempfile = file[:tempfile]
raise ArgumentError, 'Empty tempfile' if tempfile.size == 0
ext = Rack::Mime::MIME_TYPES.invert[type]
image.mime = type
target_name = image.next_name(File.basename(filename, File.extname(filename)), ext)
target_path = File.join(image.public_root, image.path, target_name)
FileUtils.mkdir_p(File.dirname(target_path))
FileUtils.cp(tempfile.path, target_path)
image.original = target_path
image.save
end
end
module InstanceMethods
def file(size = nil)
File.join('/', path, filename(size))
end
def public_file(size)
File.join(public_path, filename(size))
end
def public_path
File.join(public_root, path)
end
def path
conf[:path] || conf[:owner].to_s.downcase
end
def next_name(uid, ext)
uid = uid.to_s.scan(%r![^\\/'".:?&;\s]+!).join('-')
"#{uid}#{ext}"
end
def basename
File.basename(original, File.extname(original))
end
def public_root
Ramaze.options.roots.first
end
def filename(size)
if size
"#{basename}_#{size}.png"
else
"#{basename}.png"
end
end
def conf
self.class::IMAGE
end
def cleanup
conf[:sizes].each do |name, args|
out = public_file(name)
Ramaze::Log.debug "Remove Thumbnail: #{out}"
FileUtils.rm_f(out)
end
Ramaze::Log.debug "Remove original: #{original}"
FileUtils.rm_f(original)
end
def generate_thumbnails
FileUtils.mkdir_p(public_path)
sizes, algorithm = conf.values_at(:sizes, :algorithm)
ImageScience.with_image(original) do |img|
Ramaze::Log.debug "Process original: #{original}"
sizes.each do |name, args|
out = public_file(name)
Ramaze::Log.debug "Generate Thumbnail: #{out}"
img.send(algorithm, *args) do |thumb|
thumb.save(out)
end
end
end
end
end
end
|