file-manager 0.2.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +8 -3
- data/Gemfile +4 -0
- data/Gemfile.lock +32 -31
- data/README.md +12 -1
- data/Rakefile +1 -0
- data/file-manager.gemspec +3 -4
- data/lib/file_manager.rb +4 -0
- data/lib/local_file_manager.rb +36 -1
- data/lib/memory_file_manager.rb +21 -0
- data/lib/s3_file_manager.rb +41 -1
- data/test/coverage_test.rb +15 -0
- data/test/file_manager_test.rb +62 -0
- data/test/{local_test.rb → local_file_manager_test.rb} +3 -0
- data/test/{memory_test.rb → memory_file_manager_test.rb} +3 -0
- data/test/{s3_test.rb → s3_file_manager_test.rb} +2 -0
- data/test/test_helper.rb +3 -4
- data/test/vcr_cassettes/test_copy_file.yml +552 -0
- data/test/vcr_cassettes/test_downloading_to_temp_file.yml +253 -0
- data/test/vcr_cassettes/test_listing_files.yml +120 -119
- data/test/vcr_cassettes/test_listing_files_in_sub_dir.yml +120 -119
- data/test/vcr_cassettes/test_reading.yml +75 -74
- data/test/vcr_cassettes/test_reading_in_sub_dir.yml +75 -74
- data/test/vcr_cassettes/test_remove_not_existing_file_dont_raise.yml +26 -28
- data/test/vcr_cassettes/test_rename_file.yml +691 -0
- metadata +26 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f13f65293d73d3c2a9579e931eece74f25a71dade78f8683c18225d421bdd325
|
4
|
+
data.tar.gz: a4e2a80b4b11d894129f39a843ed1304e197a2b2e92d66f8bbffc7dfefd30306
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4034f8b52e6a0d28219fb97fc7912889193967d84930fd8cfa96a800a175c5f7d27271d6a403cf924fe947b30e1e0ccd97334945be70110357c1de5a08508c4
|
7
|
+
data.tar.gz: 3e04ef0a2d74bcb42ce53618b6e4188e446e28297aa343601d82adb9e1befe256fbafa56b205bcc435b761f66ca2c1871bef8a6fc85ba48dbefcf8cc2853a0f7
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,54 +1,55 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
file-manager (0.
|
5
|
-
aws-sdk (
|
4
|
+
file-manager (0.7.0)
|
5
|
+
aws-sdk-s3 (~> 1)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
addressable (2.
|
11
|
-
|
12
|
-
|
13
|
-
aws-
|
10
|
+
addressable (2.7.0)
|
11
|
+
public_suffix (>= 2.0.2, < 5.0)
|
12
|
+
aws-eventstream (1.1.0)
|
13
|
+
aws-partitions (1.394.0)
|
14
|
+
aws-sdk-core (3.109.3)
|
15
|
+
aws-eventstream (~> 1, >= 1.0.2)
|
16
|
+
aws-partitions (~> 1, >= 1.239.0)
|
17
|
+
aws-sigv4 (~> 1.1)
|
14
18
|
jmespath (~> 1.0)
|
15
|
-
aws-sdk-
|
16
|
-
aws-sdk-core (
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
simplecov-html (0.10.0)
|
34
|
-
vcr (3.0.1)
|
35
|
-
webmock (1.22.6)
|
19
|
+
aws-sdk-kms (1.39.0)
|
20
|
+
aws-sdk-core (~> 3, >= 3.109.0)
|
21
|
+
aws-sigv4 (~> 1.1)
|
22
|
+
aws-sdk-s3 (1.84.1)
|
23
|
+
aws-sdk-core (~> 3, >= 3.109.0)
|
24
|
+
aws-sdk-kms (~> 1)
|
25
|
+
aws-sigv4 (~> 1.1)
|
26
|
+
aws-sigv4 (1.2.2)
|
27
|
+
aws-eventstream (~> 1, >= 1.0.2)
|
28
|
+
crack (0.4.4)
|
29
|
+
hashdiff (1.0.1)
|
30
|
+
jmespath (1.4.0)
|
31
|
+
minitest (5.14.2)
|
32
|
+
public_suffix (4.0.6)
|
33
|
+
rake (13.0.1)
|
34
|
+
single_cov (1.6.0)
|
35
|
+
vcr (6.0.0)
|
36
|
+
webmock (3.9.5)
|
36
37
|
addressable (>= 2.3.6)
|
37
38
|
crack (>= 0.3.2)
|
38
|
-
hashdiff
|
39
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
39
40
|
|
40
41
|
PLATFORMS
|
41
42
|
java
|
42
43
|
ruby
|
43
44
|
|
44
45
|
DEPENDENCIES
|
45
|
-
bundler (
|
46
|
-
codeclimate-test-reporter
|
46
|
+
bundler (>= 1.16.2)
|
47
47
|
file-manager!
|
48
48
|
minitest
|
49
49
|
rake
|
50
|
+
single_cov
|
50
51
|
vcr
|
51
52
|
webmock
|
52
53
|
|
53
54
|
BUNDLED WITH
|
54
|
-
|
55
|
+
2.0.1
|
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
-
# File::Manager
|
1
|
+
# File::Manager
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/fortesinformatica/file-manager.svg?branch=master)](https://travis-ci.org/fortesinformatica/file-manager)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/fortesinformatica/file-manager/badges/gpa.svg)](https://codeclimate.com/github/fortesinformatica/file-manager)
|
2
5
|
|
3
6
|
File manager local or S3
|
4
7
|
|
@@ -37,6 +40,14 @@ file_manager_txt = factory.create(:txt)
|
|
37
40
|
|
38
41
|
file_manager_txt.save_file('babau.txt', 'buuuu')
|
39
42
|
|
43
|
+
## Testing
|
44
|
+
|
45
|
+
```sh
|
46
|
+
export AWS_ACCESS_KEY_ID='' # with your keys
|
47
|
+
export AWS_SECRET_ACCESS_KEY=''
|
48
|
+
bundle exec rake
|
49
|
+
```
|
50
|
+
|
40
51
|
## Contributing
|
41
52
|
|
42
53
|
1. Fork it
|
data/Rakefile
CHANGED
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.
|
6
|
+
spec.version = '0.7.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'
|
@@ -16,11 +16,10 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.require_paths = ["lib"]
|
17
17
|
spec.homepage = 'https://github.com/fortesinformatica/file-manager'
|
18
18
|
|
19
|
-
spec.add_dependency 'aws-sdk', '
|
20
|
-
spec.add_development_dependency "bundler", "
|
19
|
+
spec.add_dependency 'aws-sdk-s3', '~> 1'
|
20
|
+
spec.add_development_dependency "bundler", ">= 1.16.2"
|
21
21
|
spec.add_development_dependency "rake"
|
22
22
|
spec.add_development_dependency 'minitest'
|
23
23
|
spec.add_development_dependency 'vcr'
|
24
24
|
spec.add_development_dependency 'webmock'
|
25
|
-
spec.add_development_dependency 'codeclimate-test-reporter'
|
26
25
|
end
|
data/lib/file_manager.rb
CHANGED
data/lib/local_file_manager.rb
CHANGED
@@ -23,6 +23,19 @@ 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
|
+
rescue Errno::ENOENT
|
36
|
+
raise FileNotFoundError.new(full_file_name)
|
37
|
+
end
|
38
|
+
|
26
39
|
def list_files(prefix = '', file_extension = '*')
|
27
40
|
FileUtils.mkdir_p(root_path)
|
28
41
|
@logger.print "Listing \"#{prefix}*.#{file_extension}\" from local folder \"#{root_path}\"..."
|
@@ -37,8 +50,30 @@ class LocalFileManager < FileManager
|
|
37
50
|
def delete_file file_name
|
38
51
|
@logger.print "Deleting file \"#{file_name}\" from local folder \"#{root_path}\"..."
|
39
52
|
full_file_name = File.join(root_path, file_name)
|
40
|
-
File.delete(full_file_name) if File.
|
53
|
+
File.delete(full_file_name) if File.exist?(full_file_name)
|
54
|
+
@logger.puts 'done.'
|
55
|
+
end
|
56
|
+
|
57
|
+
def rename_file original_file_name, target_file_name
|
58
|
+
@logger.print "Renaming file \"#{original_file_name}\" to \"#{target_file_name}\" from local folder \"#{root_path}\"..."
|
59
|
+
full_original_file_name = File.join(root_path, original_file_name)
|
60
|
+
full_target_file_name = Pathname(File.join(root_path, target_file_name))
|
61
|
+
FileUtils.mkdir_p(full_target_file_name.dirname)
|
62
|
+
FileUtils.mv full_original_file_name, full_target_file_name
|
41
63
|
@logger.puts 'done.'
|
64
|
+
rescue Errno::ENOENT
|
65
|
+
raise FileNotFoundError.new(full_original_file_name)
|
66
|
+
end
|
67
|
+
|
68
|
+
def copy_file source_file_name, target_file_name
|
69
|
+
@logger.print "Copying file \"#{source_file_name}\" to \"#{target_file_name}\" from local folder \"#{root_path}\"..."
|
70
|
+
full_source_file_name = File.join(root_path, source_file_name)
|
71
|
+
full_target_file_name = Pathname(File.join(root_path, target_file_name))
|
72
|
+
FileUtils.mkdir_p(full_target_file_name.dirname)
|
73
|
+
FileUtils.cp full_source_file_name, full_target_file_name
|
74
|
+
@logger.puts 'done.'
|
75
|
+
rescue Errno::ENOENT
|
76
|
+
raise FileNotFoundError.new(full_source_file_name)
|
42
77
|
end
|
43
78
|
|
44
79
|
private
|
data/lib/memory_file_manager.rb
CHANGED
@@ -16,6 +16,10 @@ class MemoryFileManager < FileManager
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def list_files(prefix = '', file_extension = '*')
|
19
|
+
if prefix == '' && file_extension == '*'
|
20
|
+
return @data.keys
|
21
|
+
end
|
22
|
+
|
19
23
|
@data.keys.select{|key| key.start_with?(prefix) && key.end_with?(".#{file_extension}")}
|
20
24
|
end
|
21
25
|
|
@@ -23,4 +27,21 @@ class MemoryFileManager < FileManager
|
|
23
27
|
@data.delete "#{file_name}"
|
24
28
|
end
|
25
29
|
|
30
|
+
def download_to_temp_file(file_name)
|
31
|
+
Dir.mktmpdir do |dir|
|
32
|
+
temp_file = "#{dir}/#{Pathname(file_name).basename}"
|
33
|
+
content = read_file(file_name)
|
34
|
+
File.open(temp_file, 'w') { |file| file.write(content) }
|
35
|
+
yield(temp_file)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def rename_file original_file_name, target_file_name
|
40
|
+
@data["#{target_file_name}"] = @data["#{original_file_name}"] || (raise FileNotFoundError.new("No such file '#{original_file_name}'"))
|
41
|
+
@data.delete "#{original_file_name}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def copy_file source_file_name, target_file_name
|
45
|
+
@data["#{target_file_name}"] = @data["#{source_file_name}"] || (raise FileNotFoundError.new("No such file '#{source_file_name}'"))
|
46
|
+
end
|
26
47
|
end
|
data/lib/s3_file_manager.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'file_manager'
|
2
|
-
require 'aws-sdk'
|
2
|
+
require 'aws-sdk-s3'
|
3
3
|
|
4
4
|
class S3FileManager < FileManager
|
5
5
|
|
@@ -30,6 +30,21 @@ 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
|
+
rescue Aws::S3::Errors::NoSuchKey
|
45
|
+
raise FileNotFoundError.new("#{bucket_name}/#{file_name}")
|
46
|
+
end
|
47
|
+
|
33
48
|
def list_files(prefix = '', file_extension = '*')
|
34
49
|
# objects = []
|
35
50
|
# last_key = nil
|
@@ -67,6 +82,26 @@ class S3FileManager < FileManager
|
|
67
82
|
logger.puts 'done.'
|
68
83
|
end
|
69
84
|
|
85
|
+
def rename_file original_file_name, target_file_name
|
86
|
+
s3_service = connect_s3_service
|
87
|
+
bucket = s3_service.bucket(bucket_name)
|
88
|
+
logger.print "Renaming file \"#{original_file_name}\" \"#{target_file_name}\" from bucket \"#{bucket_name}\"..."
|
89
|
+
bucket.object(original_file_name).move_to(bucket: bucket_name, key: target_file_name)
|
90
|
+
logger.puts 'done.'
|
91
|
+
rescue Aws::S3::Errors::NoSuchKey
|
92
|
+
raise FileNotFoundError.new("#{bucket_name}/#{original_file_name}")
|
93
|
+
end
|
94
|
+
|
95
|
+
def copy_file source_file_name, target_file_name
|
96
|
+
s3_service = connect_s3_service
|
97
|
+
bucket = s3_service.bucket(bucket_name)
|
98
|
+
logger.print "Copying file \"#{source_file_name}\" \"#{target_file_name}\" from bucket \"#{bucket_name}\"..."
|
99
|
+
bucket.object(source_file_name).copy_to(bucket: bucket_name, key: target_file_name)
|
100
|
+
logger.puts 'done.'
|
101
|
+
rescue Aws::S3::Errors::NoSuchKey
|
102
|
+
raise FileNotFoundError.new("#{bucket_name}/#{source_file_name}")
|
103
|
+
end
|
104
|
+
|
70
105
|
private
|
71
106
|
|
72
107
|
def connect_s3_service
|
@@ -80,4 +115,9 @@ class S3FileManager < FileManager
|
|
80
115
|
service
|
81
116
|
end
|
82
117
|
|
118
|
+
def connect_s3_client
|
119
|
+
credentials = Aws::Credentials.new(options[:access_key_id], options[:secret_access_key])
|
120
|
+
Aws::S3::Client.new(region: options[:region], credentials: credentials)
|
121
|
+
end
|
122
|
+
|
83
123
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require './test/test_helper'
|
2
|
+
SingleCov.not_covered! # not testing any code in lib/
|
3
|
+
|
4
|
+
class CoverageTest < Minitest::Test
|
5
|
+
def test_for_untested_code
|
6
|
+
# option :tests to pass custom Dir.glob results
|
7
|
+
SingleCov.assert_used
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_for_untested_files
|
11
|
+
# option :tests and :files to pass custom Dir.glob results
|
12
|
+
# :untested to get it passing with known untested files
|
13
|
+
SingleCov.assert_tested untested: %w[lib/file_manager_factory.rb]
|
14
|
+
end
|
15
|
+
end
|
data/test/file_manager_test.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require './test/test_helper'
|
2
2
|
require 'memory_file_manager'
|
3
3
|
|
4
|
+
SingleCov.covered! uncovered: 5
|
5
|
+
|
4
6
|
module FileManagerTest
|
5
7
|
def test_reading
|
6
8
|
assert_raises(FileNotFoundError) { @manager.read_file 'not_saved' }
|
@@ -48,8 +50,68 @@ module FileManagerTest
|
|
48
50
|
assert_empty @manager.list_files
|
49
51
|
end
|
50
52
|
|
53
|
+
|
51
54
|
def test_remove_not_existing_file_dont_raise
|
52
55
|
assert_empty @manager.list_files
|
53
56
|
@manager.delete_file 'sub_dir/saved.*'
|
54
57
|
end
|
58
|
+
|
59
|
+
def test_downloading_to_temp_file
|
60
|
+
@manager.save_file 'temp_file/saved', 'content'
|
61
|
+
assert_equal 'content', @manager.read_file('temp_file/saved')
|
62
|
+
|
63
|
+
@manager.download_to_temp_file('temp_file/saved') do |temp_file|
|
64
|
+
contents = File.open(temp_file) { |f| f.read }
|
65
|
+
assert_equal contents, 'content'
|
66
|
+
end
|
67
|
+
|
68
|
+
@manager.delete_file 'temp_file/saved'
|
69
|
+
assert_raises(FileNotFoundError) { @manager.download_to_temp_file('temp_file/saved') {} }
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_rename_file
|
73
|
+
assert_empty @manager.list_files
|
74
|
+
@manager.save_file 'original/name.txt', 'rename_content'
|
75
|
+
|
76
|
+
@manager.rename_file 'original/name.txt', 'new/name.txt'
|
77
|
+
assert_equal ['new/name.txt'], @manager.list_files
|
78
|
+
assert_equal 'rename_content', @manager.read_file('new/name.txt')
|
79
|
+
|
80
|
+
@manager.rename_file 'new/name.txt', 'new/new2/name.txt'
|
81
|
+
assert_equal ['new/new2/name.txt'], @manager.list_files
|
82
|
+
assert_equal 'rename_content', @manager.read_file('new/new2/name.txt')
|
83
|
+
|
84
|
+
assert_raises(FileNotFoundError) { @manager.rename_file 'nao_existe/name.txt', 'new/name.txt' }
|
85
|
+
ensure
|
86
|
+
@manager.delete_file 'original/name.txt'
|
87
|
+
@manager.delete_file 'new/new2/name.txt'
|
88
|
+
assert_empty @manager.list_files
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_copy_file
|
92
|
+
assert_empty @manager.list_files
|
93
|
+
@manager.save_file 'original/name.txt', 'same_content'
|
94
|
+
|
95
|
+
@manager.copy_file 'original/name.txt', 'new/name.txt'
|
96
|
+
assert_equal ['new/name.txt', 'original/name.txt'].sort, @manager.list_files.sort
|
97
|
+
assert_equal 'same_content', @manager.read_file('new/name.txt')
|
98
|
+
|
99
|
+
@manager.copy_file 'original/name.txt', 'new/name.txt'
|
100
|
+
assert_equal ['new/name.txt', 'original/name.txt'].sort, @manager.list_files.sort
|
101
|
+
|
102
|
+
assert_raises(FileNotFoundError) { @manager.copy_file 'nao_existe/name.txt', 'new/name.txt' }
|
103
|
+
ensure
|
104
|
+
@manager.delete_file 'original/name.txt'
|
105
|
+
@manager.delete_file 'new/name.txt'
|
106
|
+
assert_empty @manager.list_files
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
class FileManagerLoggerTest < Minitest::Test
|
111
|
+
|
112
|
+
def test_logger_prints_and_puts
|
113
|
+
logger = FileManager::Logger.new({})
|
114
|
+
assert_output("hello\n") { logger.puts "hello" }
|
115
|
+
assert_output("hi") { logger.print "hi" }
|
116
|
+
end
|
55
117
|
end
|