Skip to content

Commit

Permalink
Merge pull request #8 from GAPartners/master
Browse files Browse the repository at this point in the history
Recursive File structure
  • Loading branch information
madeindjs authored Feb 22, 2020
2 parents 65921be + 566a07f commit 9d8ec11
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 25 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ class HolidaysController < ApplicationController
def zip
send_zip {
'Holidays in Lyon <3' => Holidays.where(place: 'lyon').first.pictures,
'Holidays in Paris' => Holidays.where(place: 'paris').first.pictures,
'Holidays in Paris' => [
'Eiffle Tower' => Holidays.where(place: 'eiffle_tower').first.pictures,
Holidays.where(place: 'paris').first.pictures
]
}
end
end
Expand All @@ -75,6 +78,10 @@ Will produce a `.zip` archive like this:
│   ├── b.png
│   └── c.gif
└── Holidays in Paris
├── Eiffle Tower
├── d.jpg
├── e.jpg
├── f.jpg
├── a.jpg
├── b.png
└── c.gif
Expand Down
6 changes: 4 additions & 2 deletions active_storage-send_zip.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'active_storage/send_zip/version'
Expand Down Expand Up @@ -38,6 +40,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '~> 1.17'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_dependency "rails", "> 5.2"
spec.add_dependency "rubyzip", "~> 1.2"
spec.add_dependency 'rails', '> 5.2'
spec.add_dependency 'rubyzip', '< 3.0'
end
4 changes: 3 additions & 1 deletion lib/active_storage/send_zip/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module ActiveStorage
module SendZip
# The version of this gem
VERSION = '0.3.2'.freeze
VERSION = '0.3.3'
end
end
41 changes: 32 additions & 9 deletions lib/active_storage/send_zip_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rails'
require 'zip'
require 'tempfile'
Expand All @@ -17,14 +19,7 @@ def self.save_files_on_server(files)
temp_folder = Dir.mktmpdir 'active_storage-send_zip'

if files.is_a? Hash
filepaths = []

files.each do |subfolder, filesHash|
filesHash = [filesHash] unless filesHash.is_a? Array
filesHash.each do |f|
filepaths << save_file_on_server(f, temp_folder, subfolder: subfolder.to_s)
end
end
filepaths = construct_with_hash(files, temp_folder)
elsif files.respond_to? :each
files.each { |file| save_file_on_server(file, temp_folder) }
else
Expand All @@ -34,6 +29,34 @@ def self.save_files_on_server(files)
temp_folder
end

# Parses hash to build out directories where folders are key names
#
# @param files [ActiveStorage::Attached::One|ActiveStorage::Attached::Many|Array|Hash] file(s) to save
# @return [String] folder path of saved files
def self.construct_with_hash(files, temp_folder)
filepaths = []

files.each do |subfolder, filesHash|
filesHash = [filesHash] unless filesHash.is_a? Array

filesHash.each do |f|
# Build directory strucutre recursively for hashes
if f.is_a? Hash
# Build folder for hash entry
folder = File.join(temp_folder, subfolder.to_s)
Dir.mkdir(folder) unless Dir.exist?(folder)

filepaths += construct_with_hash(f, folder)
# Save attachement
else
filepaths << save_file_on_server(f, temp_folder, subfolder: subfolder.to_s)
end
end
end

filepaths
end

# Save the given file on the server
#
# @param file [ActiveStorage::Attached] files to save
Expand Down Expand Up @@ -84,7 +107,7 @@ def self.create_temporary_zip_file(folderpath)
end
end

return File.read(temp_file.path)
File.read(temp_file.path)
ensure
# close all ressources & remove temporary files
# temp_file.close
Expand Down
29 changes: 17 additions & 12 deletions test/active_storage/send_zip_helper_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

require 'test_helper'
require 'pathname'

class ActiveStorageMock
attr_reader :filename, :download

def initialize(filename)
def initialize(filename = 'foo.txt')
@filename = filename
@download = 'content of file'
end
Expand All @@ -17,15 +19,15 @@ def test_that_it_has_a_version_number

def test_it_should_save_one_active_support
files = [
ActiveStorageMock.new('foo.txt')
ActiveStorageMock.new
]

assert_produce_files files
end

def test_it_should_save_two_active_support
files = [
ActiveStorageMock.new('foo.txt'),
ActiveStorageMock.new,
ActiveStorageMock.new('bar.txt')
]

Expand All @@ -34,8 +36,8 @@ def test_it_should_save_two_active_support

def test_it_should_save_two_active_support
files = [
ActiveStorageMock.new('foo.txt'),
ActiveStorageMock.new('foo.txt')
ActiveStorageMock.new,
ActiveStorageMock.new
]

assert_produce_files files, count: 2
Expand All @@ -44,8 +46,8 @@ def test_it_should_save_two_active_support
def test_it_should_save_files_in_differents_folders
files = {
'folder A' => [
ActiveStorageMock.new('foo.txt'),
ActiveStorageMock.new('foo.txt')
ActiveStorageMock.new,
ActiveStorageMock.new
],
'folder B' => [
ActiveStorageMock.new('bar.txt')
Expand All @@ -63,16 +65,19 @@ def test_it_should_raise_an_exception
def test_it_should_save_files_in_differents_folders_with_root_files
files = {
'folder A' => [
ActiveStorageMock.new('foo.txt'),
ActiveStorageMock.new('foo.txt')
ActiveStorageMock.new,
ActiveStorageMock.new
],
'folder B' => [
ActiveStorageMock.new('bar.txt')
ActiveStorageMock.new('bar.txt'),
'folder C' => [
ActiveStorageMock.new
]
],
0 => ActiveStorageMock.new('foo.txt'),
0 => ActiveStorageMock.new,
1 => ActiveStorageMock.new('bar.txt')
}
assert_produce_nested_files files, folder_count: 4, files_count: 5
assert_produce_nested_files files, folder_count: 5, files_count: 6
end

private
Expand Down

0 comments on commit 9d8ec11

Please sign in to comment.