Skip to content

Commit 33b762a

Browse files
committed
Correct subfolder creation
1 parent 1cf28cc commit 33b762a

File tree

4 files changed

+59
-22
lines changed

4 files changed

+59
-22
lines changed

CHANGELOG.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
0.3.0:
2+
- correct subfolders creation (and break some methods signature)
13
0.2.0:
24
- move some methods to avoid to polute controller methods
35
- `ActiveStorage::SendZip#save_files_on_server` become `ActiveStorage::SendZipHelperSendZip#save_files_on_server`
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ActiveStorage
22
module SendZip
33
# The version of this gem
4-
VERSION = '0.2.0'.freeze
4+
VERSION = '0.3.0'.freeze
55
end
66
end

lib/active_storage/send_zip_helper.rb

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,26 @@ module SendZipHelper
1010
# Download active storage files on server in a temporary folder
1111
#
1212
# @param files [ActiveStorage::Attached::Many] files to save
13-
# @return [Array<String>] files paths of saved files
13+
# @return [String] folder path of saved files
1414
def self.save_files_on_server(files)
1515
require 'zip'
1616
# get a temporary folder and create it
1717
temp_folder = Dir.mktmpdir 'active_storage-send_zip'
1818

1919
if files.is_a? Array
20-
return files.map { |file| save_file_on_server(file, temp_folder) }
20+
files.each { |file| save_file_on_server(file, temp_folder) }
2121
elsif files.is_a? Hash
2222
filepaths = []
2323

2424
files.each do |subfolder, filesHash|
25-
filesHash.each { |f| filepaths << save_file_on_server(f, temp_folder, subfolder: subfolder) }
25+
filesHash = [filesHash] unless filesHash.is_a? Array
26+
filesHash.each do |f|
27+
filepaths << save_file_on_server(f, temp_folder, subfolder: subfolder.to_s)
28+
end
2629
end
27-
28-
return filepaths
2930
end
31+
32+
temp_folder
3033
end
3134

3235
# Save the given file on the server
@@ -59,30 +62,32 @@ def self.save_file_on_server(file, folder, subfolder: nil)
5962

6063
# Create a temporary zip file & return the content as bytes
6164
#
62-
# @param filepaths [Array<String>] files paths
65+
# @param folderpath [String] folder path
6366
# @return [String] as content of zip
64-
def self.create_temporary_zip_file(filepaths)
67+
def self.create_temporary_zip_file(folderpath)
6568
temp_file = Tempfile.new('user.zip')
69+
folderpath_glob = File.join folderpath, '**', '*'
70+
71+
files = Dir.glob(folderpath_glob).reject { |e| File.directory? e }
6672

6773
begin
6874
# Initialize the temp file as a zip file
6975
Zip::OutputStream.open(temp_file) { |zos| }
7076

7177
# open the zip
7278
Zip::File.open(temp_file.path, Zip::File::CREATE) do |zip|
73-
filepaths.each do |filepath|
74-
filename = File.basename filepath
75-
# add file into the zip
76-
zip.add filename, filepath
79+
files.each do |filepath|
80+
filepath_zip = filepath.sub(folderpath, '').sub(File::SEPARATOR, '')
81+
zip.add filepath_zip, filepath
7782
end
7883
end
7984

8085
return File.read(temp_file.path)
8186
ensure
8287
# close all ressources & remove temporary files
83-
temp_file.close
84-
temp_file.unlink
85-
filepaths.each { |filepath| FileUtils.rm(filepath) }
88+
# temp_file.close
89+
# temp_file.unlink
90+
# FileUtils.rm_rf(folderpath)
8691
end
8792
end
8893
end

test/active_storage/send_zip_helper_test.rb

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,47 @@ def test_it_should_save_files_in_differents_folders
5151
ActiveStorageMock.new('bar.txt')
5252
]
5353
}
54-
files_saved = ActiveStorage::SendZipHelper.save_files_on_server(files)
55-
assert_equal 2, files_saved.map { |f| File.dirname f }.uniq.count
56-
assert_equal 3, files_saved.count
57-
refute_nil ActiveStorage::SendZipHelper.create_temporary_zip_file(files_saved)
54+
assert_produce_nested_files files, folder_count: 2, files_count: 3
55+
end
56+
57+
def test_it_should_save_files_in_differents_folders_with_root_files
58+
files = {
59+
'folder A' => [
60+
ActiveStorageMock.new('foo.txt'),
61+
ActiveStorageMock.new('foo.txt')
62+
],
63+
'folder B' => [
64+
ActiveStorageMock.new('bar.txt')
65+
],
66+
0 => ActiveStorageMock.new('foo.txt'),
67+
1 => ActiveStorageMock.new('bar.txt')
68+
}
69+
assert_produce_nested_files files, folder_count: 4, files_count: 5
5870
end
5971

6072
private
6173

74+
def assert_produce_nested_files(files, folder_count: 1, files_count: 1)
75+
temp_folder = ActiveStorage::SendZipHelper.save_files_on_server(files)
76+
temp_folder_glob = File.join temp_folder, '**', '*'
77+
78+
glob = Dir.glob(temp_folder_glob)
79+
80+
files_paths = glob.reject { |e| File.directory? e }
81+
folders_paths = glob.select { |e| File.directory? e }
82+
83+
assert_equal folder_count, folders_paths.count
84+
assert_equal files_count, files_paths.count
85+
refute_nil ActiveStorage::SendZipHelper.create_temporary_zip_file(temp_folder)
86+
end
87+
6288
def assert_produce_files(files, count: 1)
63-
files_saved = ActiveStorage::SendZipHelper.save_files_on_server(files)
64-
assert_equal count, files_saved.count
65-
refute_nil ActiveStorage::SendZipHelper.create_temporary_zip_file(files_saved)
89+
temp_folder = ActiveStorage::SendZipHelper.save_files_on_server(files)
90+
91+
temp_folder_glob = File.join temp_folder, '**', '*'
92+
files_path = Dir.glob(temp_folder_glob).reject { |e| File.directory? e }
93+
94+
assert_equal count, files_path.count
95+
refute_nil ActiveStorage::SendZipHelper.create_temporary_zip_file(temp_folder)
6696
end
6797
end

0 commit comments

Comments
 (0)