rack-archive-zip-extract 0.0.2 → 0.0.3

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 (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rack/archive/zip/extract.rb +45 -24
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 367946e3ae493b7beb2c8a894a737861d8819a44
4
- data.tar.gz: 1b2509141f86cde9c02afb4ce3174136db6d57fd
3
+ metadata.gz: 72114a73a80de1f5a42ba4439d0de4d8bd490be9
4
+ data.tar.gz: 1b0f2ca04e99752201464ac5f7e5e113df7c441d
5
5
  SHA512:
6
- metadata.gz: 0200cd1fa15c0182d6597d21dd25796c5c928753d252060a44d1255a0c10d1aa5a91ad910387321b8073e2b12773066322b06bf43dc49f705cf27e4e3ec1364d
7
- data.tar.gz: 36807535e98a5b0947fef9066be63b5d47ad09a951854b211ea894960ddc92c61f92a1d57448a4141412e73170122a04750aa38ee4a4ce065201f874ee9e1d42
6
+ metadata.gz: 7256c9a7a0cc5fde2f603e04ad495a46ad20af2858afa32707e7afd1576a7faf9dd1ec4c6050869d87827535e0484ee2bbeb4cd5ab79fe77c9d60f1dbb28b503
7
+ data.tar.gz: c49eebd140a6258aad2987ed476af758dbd634515fb7f823c2bee7999eb0faab722625f1554b1bffd84ec48e8fd0f2d61cc81df6ec700c1de1dd9fee6e5bc62a
@@ -45,30 +45,27 @@ module Rack::Archive
45
45
  return [status_code(:method_not_allowd), {ALLOW => ALLOWED_VERBS.join(COMMA)}, []] unless ALLOWED_VERBS.include? env[REQUEST_METHOD]
46
46
 
47
47
  path_info = unescape(env[PATH_INFO])
48
- if_modified_since = env[IF_MODIFIED_SINCE]
49
- if_modified_since = Time.parse(if_modified_since) if if_modified_since
50
- zip_file = nil
51
- body = nil
52
- file_size = nil
53
- mtime = nil
48
+ file = nil
54
49
  @extensions.each do |ext|
55
50
  zip_file, inner_path = find_zip_file_and_inner_path(path_info, ext)
56
- body, file_size, mtime = extract_content(zip_file, inner_path, if_modified_since)
57
- break if mtime
51
+ file = extract_content(zip_file, inner_path)
52
+ break if file
58
53
  end
59
- return [status_code(:not_found), {}, []] if mtime.nil?
54
+ return [status_code(:not_found), {}, []] if file.nil?
60
55
 
61
- if if_modified_since and mtime <= if_modified_since
56
+ if_modified_since = env[IF_MODIFIED_SINCE]
57
+ if_modified_since = Time.parse(if_modified_since) if if_modified_since
58
+ if if_modified_since and file.mtime <= if_modified_since
62
59
  [status_code(:not_modified), {}, []]
63
60
  else
64
61
  [
65
62
  status_code(:ok),
66
63
  {
67
64
  CONTENT_TYPE => Rack::Mime.mime_type(::File.extname(path_info)),
68
- CONTENT_LENGTH => file_size.to_s,
69
- LAST_MODIFIED => mtime.httpdate
65
+ CONTENT_LENGTH => file.size.to_s,
66
+ LAST_MODIFIED => file.mtime.httpdate
70
67
  },
71
- [body]
68
+ file
72
69
  ]
73
70
  end
74
71
  end
@@ -89,20 +86,17 @@ module Rack::Archive
89
86
 
90
87
  # @param zip_file_path [Pathname] path to zip file
91
88
  # @param inner_path [String] path to file in zip archive
92
- # @return [String] content
89
+ # @return [Zip::File]
93
90
  # @return [nil] if +zip_file_path+ is nil or +inner_path+ is empty
94
91
  # @return [nil] if +inner_path+ doesn't exist in zip archive
95
- def extract_content(zip_file_path, inner_path, if_modified_since)
92
+ def extract_content(zip_file_path, inner_path)
96
93
  return if zip_file_path.nil? or inner_path.empty?
97
- ::Zip::Archive.open zip_file_path.to_path do |archive|
98
- return if archive.locate_name(inner_path) < 0
99
- archive.fopen inner_path do |file|
100
- if if_modified_since and file.mtime <= if_modified_since
101
- return nil, nil, file.mtime
102
- else
103
- return file.read, file.size, file.mtime
104
- end
105
- end
94
+ archive = ::Zip::Archive.open(zip_file_path.to_path)
95
+ if archive.locate_name(inner_path) < 0
96
+ archive.close
97
+ nil
98
+ else
99
+ ExtractedFile.new(archive, archive.fopen(inner_path))
106
100
  end
107
101
  end
108
102
 
@@ -118,6 +112,33 @@ module Rack::Archive
118
112
  end
119
113
  clean
120
114
  end
115
+
116
+ class ExtractedFile
117
+ # @param archive [Zip::Archive]
118
+ # @param file [Zip::File]
119
+ def initialize(archive, file)
120
+ @archive, @file = archive, file
121
+ end
122
+
123
+ def each
124
+ @file.read do |chunk|
125
+ yield chunk
126
+ end
127
+ end
128
+
129
+ def mtime
130
+ @file.mtime
131
+ end
132
+
133
+ def size
134
+ @file.size
135
+ end
136
+
137
+ def close
138
+ @file.close
139
+ @archive.close
140
+ end
141
+ end
121
142
  end
122
143
  end
123
144
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-archive-zip-extract
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - KITAITI Makoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-05 00:00:00.000000000 Z
11
+ date: 2014-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack