Skip to content

Instantly share code, notes, and snippets.

@unnu
Created December 10, 2012 21:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unnu/4253524 to your computer and use it in GitHub Desktop.
Save unnu/4253524 to your computer and use it in GitHub Desktop.
Paperclip Redis Storage
module Paperclip::Storage::Redis
def self.extended(base)
base.instance_eval do
@options[:path] = ":class/:attachment/:id_partition/:style/:filename"
@options[:url] = "/dynamic/:class/:attachment/:id_partition/:style/:filename"
@redis = Redis.new
end
end
def exists?(style = default_style)
@redis.exists(path(style))
end
def flush_writes
@queued_for_write.each do |style, file|
log("saving #{path(style)}")
file.rewind
@redis.set(path(style), file.read)
end
end
def flush_deletes
@queued_for_delete.each do |path|
log("deleting #{path}")
@redis.del(path)
end
end
def copy_to_local_file(style, destination_path)
File.open(destination_path, "w") do |f|
f << read
end
end
def read(style = default_style)
@redis.get(path(style))
end
class App
def call(env)
content = Redis.new.get(env["PATH_INFO"].gsub(/^\//, ""))
extname = File.extname(env["PATH_INFO"])[1..-1]
content_type = Mime::Type.lookup_by_extension(extname)
[200, {"Content-Type" => content_type.to_s, "Content-Length" => content.bytesize.to_s}, [content]]
rescue
raise $!
[404, {}, []]
end
end
end
@unnu
Copy link
Author

unnu commented Dec 10, 2012

mount Paperclip::Storage::Redis::App.new => "/dynamic"

@webhat
Copy link

webhat commented Mar 19, 2014

Using this as a guideline I put together a ruby gem and added you as one of the authors: https://rubygems.org/gems/paperclip_redis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment