file-manager 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3274b9402d808285bad4ecb41f687666157135f7
4
- data.tar.gz: a5d16438c16799c45c76d8b7633fe9dcaf1a5732
3
+ metadata.gz: 5b529caed3e2276f2f82062056cfbe2f3db1c46b
4
+ data.tar.gz: e58a5c7976cb19b329784fa34ef3ca6dc8ed2d18
5
5
  SHA512:
6
- metadata.gz: cc14c2b86c76e173319127c7562902044b4a8355478594f63c515b8b2f7d67095b2d11744a65d75745fb188c9d9a5adeec106f176b14f2039079268a8766374c
7
- data.tar.gz: 2ae8c3f919f1620b4f41d949a2b35b8b3cfac1ae247a01631f29626a4dde91532414d73ca33ea6b702bf098b19608f1715c1ebb40ab777ec277f1d824d25d70d
6
+ metadata.gz: 33882e75cbe545f2b1282bf6bc4a788ce025b1ba64ab44cf8f6ed15fa6aef789ab4887ddef58ac6dd9c2914d997e98fc7b0b8a501fc1e0b2ecb176d0991bb6ae
7
+ data.tar.gz: 5cea2792c307170389e2fc39cf690f325016b92c5a94c5f8865104cc8e4c9ea3abd85d15c68342c3632d5d50d736221f452b6bb330fb4802cddbda24c87914f3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- file-manager (0.2.0)
4
+ file-manager (0.3.0)
5
5
  aws-sdk (= 2.2.13)
6
6
 
7
7
  GEM
@@ -51,4 +51,4 @@ DEPENDENCIES
51
51
  webmock
52
52
 
53
53
  BUNDLED WITH
54
- 1.10.6
54
+ 1.15.4
data/file-manager.gemspec CHANGED
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "file-manager"
6
- spec.version = '0.2.0'
6
+ spec.version = '0.3.0'
7
7
  spec.authors = ["Francisco Barroso / Marlus Saraiva"]
8
8
  spec.email = ["franciscobarroso@grupofortes.com.br"]
9
9
  spec.description = 'File manager, access S3 or local'
data/lib/file_manager.rb CHANGED
@@ -15,6 +15,10 @@ class FileManager
15
15
  raise 'Not implemented!'
16
16
  end
17
17
 
18
+ def download_to_temp_file(file_name)
19
+ raise 'Not implemented!'
20
+ end
21
+
18
22
  def list_files(prefix = '', file_extension = '*')
19
23
  raise 'Not implemented!'
20
24
  end
@@ -23,6 +23,17 @@ class LocalFileManager < FileManager
23
23
  @logger.puts 'done.'
24
24
  end
25
25
 
26
+ def download_to_temp_file(file_name)
27
+ @logger.print "Copying local file \"#{file_name}\" to folder \"#{root_path}\"..."
28
+ full_file_name = Pathname(File.join(root_path, file_name))
29
+ Dir.mktmpdir do |dir|
30
+ temp_file = "#{dir}/#{full_file_name.basename}"
31
+ FileUtils.cp(full_file_name, temp_file)
32
+ yield(temp_file)
33
+ end
34
+ @logger.puts 'done.'
35
+ end
36
+
26
37
  def list_files(prefix = '', file_extension = '*')
27
38
  FileUtils.mkdir_p(root_path)
28
39
  @logger.print "Listing \"#{prefix}*.#{file_extension}\" from local folder \"#{root_path}\"..."
@@ -23,4 +23,13 @@ class MemoryFileManager < FileManager
23
23
  @data.delete "#{file_name}"
24
24
  end
25
25
 
26
+ def download_to_temp_file(file_name)
27
+ Dir.mktmpdir do |dir|
28
+ temp_file = "#{dir}/#{Pathname(file_name).basename}"
29
+ content = read_file(file_name)
30
+ File.open(temp_file, 'w') { |file| file.write(content) }
31
+ yield(temp_file)
32
+ end
33
+ end
34
+
26
35
  end
@@ -30,6 +30,19 @@ class S3FileManager < FileManager
30
30
  logger.puts 'done.'
31
31
  end
32
32
 
33
+ def download_to_temp_file(file_name)
34
+ Dir.mktmpdir do |dir|
35
+ @logger.print "Downloading file \"#{bucket_name}/#{file_name}\" to temp folder \"#{dir}\"..."
36
+ temp_file = "#{dir}/#{Pathname(file_name).basename}"
37
+
38
+ s3_client = connect_s3_client
39
+ s3_client.get_object({ bucket: bucket_name, key: file_name }, target: temp_file)
40
+
41
+ yield(temp_file)
42
+ end
43
+ logger.puts 'done.'
44
+ end
45
+
33
46
  def list_files(prefix = '', file_extension = '*')
34
47
  # objects = []
35
48
  # last_key = nil
@@ -80,4 +93,9 @@ class S3FileManager < FileManager
80
93
  service
81
94
  end
82
95
 
96
+ def connect_s3_client
97
+ credentials = Aws::Credentials.new(options[:access_key_id], options[:secret_access_key])
98
+ Aws::S3::Client.new(region: options[:region], credentials: credentials)
99
+ end
100
+
83
101
  end
@@ -48,8 +48,22 @@ module FileManagerTest
48
48
  assert_empty @manager.list_files
49
49
  end
50
50
 
51
+
51
52
  def test_remove_not_existing_file_dont_raise
52
53
  assert_empty @manager.list_files
53
54
  @manager.delete_file 'sub_dir/saved.*'
54
55
  end
56
+
57
+ def test_downloading_to_temp_file
58
+ @manager.save_file 'temp_file/saved', 'content'
59
+ assert_equal 'content', @manager.read_file('temp_file/saved')
60
+
61
+ @manager.download_to_temp_file('temp_file/saved') do |temp_file|
62
+ contents = File.open(temp_file) { |f| f.read }
63
+ assert_equal contents, 'content'
64
+ end
65
+
66
+ @manager.delete_file 'temp_file/saved'
67
+ end
68
+
55
69
  end
@@ -0,0 +1,207 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: put
5
+ uri: https://file-manager-tests.s3-us-west-2.amazonaws.com/temp_file/saved
6
+ body:
7
+ encoding: UTF-8
8
+ string: content
9
+ headers:
10
+ Content-Type:
11
+ - ''
12
+ Accept-Encoding:
13
+ - ''
14
+ User-Agent:
15
+ - aws-sdk-ruby2/2.2.13 ruby/2.2.3 x86_64-darwin14
16
+ Expect:
17
+ - 100-continue
18
+ Content-Md5:
19
+ - mgNkuembtIDdJeHwKEyFVQ==
20
+ X-Amz-Date:
21
+ - 20170905T193111Z
22
+ Host:
23
+ - file-manager-tests.s3-us-west-2.amazonaws.com
24
+ X-Amz-Content-Sha256:
25
+ - ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73
26
+ Authorization:
27
+ - AWS4-HMAC-SHA256 Credential=AKIAJ73G6IOWSUPCKP5A/20170905/us-west-2/s3/aws4_request,
28
+ SignedHeaders=content-md5;host;x-amz-content-sha256;x-amz-date, Signature=77b5ca0c733dca9e50fa94641c075eb003cc5f48828806021d91383003b121c2
29
+ Content-Length:
30
+ - '7'
31
+ Accept:
32
+ - "*/*"
33
+ response:
34
+ status:
35
+ code: 200
36
+ message: OK
37
+ headers:
38
+ X-Amz-Id-2:
39
+ - ThrCrHLPq67iKa3zU+BSgqEJ9qqNz636LiISvezfUKVARKmV+3mE400nDn0IEVEKdrB5CKgKBIg=
40
+ X-Amz-Request-Id:
41
+ - EA7AABC0E1F7C52C
42
+ Date:
43
+ - Tue, 05 Sep 2017 19:31:12 GMT
44
+ Etag:
45
+ - '"9a0364b9e99bb480dd25e1f0284c8555"'
46
+ Content-Length:
47
+ - '0'
48
+ Server:
49
+ - AmazonS3
50
+ body:
51
+ encoding: UTF-8
52
+ string: ''
53
+ http_version:
54
+ recorded_at: Tue, 05 Sep 2017 19:31:12 GMT
55
+ - request:
56
+ method: get
57
+ uri: https://file-manager-tests.s3-us-west-2.amazonaws.com/temp_file/saved
58
+ body:
59
+ encoding: ASCII-8BIT
60
+ string: ''
61
+ headers:
62
+ Content-Type:
63
+ - ''
64
+ Accept-Encoding:
65
+ - ''
66
+ User-Agent:
67
+ - aws-sdk-ruby2/2.2.13 ruby/2.2.3 x86_64-darwin14
68
+ X-Amz-Date:
69
+ - 20170905T193112Z
70
+ Host:
71
+ - file-manager-tests.s3-us-west-2.amazonaws.com
72
+ X-Amz-Content-Sha256:
73
+ - e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
74
+ Authorization:
75
+ - AWS4-HMAC-SHA256 Credential=AKIAJ73G6IOWSUPCKP5A/20170905/us-west-2/s3/aws4_request,
76
+ SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=d77b195464c76daf6a4e0c2d081e9011886a7fe30d5e9eba1fb80bb77568a8fe
77
+ Content-Length:
78
+ - '0'
79
+ Accept:
80
+ - "*/*"
81
+ response:
82
+ status:
83
+ code: 200
84
+ message: OK
85
+ headers:
86
+ X-Amz-Id-2:
87
+ - 9UJ3/6AVPXQ6SLEYwc/+TMoZq0yszEhK1qi8ACLAvyeC8jSy9fBFoWOcvEKwBuLiYPVs8hrOdow=
88
+ X-Amz-Request-Id:
89
+ - 0BE11CB38DBAE349
90
+ Date:
91
+ - Tue, 05 Sep 2017 19:31:14 GMT
92
+ Last-Modified:
93
+ - Tue, 05 Sep 2017 19:31:12 GMT
94
+ Etag:
95
+ - '"9a0364b9e99bb480dd25e1f0284c8555"'
96
+ Accept-Ranges:
97
+ - bytes
98
+ Content-Type:
99
+ - ''
100
+ Content-Length:
101
+ - '7'
102
+ Server:
103
+ - AmazonS3
104
+ body:
105
+ encoding: UTF-8
106
+ string: content
107
+ http_version:
108
+ recorded_at: Tue, 05 Sep 2017 19:31:13 GMT
109
+ - request:
110
+ method: get
111
+ uri: https://file-manager-tests.s3-us-west-2.amazonaws.com/temp_file/saved
112
+ body:
113
+ encoding: ASCII-8BIT
114
+ string: ''
115
+ headers:
116
+ Content-Type:
117
+ - ''
118
+ Accept-Encoding:
119
+ - ''
120
+ User-Agent:
121
+ - aws-sdk-ruby2/2.2.13 ruby/2.2.3 x86_64-darwin14
122
+ X-Amz-Date:
123
+ - 20170905T193113Z
124
+ Host:
125
+ - file-manager-tests.s3-us-west-2.amazonaws.com
126
+ X-Amz-Content-Sha256:
127
+ - e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
128
+ Authorization:
129
+ - AWS4-HMAC-SHA256 Credential=AKIAJ73G6IOWSUPCKP5A/20170905/us-west-2/s3/aws4_request,
130
+ SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=1256dea399728044ac726831903c6881d13fe35e9b2df7815668a3fb7df1e49c
131
+ Content-Length:
132
+ - '0'
133
+ Accept:
134
+ - "*/*"
135
+ response:
136
+ status:
137
+ code: 200
138
+ message: OK
139
+ headers:
140
+ X-Amz-Id-2:
141
+ - lvcN93Sm1hCQ8hL5G/FVyDs1jBrMBGPLcZ1XFwT8S1LGok6LATTuf7ECp/JdRlsruqynqEPBq/w=
142
+ X-Amz-Request-Id:
143
+ - 776F417270F33D9C
144
+ Date:
145
+ - Tue, 05 Sep 2017 19:31:14 GMT
146
+ Last-Modified:
147
+ - Tue, 05 Sep 2017 19:31:12 GMT
148
+ Etag:
149
+ - '"9a0364b9e99bb480dd25e1f0284c8555"'
150
+ Accept-Ranges:
151
+ - bytes
152
+ Content-Type:
153
+ - ''
154
+ Content-Length:
155
+ - '7'
156
+ Server:
157
+ - AmazonS3
158
+ body:
159
+ encoding: UTF-8
160
+ string: content
161
+ http_version:
162
+ recorded_at: Tue, 05 Sep 2017 19:31:13 GMT
163
+ - request:
164
+ method: delete
165
+ uri: https://file-manager-tests.s3-us-west-2.amazonaws.com/temp_file/saved
166
+ body:
167
+ encoding: ASCII-8BIT
168
+ string: ''
169
+ headers:
170
+ Content-Type:
171
+ - ''
172
+ Accept-Encoding:
173
+ - ''
174
+ User-Agent:
175
+ - aws-sdk-ruby2/2.2.13 ruby/2.2.3 x86_64-darwin14
176
+ X-Amz-Date:
177
+ - 20170905T193113Z
178
+ Host:
179
+ - file-manager-tests.s3-us-west-2.amazonaws.com
180
+ X-Amz-Content-Sha256:
181
+ - e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
182
+ Authorization:
183
+ - AWS4-HMAC-SHA256 Credential=AKIAJ73G6IOWSUPCKP5A/20170905/us-west-2/s3/aws4_request,
184
+ SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=0f4da92980176e880b3d5c8f837d270c17d4c1a9849aa905650dc073bcb994ac
185
+ Content-Length:
186
+ - '0'
187
+ Accept:
188
+ - "*/*"
189
+ response:
190
+ status:
191
+ code: 204
192
+ message: No Content
193
+ headers:
194
+ X-Amz-Id-2:
195
+ - RGyAFzIbtaWZ6tWxwpay0kziUm4lUDVuopRLLHrwOl79h+IaCit61Q6J2KO3YEtHuPpjaQ2hP54=
196
+ X-Amz-Request-Id:
197
+ - DFEAEC74800C8768
198
+ Date:
199
+ - Tue, 05 Sep 2017 19:31:15 GMT
200
+ Server:
201
+ - AmazonS3
202
+ body:
203
+ encoding: UTF-8
204
+ string: ''
205
+ http_version:
206
+ recorded_at: Tue, 05 Sep 2017 19:31:14 GMT
207
+ recorded_with: VCR 3.0.1