Skip to content

Commit

Permalink
Fix generation of zip folder to account for reading from AWS fog, add…
Browse files Browse the repository at this point in the history
…resses #10.
  • Loading branch information
remomueller committed Sep 27, 2019
1 parent 333c2cd commit 2a3ffe8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
3 changes: 1 addition & 2 deletions app/controllers/folders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ def show
# GET /docs/:category/:folder/download-zip
def download_zip
@folder.generate_zipped_folder!
filename = "lofthf-study-#{@folder.category.slug}-#{@folder.slug}.zip"

if Rails.env.production?
redirect_to @folder.zipped_folder.url(query: { "response-content-disposition" => "attachment" }), allow_other_host: true
else
send_file_if_present @folder.zipped_folder, filename: filename
send_file_if_present @folder.zipped_folder
end
end

Expand Down
17 changes: 7 additions & 10 deletions app/models/folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,22 @@ def attach_file!(file)
end

def generate_zipped_folder!
zip_name = "folder.zip"
zip_name = "lofthf-study-#{category.slug}-#{slug}.zip"
temp_zip_file = Tempfile.new(zip_name)
begin
# Initialize temp zip file.
Zip::OutputStream.open(temp_zip_file) { |zos| }
# Write to temp zip file.
Zip::File.open(temp_zip_file, Zip::File::CREATE) do |zip|
stream = Zip::OutputStream::write_buffer do |zos|
documents.each do |document|
# Two arguments:
# - The name of the file as it will appear in the archive
# - The original file, including the path to find it
zip.add(document.filename, document.file.path) if File.exist?(document.file.path) && File.size(document.file.path).positive?
zos.put_next_entry document.filename
zos.write document.file.read
end
end
stream.rewind
temp_zip_file.write(stream.sysread)
temp_zip_file.define_singleton_method(:original_filename) do
zip_name
end
update zipped_folder: temp_zip_file
# update file_size: zipped_folder.size # Cache after attaching to model.
update zipped_folder_byte_size: zipped_folder.size
ensure
# Close and delete the temp file
temp_zip_file.close
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddZippedFolderByteSizeToFolders < ActiveRecord::Migration[6.0]
def change
add_column :folders, :zipped_folder_byte_size, :bigint, null: false, default: 0
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_09_26_234257) do
ActiveRecord::Schema.define(version: 2019_09_27_010705) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -80,6 +80,7 @@
t.datetime "updated_at", null: false
t.integer "documents_count", default: 0, null: false
t.string "zipped_folder"
t.bigint "zipped_folder_byte_size", default: 0, null: false
t.index ["archived"], name: "index_folders_on_archived"
t.index ["category_id", "name"], name: "index_folders_on_category_id_and_name", unique: true
t.index ["category_id", "slug"], name: "index_folders_on_category_id_and_slug", unique: true
Expand Down

0 comments on commit 2a3ffe8

Please sign in to comment.