Skip to content

Commit a28eb76

Browse files
author
Ubuntu
committed
zip up compressed files
1 parent c1708bb commit a28eb76

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ gem 'less'
4343
gem 'less-rails'
4444
gem 'twitter-bootstrap-rails'
4545

46+
# zip
47+
gem 'rubyzip'
48+
4649
group :development, :test do
4750
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
4851
gem 'byebug'

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ GEM
126126
json (~> 1.4)
127127
ref (1.0.5)
128128
rmagick (2.14.0)
129+
rubyzip (1.1.7)
129130
sass (3.4.13)
130131
sass-rails (5.0.3)
131132
railties (>= 4.0.0, < 5.0)
@@ -184,6 +185,7 @@ DEPENDENCIES
184185
less-rails
185186
rails (= 4.2.0)
186187
rmagick
188+
rubyzip
187189
sass-rails (~> 5.0)
188190
sdoc (~> 0.4.0)
189191
spring

lib/tasks/import.rake

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
namespace :pictures do
22
require 'fileutils'
3+
require 'rubygems'
4+
require 'zip'
35

46
S3_BUCKET_NAME = ENV['S3_BUCKET_NAME'] || raise('please set environment var S3_BUCKET_NAME')
7+
8+
desc 'Zip up compressed images'
9+
task :zip => :environment do
10+
generate_zip Rails.root.join('app','assets','images', 'compressed'), Rails.root.join('test.zip')
11+
end
512

613
desc 'Download pictures from a S3 bucket to assets'
714
task :import => :environment do
@@ -39,6 +46,9 @@ namespace :pictures do
3946
generate_compressed File.join(compress_dir, img_name), img
4047
end
4148

49+
# generate zip file
50+
generate_zip compress_dir, Rails.root.join('app', 'assets', 'images.zip')
51+
4252
# done!
4353
puts "All done!"
4454
puts "Total time taken was #{Time.now - start_time} seconds"
@@ -54,4 +64,19 @@ namespace :pictures do
5464
def generate_compressed path, img
5565
img.write(path) { self.quality = 70 }
5666
end
67+
68+
# zip files up so they can be downloaded all at once
69+
def generate_zip folder, zip_path
70+
puts File.join(folder, '*')
71+
input_files = Dir.glob(File.join(folder, '*'))
72+
73+
Zip::File.open(zip_path, Zip::File::CREATE) do |zipfile|
74+
input_files.each do |file|
75+
# Two arguments:
76+
# - The name of the file as it will appear in the archive
77+
# - The original file, including the path to find it
78+
zipfile.add(File.basename(file), file)
79+
end
80+
end
81+
end
5782
end

lib/tasks/import.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
namespace :pictures do
22
require 'fileutils'
3+
require 'rubygems'
4+
require 'zip'
35

46
S3_BUCKET_NAME = ENV['S3_BUCKET_NAME'] || raise('please set environment var S3_BUCKET_NAME')
7+
8+
desc 'Zip up compressed images'
9+
task :zip => :environment do
10+
generate_zip Rails.root.join('app','assets','images', 'compressed'), Rails.root.join('test.zip')
11+
end
512

613
desc 'Download pictures from a S3 bucket to assets'
714
task :import => :environment do
@@ -39,6 +46,9 @@
3946
generate_compressed File.join(compress_dir, img_name), img
4047
end
4148

49+
# generate zip file
50+
generate_zip compress_dir, Rails.root.join('app', 'assets', 'images.zip')
51+
4252
# done!
4353
puts "All done!"
4454
puts "Total time taken was #{Time.now - start_time} seconds"
@@ -54,4 +64,19 @@ def generate_thumbnail path, img
5464
def generate_compressed path, img
5565
img.write(path) { self.quality = 70 }
5666
end
67+
68+
# zip files up so they can be downloaded all at once
69+
def generate_zip folder, zip_path
70+
puts File.join(folder, '*')
71+
input_files = Dir.glob(File.join(folder, '*'))
72+
73+
Zip::File.open(zip_path, Zip::File::CREATE) do |zipfile|
74+
input_files.each do |file|
75+
# Two arguments:
76+
# - The name of the file as it will appear in the archive
77+
# - The original file, including the path to find it
78+
zipfile.add(File.basename(file), file)
79+
end
80+
end
81+
end
5782
end

0 commit comments

Comments
 (0)