Created
December 10, 2012 21:20
-
-
Save unnu/4253524 to your computer and use it in GitHub Desktop.
Paperclip Redis Storage
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
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 |
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
mount Paperclip::Storage::Redis::App.new => "/dynamic"