-
-
Save negonicrac/30348 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# An uploader for merb, stored in app/uploaders/my_uploader.rb | |
# | |
# Usage: | |
# | |
# identifier = "some_string_probably_filename.png" | |
# uploader = MyUploader.new(identifier) | |
# | |
# Storing files to the filesystem | |
# uploader.storage = :file | |
# | |
# Filename defaults to the same as identifier, but can be overridden | |
# uploader.filename # => uploader.identifier | |
# | |
# Path for storage defaults to store_dir plus filename, but can be overridden | |
# uploader.path # => uploader.store_dir / uploader.filename | |
# | |
# Read contents | |
# uploader.read # => nil (file is empty) | |
# | |
# Upload a file. | |
# uploader.upload!(file) | |
# | |
# Identical to: | |
# uploader.storage.upload!(file) | |
# | |
# Read contents | |
# uploader.read # => "..." (contents of file) | |
# | |
# Processing the files when they are uploaded: | |
# class MyUploader | |
# process :shrink! | |
# process :inflate! | |
# process :grow! | |
# end | |
# this will call uploader.shrink!, uploader.inflate!, uploader.grow! after calling | |
# uploader.upload! | |
# | |
# class MyUploader | |
# process :shrink! | |
# process :inflate! => "original size" | |
# process :grow! => [200, 400] | |
# end | |
# this will call uploader.shrink!, uploader.inflate!("original size"), uploader.grow!(200, 400) | |
# | |
# those methods can then be done whatever we want. Plugins can use this, for example RMagick: | |
# | |
# class MyUploader | |
# include Merb::Uploader::RMagick | |
# process :crop_resized => [300, 300] | |
# process :desaturate | |
# process :awesomerize | |
# | |
# def awesomerize | |
# manipulate do |img| | |
# # do something | |
# end | |
# end | |
# end | |
class MyUploader | |
# Provided by merb-uploader | |
include Merb::Uploader | |
# Provided by merb_rmagick, merb_image_science, etc... | |
include Merb::Uploader::RMagick | |
# This is done for all versions | |
process :resize => '800x600' | |
# It's possible to create versions of uploaded files (maybe?) | |
version :thumb do | |
# Multiple instructions for a single process, maybe the plugins can take advantage of this | |
# somehow in order to perform all this simultaneously and save memory? | |
process :crop_resized => '100x60', :convert => 'png' | |
end | |
version :backup do | |
# We can redefine most stuff for versions | |
include Merb::Uploader::ImageScience | |
storage :s3 | |
process :resize => '400x300' | |
end | |
# Most things should be overridable | |
def filename | |
"#{basename}.#{extension}" | |
end | |
# Like so. | |
def extension | |
options[:extension] || "png" | |
end | |
# This thing is a DataMapper type, so we can use it as such, to get DataMapper backed uploads | |
# for free ;) | |
def self.load(string) | |
self.retrieve(string) | |
end | |
def self.dump(file) | |
self.upload!(file.original_filename, file) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment