zipline 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +7 -0
- data/README.md +0 -1
- data/lib/zipline/fake_stream.rb +1 -1
- data/lib/zipline/version.rb +1 -1
- data/lib/zipline/zip_generator.rb +29 -25
- data/spec/lib/zipline/zip_generator_spec.rb +68 -0
- data/spec/spec_helper.rb +14 -0
- data/zipline.gemspec +1 -1
- metadata +13 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e29199e5ffa8970199f9586ea13025d68373594f
|
4
|
+
data.tar.gz: b155365912bf26cdedc37221d4061571fbe7c985
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e9b51d097401fe6e7142ed4220b4c779b2f3cdc0e77f7665c85f0ad60daa62d33557b53c37e006bca87bd7584153d6a622d25e85e0366e5b32f4dbcbc604524d
|
7
|
+
data.tar.gz: 35b0a617645946e17306617b5e21f4f88f10c11a291b5c2079fe9d93c440eb68df71b600d30c305c76720c6359ee682d0782fefa236776b5c5cf804909ab5d90
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -48,6 +48,5 @@ For directories, just give the files names like "directory/file".
|
|
48
48
|
## TODO (possible contributions?)
|
49
49
|
|
50
50
|
* tests!
|
51
|
-
* support rails 4.0 streaming
|
52
51
|
* extract library for plain ruby streaming zips, which this will depend on.
|
53
52
|
* get my changes to support streaming zips checked in to the rubyzip library.
|
data/lib/zipline/fake_stream.rb
CHANGED
data/lib/zipline/version.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# this class acts as a streaming body for rails
|
2
2
|
# initialize it with an array of the files you want to zip
|
3
|
-
# right now only carrierwave is supported with file storage or S3
|
4
3
|
module Zipline
|
5
4
|
class ZipGenerator
|
6
5
|
# takes an array of pairs [[uploader, filename], ... ]
|
@@ -16,27 +15,29 @@ module Zipline
|
|
16
15
|
def each(&block)
|
17
16
|
output = new_output(&block)
|
18
17
|
OutputStream.open(output) do |zip|
|
19
|
-
@files.each
|
20
|
-
|
18
|
+
@files.each {|file, name| handle_file(zip, file, name) }
|
19
|
+
end
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
22
|
+
def handle_file(zip, file, name)
|
23
|
+
file = normalize(file)
|
24
|
+
name = uniquify_name(name)
|
25
|
+
write_file(zip, file, name)
|
26
|
+
end
|
27
|
+
|
28
|
+
def normalize(file)
|
29
|
+
unless is_io?(file)
|
30
|
+
if file.respond_to?(:url) && (!defined?(::Paperclip::Attachment) || !file.is_a?(::Paperclip::Attachment))
|
31
|
+
file = file
|
32
|
+
elsif file.respond_to? :file
|
33
|
+
file = File.open(file.file)
|
34
|
+
elsif file.respond_to? :path
|
35
|
+
file = File.open(file.path)
|
36
|
+
else
|
37
|
+
raise(ArgumentError, 'Bad File/Stream')
|
38
38
|
end
|
39
39
|
end
|
40
|
+
file
|
40
41
|
end
|
41
42
|
|
42
43
|
def new_output(&block)
|
@@ -45,10 +46,9 @@ module Zipline
|
|
45
46
|
|
46
47
|
def write_file(zip, file, name)
|
47
48
|
size = get_size(file)
|
48
|
-
|
49
49
|
zip.put_next_entry name, size
|
50
50
|
|
51
|
-
if file
|
51
|
+
if is_io?(file)
|
52
52
|
while buffer = file.read(2048)
|
53
53
|
zip << buffer
|
54
54
|
end
|
@@ -65,18 +65,22 @@ module Zipline
|
|
65
65
|
end
|
66
66
|
|
67
67
|
def get_size(file)
|
68
|
-
|
69
|
-
when 'File'
|
68
|
+
if is_io?(file)
|
70
69
|
file.size
|
71
|
-
|
70
|
+
elsif file.respond_to? :content_length
|
72
71
|
file.content_length
|
72
|
+
else
|
73
|
+
throw 'cannot determine file size'
|
73
74
|
end
|
74
75
|
end
|
75
76
|
|
77
|
+
def is_io?(file)
|
78
|
+
file.is_a?(IO) || (defined?(StringIO) && file.is_a?(StringIO))
|
79
|
+
end
|
80
|
+
|
76
81
|
def uniquify_name(name)
|
77
82
|
@used_names ||= Set.new
|
78
83
|
|
79
|
-
|
80
84
|
if @used_names.include?(name)
|
81
85
|
|
82
86
|
#remove suffix e.g. ".foo"
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
describe Zipline::ZipGenerator do
|
5
|
+
|
6
|
+
before { Fog.mock! }
|
7
|
+
let(:file_attributes){ {
|
8
|
+
key: 'fog_file_tests',
|
9
|
+
body: 'some body',
|
10
|
+
public: true
|
11
|
+
}}
|
12
|
+
let(:directory_attributes){{
|
13
|
+
key: 'fog_directory'
|
14
|
+
}}
|
15
|
+
let(:storage_attributes){{
|
16
|
+
aws_access_key_id: 'fake_access_key_id',
|
17
|
+
aws_secret_access_key: 'fake_secret_access_key',
|
18
|
+
provider: 'AWS'
|
19
|
+
}}
|
20
|
+
let(:storage){ Fog::Storage.new(storage_attributes)}
|
21
|
+
let(:directory){ storage.directories.create(directory_attributes) }
|
22
|
+
let(:file){ directory.files.create(file_attributes) }
|
23
|
+
|
24
|
+
describe '.normalize' do
|
25
|
+
let(:generator){ Zipline::ZipGenerator.new([])}
|
26
|
+
context "CarrierWave" do
|
27
|
+
context "Remote" do
|
28
|
+
let(:file){ CarrierWave::Storage::Fog::File.new(nil,nil,nil) }
|
29
|
+
it "passes through" do
|
30
|
+
expect(File).not_to receive(:open)
|
31
|
+
expect(generator.normalize(file)).to be file
|
32
|
+
end
|
33
|
+
end
|
34
|
+
context "Local" do
|
35
|
+
let(:file){ CarrierWave::SanitizedFile.new(Tempfile.new('t')) }
|
36
|
+
it "creates a File" do
|
37
|
+
expect(generator.normalize(file)).to be_a File
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
context "Paperclip" do
|
42
|
+
let(:file){ Paperclip::Attachment.new(:name, :instance) }
|
43
|
+
it "creates a File" do
|
44
|
+
expect(File).to receive(:open).once
|
45
|
+
generator.normalize(file)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
context "Fog" do
|
49
|
+
it "passes through" do
|
50
|
+
expect(File).not_to receive(:open)
|
51
|
+
expect(generator.normalize(file)).to be file
|
52
|
+
end
|
53
|
+
end
|
54
|
+
context "IOStream" do
|
55
|
+
let(:file){ StringIO.new('passthrough')}
|
56
|
+
it "passes through" do
|
57
|
+
expect(generator.normalize(file)).to be file
|
58
|
+
end
|
59
|
+
end
|
60
|
+
context "invalid" do
|
61
|
+
let(:file){ Thread.new{} }
|
62
|
+
it "raises error" do
|
63
|
+
expect{generator.normalize(file)}.to raise_error(ArgumentError)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'zipline'
|
3
|
+
require 'paperclip'
|
4
|
+
require 'fog'
|
5
|
+
require 'carrierwave'
|
6
|
+
|
7
|
+
Dir["#{File.expand_path('..', __FILE__)}/support/**/*.rb"].each { |f| require f }
|
8
|
+
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.color_enabled = true
|
12
|
+
config.order = :random
|
13
|
+
config.run_all_when_everything_filtered = true
|
14
|
+
end
|
data/zipline.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zipline
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ram Dobson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubyzip
|
@@ -37,6 +37,9 @@ dependencies:
|
|
37
37
|
- - '>='
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: 3.2.1
|
40
|
+
- - <
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '4.3'
|
40
43
|
type: :runtime
|
41
44
|
prerelease: false
|
42
45
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -44,6 +47,9 @@ dependencies:
|
|
44
47
|
- - '>='
|
45
48
|
- !ruby/object:Gem::Version
|
46
49
|
version: 3.2.1
|
50
|
+
- - <
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '4.3'
|
47
53
|
- !ruby/object:Gem::Dependency
|
48
54
|
name: curb
|
49
55
|
requirement: !ruby/object:Gem::Requirement
|
@@ -75,6 +81,8 @@ files:
|
|
75
81
|
- lib/zipline/output_stream.rb
|
76
82
|
- lib/zipline/version.rb
|
77
83
|
- lib/zipline/zip_generator.rb
|
84
|
+
- spec/lib/zipline/zip_generator_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
78
86
|
- zipline.gemspec
|
79
87
|
homepage: http://github.com/fringd/zipline
|
80
88
|
licenses: []
|
@@ -99,4 +107,6 @@ rubygems_version: 2.0.0.rc.2
|
|
99
107
|
signing_key:
|
100
108
|
specification_version: 4
|
101
109
|
summary: stream zip files from rails
|
102
|
-
test_files:
|
110
|
+
test_files:
|
111
|
+
- spec/lib/zipline/zip_generator_spec.rb
|
112
|
+
- spec/spec_helper.rb
|