grifizoid 0.0.1 → 0.0.2

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.
Files changed (2) hide show
  1. data/lib/grifizoid.rb +24 -5
  2. metadata +6 -20
data/lib/grifizoid.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'fileutils'
2
+
1
3
  class Grifizoid
2
4
  def initialize(app, &path_blk)
3
5
  @app = app
@@ -7,9 +9,8 @@ class Grifizoid
7
9
  # Get file from GridFileSystem or pass-thru
8
10
  def call(env)
9
11
  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 )
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)
13
14
 
14
15
  headers = {
15
16
  'ETag' => "\"#{etag}\"",
@@ -18,14 +19,32 @@ class Grifizoid
18
19
  if not_modified?(env, etag, last_modified )
19
20
  [304, headers, 'Not Modified']
20
21
  else
21
- [200, headers.merge('Content-Type' => file.content_type, 'Content-Length' => file.file_length.to_s), file]
22
+ [200, headers.merge('Content-Type' => mongo_file.content_type, 'Content-Length' => mongo_file.file_length.to_s), file]
22
23
  end
23
24
  rescue Mongo::GridError, Mongo::GridFileNotFound
24
25
  @app.call(env)
25
26
  end
26
27
  end
27
28
 
28
- # use raw path or path request to block to let user set a custom path
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
29
48
  def extract_gfs_path(env)
30
49
  request = Rack::Request.new(env)
31
50
 
metadata CHANGED
@@ -1,21 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grifizoid
3
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
4
+ prerelease:
5
+ version: 0.0.2
11
6
  platform: ruby
12
7
  authors:
13
- - Pogodan
8
+ - Paul Meserve
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-06-07 00:00:00 +02:00
13
+ date: 2011-06-13 00:00:00 +02:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -26,9 +21,6 @@ dependencies:
26
21
  requirements:
27
22
  - - ">="
28
23
  - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
24
  version: "0"
33
25
  type: :runtime
34
26
  version_requirements: *id001
@@ -44,7 +36,7 @@ files:
44
36
  - lib/grifizoid.rb
45
37
  - README.md
46
38
  has_rdoc: true
47
- homepage: http://www.pogodan.com/projects
39
+ homepage: http://github.com/pogodan/grifizoid
48
40
  licenses: []
49
41
 
50
42
  post_install_message:
@@ -57,23 +49,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
57
49
  requirements:
58
50
  - - ">="
59
51
  - !ruby/object:Gem::Version
60
- hash: 3
61
- segments:
62
- - 0
63
52
  version: "0"
64
53
  required_rubygems_version: !ruby/object:Gem::Requirement
65
54
  none: false
66
55
  requirements:
67
56
  - - ">="
68
57
  - !ruby/object:Gem::Version
69
- hash: 3
70
- segments:
71
- - 0
72
58
  version: "0"
73
59
  requirements: []
74
60
 
75
61
  rubyforge_project: none
76
- rubygems_version: 1.3.7
62
+ rubygems_version: 1.6.2
77
63
  signing_key:
78
64
  specification_version: 3
79
65
  summary: Rack middleware for Mongo GridFileSystem