grifizoid 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +6 -12
  2. data/lib/grifizoid.rb +5 -24
  3. metadata +1 -1
data/README.md CHANGED
@@ -14,22 +14,16 @@ It's sending a file handler not the raw data, so this should be low-memory use e
14
14
  and `bundle install`
15
15
 
16
16
  ## Use with Rails 3
17
-
17
+
18
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|
19
+ config.middleware.use Rack::GridFS do |req|
25
20
  site = Site.where(:host => req.host).first
26
21
 
27
22
  File.join(site.to_param, req.path_info)
28
23
  end
29
-
30
24
 
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
25
+ Now someone going to 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
26
 
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)
27
+ Some ideas/code taken from:
28
+ https://github.com/dusty/rack_grid
29
+ https://github.com/skinandbones/rack-gridfs
data/lib/grifizoid.rb CHANGED
@@ -1,5 +1,3 @@
1
- require 'fileutils'
2
-
3
1
  class Grifizoid
4
2
  def initialize(app, &path_blk)
5
3
  @app = app
@@ -9,8 +7,9 @@ class Grifizoid
9
7
  # Get file from GridFileSystem or pass-thru
10
8
  def call(env)
11
9
  begin
12
- mongo_file, file = retrieve_file(env)
13
- etag, last_modified = mongo_file.instance_variable_get(:@md5), Time.at(mongo_file.upload_date.to_i)
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 )
14
13
 
15
14
  headers = {
16
15
  'ETag' => "\"#{etag}\"",
@@ -19,32 +18,14 @@ class Grifizoid
19
18
  if not_modified?(env, etag, last_modified )
20
19
  [304, headers, 'Not Modified']
21
20
  else
22
- [200, headers.merge('Content-Type' => mongo_file.content_type, 'Content-Length' => mongo_file.file_length.to_s), file]
21
+ [200, headers.merge('Content-Type' => file.content_type, 'Content-Length' => file.file_length.to_s), file]
23
22
  end
24
23
  rescue Mongo::GridError, Mongo::GridFileNotFound
25
24
  @app.call(env)
26
25
  end
27
26
  end
28
27
 
29
- def retrieve_file(env)
30
- gfs_path = extract_gfs_path(env)
31
- fs_path = File.join(Rails.root, "tmp/grifizoid/#{gfs_path}")
32
-
33
- mongo_file = Mongo::GridFileSystem.new(Mongoid.database).open(gfs_path, 'r')
34
- #updated_at = Time.at(mongo_file.upload_date.to_i)
35
-
36
- fs_file = File.open(fs_path, 'r') if File.exists?(fs_path)
37
- fs_file ||= begin
38
- FileUtils.mkdir_p File.dirname(fs_path)
39
- File.open(fs_path, 'wb'){ |f| f << mongo_file.read }
40
- File.open(fs_path, 'r')
41
- #File.utime(updated_at, updated_at, fs_path)
42
- end
43
-
44
- [mongo_file, fs_file]
45
- end
46
-
47
- # use raw path or pass request to block to let user set a custom path
28
+ # use raw path or path request to block to let user set a custom path
48
29
  def extract_gfs_path(env)
49
30
  request = Rack::Request.new(env)
50
31
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: grifizoid
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Paul Meserve