grifizoid 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +35 -0
- data/lib/grifizoid.rb +51 -0
- metadata +81 -0
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Grifizoid
|
2
|
+
|
3
|
+
This is intended to be a simple Rack middleware for hosting site assets in a MongoDB [GridFileSystem](http://www.mongodb.org/display/DOCS/GridFS+in+Ruby). Right now it assumes you're using [Mongoid](http://mongoid.org/), mostly as a simple way to avoid needing any custom connection-management code
|
4
|
+
|
5
|
+
Unlike most similar tools, this is doing the lookup on the raw file path itself (optionally customized by passing a block to the initializer), not using a BSON ObjectID. The intended use-case for is storing site assets in GridFS directly and accessing them with normal human-friendly URLs, just like if they were sitting on your hard drive
|
6
|
+
|
7
|
+
It's sending a file handler not the raw data, so this should be low-memory use even for large files. It will set `Etag` and `Last-Modified` headers, and respond with a `304 Not Modified` when possible. Any additional caching you should do yourself where appropriate
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
# Gemfile
|
12
|
+
gem 'grifizoid', :git => 'git://github.com/pogodan/grifizoid.git'
|
13
|
+
|
14
|
+
and `bundle install`
|
15
|
+
|
16
|
+
## Use with Rails 3
|
17
|
+
|
18
|
+
# application.rb
|
19
|
+
|
20
|
+
# /images/file.jpg => images/file.jpg
|
21
|
+
config.middleware.use Grifizoid
|
22
|
+
|
23
|
+
# /file.jpg => #{site.to_param}/images/file.jpg
|
24
|
+
config.middleware.use Grifizoid do |req|
|
25
|
+
site = Site.where(:host => req.host).first
|
26
|
+
|
27
|
+
File.join(site.to_param, req.path_info)
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
Now someone going to [http://mydomain.com/file.jpg](http://mydomain.com/file.jpg) will result in the middleware doing a query to find the site `mydomain.com` (with an ID like `4d80c69f4cfad13cd100000c`) and then looks for a GridFS file named `4d80c69f4cfad13cd100000c/file.jpg`, sending it through to the client if found or else passing the request through to your app
|
32
|
+
|
33
|
+
Some ideas/code taken from:
|
34
|
+
* [https://github.com/dusty/rack_grid](https://github.com/dusty/rack_grid)
|
35
|
+
* [https://github.com/skinandbones/rack-gridfs](https://github.com/skinandbones/rack-gridfs)
|
data/lib/grifizoid.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
class Grifizoid
|
2
|
+
def initialize(app, &path_blk)
|
3
|
+
@app = app
|
4
|
+
@path_blk = path_blk
|
5
|
+
end
|
6
|
+
|
7
|
+
# Get file from GridFileSystem or pass-thru
|
8
|
+
def call(env)
|
9
|
+
begin
|
10
|
+
gfs_path = extract_gfs_path(env)
|
11
|
+
file = Mongo::GridFileSystem.new(Mongoid.database).open(gfs_path, 'r')
|
12
|
+
etag, last_modified = file.instance_variable_get(:@md5), Time.at( file.upload_date.to_i )
|
13
|
+
|
14
|
+
headers = {
|
15
|
+
'ETag' => "\"#{etag}\"",
|
16
|
+
'Last-Modified' => last_modified.httpdate,
|
17
|
+
}
|
18
|
+
if not_modified?(env, etag, last_modified )
|
19
|
+
[304, headers, 'Not Modified']
|
20
|
+
else
|
21
|
+
[200, headers.merge('Content-Type' => file.content_type, 'Content-Length' => file.file_length.to_s), file]
|
22
|
+
end
|
23
|
+
rescue Mongo::GridError, Mongo::GridFileNotFound
|
24
|
+
@app.call(env)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# use raw path or path request to block to let user set a custom path
|
29
|
+
def extract_gfs_path(env)
|
30
|
+
request = Rack::Request.new(env)
|
31
|
+
|
32
|
+
if @path_blk
|
33
|
+
@path_blk.call(request)
|
34
|
+
else
|
35
|
+
request.path_info
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def not_modified?(env, etag, last_modified )
|
41
|
+
if_none_match = env['HTTP_IF_NONE_MATCH']
|
42
|
+
if if_modified_since = env['HTTP_IF_MODIFIED_SINCE']
|
43
|
+
if_modified_since = Time.rfc2822(if_modified_since) rescue nil
|
44
|
+
end
|
45
|
+
|
46
|
+
not_modified = if_none_match.present? || if_modified_since.present?
|
47
|
+
not_modified &&= (if_none_match == "\"#{etag}\"") if if_none_match && etag
|
48
|
+
not_modified &&= (if_modified_since >= last_modified) if if_modified_since && last_modified
|
49
|
+
not_modified
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grifizoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Pogodan
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-07 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: mongo
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Rack middleware for Mongo GridFileSystem
|
36
|
+
email: dev@pogodan.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README.md
|
43
|
+
files:
|
44
|
+
- lib/grifizoid.rb
|
45
|
+
- README.md
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: http://www.pogodan.com/projects
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project: none
|
76
|
+
rubygems_version: 1.3.7
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Rack middleware for Mongo GridFileSystem
|
80
|
+
test_files: []
|
81
|
+
|