ngzip 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ ### 1.0.3 (2013-08-05)
2
+
3
+ * Added the :base_dir option
4
+
1
5
  ### 1.0.2 (2013-07-15)
2
6
 
3
7
  * Fixed the escaping of path names
data/README.md CHANGED
@@ -21,10 +21,48 @@ Or install it yourself as:
21
21
 
22
22
  In a controller
23
23
 
24
- b = Nginx::Builder.new()
25
- response.headers['X-Archive-Files'] = 'zip'
26
- render :text => b.build("/data/test/Report.pdf", "/data/test/LargeArchive.tar")
24
+ ```ruby
25
+ b = Nginx::Builder.new()
26
+ response.headers['X-Archive-Files'] = 'zip'
27
+ render :text => b.build(["/data/test/Report.pdf", "/data/test/LargeArchive.tar"])
28
+ ```
27
29
 
30
+ This will generate a manifest similar to this
31
+
32
+ 8f92322f 446 /data/test/Report.pdf Report.pdf
33
+ 13788d9a 234 /data/test/LargeArchive.tar LargeArchive.tar
34
+
35
+ ### Directories in the archive
36
+
37
+ The builder will automatically remove common directory prefix for the pathes inside the archive. If you want to keep relative directories inside, specify the :base_dir option when calling the builder:
38
+
39
+ ```ruby
40
+ options = {:base_dir => '/data'}
41
+ render :text => b.build(["/data/test/Report.pdf", "/data/test/LargeArchive.tar"], options)
42
+ ```
43
+
44
+ This will generate
45
+
46
+ 8f92322f 446 /data/test/Report.pdf test/Report.pdf
47
+ 13788d9a 234 /data/test/LargeArchive.tar test/LargeArchive.tar
48
+
49
+
50
+
51
+ ### nginx configuration
52
+
53
+ Compile nginx with the mod_zip module from https://github.com/evanmiller/mod_zip.
54
+
55
+ Configure nginx to serve files with absolute paths from an internal location
56
+
57
+ # mod_zip location helper
58
+ # Note: The leading ^~ is essential here, no more checks should be done after this match
59
+ location ^~ /data/ {
60
+ root /;
61
+ internal;
62
+ }
63
+
64
+ Note that we only operate with local files in this examples. The module mod_zip uses subrequests to
65
+ fetch the actual file data and would support any location type (even proxied).
28
66
 
29
67
  ## License
30
68
 
@@ -17,12 +17,14 @@ module Ngzip
17
17
  # The following options are available:
18
18
  # :crc => Enable or disable CRC-32 checksums
19
19
  # :crc_cache => Allows for provided cached CRC-32 checksums in a hash where the key is the file path
20
+ # :base_dir => Use this as root for the relative pathes in the archive, keep all directories below
20
21
  def build(files, options = {})
21
22
  settings = {:crc => true}
22
23
  settings.merge! options
23
24
 
24
25
  list = file_list(files)
25
- prefix = detect_common_prefix(list)
26
+ prefix = options[:base_dir] || detect_common_prefix(list)
27
+ prefix += '/' unless prefix.end_with?('/')
26
28
  list.map do |f|
27
29
  sprintf('%s %d %s %s',
28
30
  compute_crc32(f, settings),
@@ -49,7 +51,7 @@ module Ngzip
49
51
  # Returns a common prefix
50
52
  def detect_common_prefix(list)
51
53
  if list.size == 1
52
- return File.dirname(list.first) + '/'
54
+ return File.dirname(list.first)
53
55
  end
54
56
  prefix = ''
55
57
  min, max = list.sort.values_at(0, -1)
@@ -1,3 +1,3 @@
1
1
  module Ngzip
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -87,7 +87,29 @@ describe Ngzip::Builder do
87
87
  expected = "#{invalid_but_cached} 446 #{lorem} lorem.txt"
88
88
  builder.build(lorem, options.merge(:crc_cache => {lorem => invalid_but_cached})).must_equal expected
89
89
  end
90
+
91
+ it 'must remove common directories by default' do
92
+ pathes = builder.build([lorem, ipsum]).split("\n").map{|line| line.split.last}
93
+ expected = ["lorem.txt", "ipsum.txt"]
94
+ pathes.must_equal expected
95
+ end
96
+
97
+ it 'must keep common directories if :base_dir is provided' do
98
+ options = {:base_dir => Pathname.new(lorem).parent.parent.to_s}
99
+ pathes = builder.build([lorem, ipsum], options).split("\n").map{|line| line.split.last}
100
+ expected = ["a/lorem.txt", "a/ipsum.txt"]
101
+ pathes.must_equal expected
102
+ end
103
+
104
+ it 'must cope with a trailing / in the :base_dir' do
105
+ options = {:base_dir => Pathname.new(lorem).parent.parent.to_s + '/'}
106
+ pathes = builder.build([lorem, ipsum], options).split("\n").map{|line| line.split.last}
107
+ expected = ["a/lorem.txt", "a/ipsum.txt"]
108
+ pathes.must_equal expected
90
109
 
110
+ end
111
+
112
+
91
113
  end
92
114
 
93
115
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ngzip
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-14 00:00:00.000000000 Z
12
+ date: 2013-08-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -86,7 +86,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
86
86
  version: '0'
87
87
  segments:
88
88
  - 0
89
- hash: 3803006848080428378
89
+ hash: 4140413543763942870
90
90
  required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  none: false
92
92
  requirements:
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  version: '0'
96
96
  segments:
97
97
  - 0
98
- hash: 3803006848080428378
98
+ hash: 4140413543763942870
99
99
  requirements: []
100
100
  rubyforge_project:
101
101
  rubygems_version: 1.8.25