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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5dbef1dda4a5236e3290d9cd38a23de7e296b5e404356bf222dfbeb0c75197b
4
- data.tar.gz: d2558817de8b496c48606c1e3af71df086480eede14b55186ea4a5e7206bfe5c
3
+ metadata.gz: 5a822b74744d8bda686b1c1e357ca747ba60e3642b2fa8c746ae31daeff56e0f
4
+ data.tar.gz: 6f8ead51a8f379a0d742b95705c8b8737850fe124d64a0bde67e3a5ecacc351d
5
5
  SHA512:
6
- metadata.gz: 49f3758cc21c8a66dce8f69fe231c679f0802ad8374a222f8a178557ac668b5c2cf3ce7ea4a4f1d0c1e40dcb701a65c200de7bea3e82089cc56a3e20d9495e5f
7
- data.tar.gz: 8830d24a60774f277e4576614243a077f661d08313259aaa501692ca5f58cb5bb13ef8975e6659432433d366195211e5f3c38c8bd25e2312feee1352908bf7a6
6
+ metadata.gz: 1beee83a8165c020a8bedd6cd4427288e54f9fe0fe177f2dbb4f4aaf7adbd2ebac773ebce315e6319406768ee56bb66f91549e7871932154a5ead4bc87004568
7
+ data.tar.gz: 4dc79d8813340c86c33c0ded3f2c092d5f94d47f5cecb58c9692a3cd2f7d53a2d651b222790e62b0c5d38c38b84becd2597451d9b237abd90436253eafdcc737
data/CHANGELOG.yml CHANGED
@@ -1,3 +1,5 @@
1
+ 0.3.0:
2
+ - correct subfolders creation (and break some methods signature)
1
3
  0.2.0:
2
4
  - move some methods to avoid to polute controller methods
3
5
  - `ActiveStorage::SendZip#save_files_on_server` become `ActiveStorage::SendZipHelperSendZip#save_files_on_server`
@@ -1,6 +1,6 @@
1
1
  module ActiveStorage
2
2
  module SendZip
3
3
  # The version of this gem
4
- VERSION = '0.2.0'.freeze
4
+ VERSION = '0.3.0'.freeze
5
5
  end
6
6
  end
@@ -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 [Array<String>] files paths of saved files
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
- return files.map { |file| save_file_on_server(file, temp_folder) }
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.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
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 filepaths [Array<String>] files paths
65
+ # @param folderpath [String] folder path
63
66
  # @return [String] as content of zip
64
- def self.create_temporary_zip_file(filepaths)
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
- 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
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
- filepaths.each { |filepath| FileUtils.rm(filepath) }
88
+ # temp_file.close
89
+ # temp_file.unlink
90
+ # FileUtils.rm_rf(folderpath)
86
91
  end
87
92
  end
88
93
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_storage-send_zip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre Rousseau