active_storage-send_zip 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 +4 -4
- data/CHANGELOG.yml +2 -0
- data/lib/active_storage/send_zip/version.rb +1 -1
- data/lib/active_storage/send_zip_helper.rb +19 -14
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a822b74744d8bda686b1c1e357ca747ba60e3642b2fa8c746ae31daeff56e0f
|
4
|
+
data.tar.gz: 6f8ead51a8f379a0d742b95705c8b8737850fe124d64a0bde67e3a5ecacc351d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1beee83a8165c020a8bedd6cd4427288e54f9fe0fe177f2dbb4f4aaf7adbd2ebac773ebce315e6319406768ee56bb66f91549e7871932154a5ead4bc87004568
|
7
|
+
data.tar.gz: 4dc79d8813340c86c33c0ded3f2c092d5f94d47f5cecb58c9692a3cd2f7d53a2d651b222790e62b0c5d38c38b84becd2597451d9b237abd90436253eafdcc737
|
data/CHANGELOG.yml
CHANGED
@@ -10,23 +10,26 @@ module ActiveStorage
|
|
10
10
|
# Download active storage files on server in a temporary folder
|
11
11
|
#
|
12
12
|
# @param files [ActiveStorage::Attached::Many] files to save
|
13
|
-
# @return [
|
13
|
+
# @return [String] folder path of saved files
|
14
14
|
def self.save_files_on_server(files)
|
15
15
|
require 'zip'
|
16
16
|
# get a temporary folder and create it
|
17
17
|
temp_folder = Dir.mktmpdir 'active_storage-send_zip'
|
18
18
|
|
19
19
|
if files.is_a? Array
|
20
|
-
|
20
|
+
files.each { |file| save_file_on_server(file, temp_folder) }
|
21
21
|
elsif files.is_a? Hash
|
22
22
|
filepaths = []
|
23
23
|
|
24
24
|
files.each do |subfolder, filesHash|
|
25
|
-
filesHash
|
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
|
26
29
|
end
|
27
|
-
|
28
|
-
return filepaths
|
29
30
|
end
|
31
|
+
|
32
|
+
temp_folder
|
30
33
|
end
|
31
34
|
|
32
35
|
# Save the given file on the server
|
@@ -59,10 +62,13 @@ module ActiveStorage
|
|
59
62
|
|
60
63
|
# Create a temporary zip file & return the content as bytes
|
61
64
|
#
|
62
|
-
# @param
|
65
|
+
# @param folderpath [String] folder path
|
63
66
|
# @return [String] as content of zip
|
64
|
-
def self.create_temporary_zip_file(
|
67
|
+
def self.create_temporary_zip_file(folderpath)
|
65
68
|
temp_file = Tempfile.new('user.zip')
|
69
|
+
folderpath_glob = File.join folderpath, '**', '*'
|
70
|
+
|
71
|
+
files = Dir.glob(folderpath_glob).reject { |e| File.directory? e }
|
66
72
|
|
67
73
|
begin
|
68
74
|
# Initialize the temp file as a zip file
|
@@ -70,19 +76,18 @@ module ActiveStorage
|
|
70
76
|
|
71
77
|
# open the zip
|
72
78
|
Zip::File.open(temp_file.path, Zip::File::CREATE) do |zip|
|
73
|
-
|
74
|
-
|
75
|
-
|
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
|
77
82
|
end
|
78
83
|
end
|
79
84
|
|
80
85
|
return File.read(temp_file.path)
|
81
86
|
ensure
|
82
87
|
# close all ressources & remove temporary files
|
83
|
-
temp_file.close
|
84
|
-
temp_file.unlink
|
85
|
-
|
88
|
+
# temp_file.close
|
89
|
+
# temp_file.unlink
|
90
|
+
# FileUtils.rm_rf(folderpath)
|
86
91
|
end
|
87
92
|
end
|
88
93
|
end
|