File tree 2 files changed +28
-11
lines changed
2 files changed +28
-11
lines changed Original file line number Diff line number Diff line change 2
2
require 'rails'
3
3
require 'zip'
4
4
require 'tempfile'
5
+ require 'pathname'
5
6
6
7
module ActiveStorage
7
8
module SendZip
@@ -31,15 +32,31 @@ def save_files_on_server(files)
31
32
require 'zip'
32
33
# get a temporary folder and create it
33
34
temp_folder = Dir . mktmpdir 'active_storage-send_zip'
34
- # FileUtils.mkdir_p(temp_folder) unless Dir.exist?(temp_folder)
35
+
36
+ # count each files to avoid duplicates
37
+ filepaths = [ ]
35
38
36
39
# download all ActiveStorage into
37
- files . map do |picture |
40
+ files . each do |picture |
38
41
filename = picture . filename . to_s
39
42
filepath = File . join temp_folder , filename
43
+
44
+ # ensure that filename not exists
45
+ if filepaths . include? filepath
46
+ # create a new random filenames
47
+ basename = File . basename filename
48
+ extension = File . extname filename
49
+
50
+ filename = "#{ basename } _#{ SecureRandom . uuid } #{ extension } "
51
+ filepath = File . join temp_folder , filename
52
+ end
53
+
40
54
File . open ( filepath , 'wb' ) { |f | f . write ( picture . download ) }
41
- filepath
55
+
56
+ filepaths << filepath
42
57
end
58
+
59
+ filepaths
43
60
end
44
61
45
62
# Create a temporary zip file & return the content as bytes
Original file line number Diff line number Diff line change @@ -43,14 +43,14 @@ def test_it_should_save_two_active_support
43
43
assert_produce_files files , count : 2
44
44
end
45
45
46
- # def test_it_should_save_two_active_support
47
- # files = [
48
- # ActiveStorageMock.new('foo.txt'),
49
- # ActiveStorageMock.new('foo.txt')
50
- # ]
51
- #
52
- # assert_produce_files files, count: 2
53
- # end
46
+ def test_it_should_save_two_active_support
47
+ files = [
48
+ ActiveStorageMock . new ( 'foo.txt' ) ,
49
+ ActiveStorageMock . new ( 'foo.txt' )
50
+ ]
51
+
52
+ assert_produce_files files , count : 2
53
+ end
54
54
55
55
private
56
56
You can’t perform that action at this time.
0 commit comments