From 29d65b4d16881290f700b336224f65bc0396bbe1 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Mon, 10 Jul 2017 09:09:14 -0700 Subject: [PATCH] ensure that zip tmpfile gets deleted on error --- models/site.rb | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/models/site.rb b/models/site.rb index a1626227..a5aa42ff 100644 --- a/models/site.rb +++ b/models/site.rb @@ -780,17 +780,22 @@ class Site < Sequel::Model tmpfile = Tempfile.new 'neocities-site-zip' tmpfile.close - Zip::Archive.open(tmpfile.path, Zip::CREATE) do |ar| - ar.add_dir(zip_name) + begin + Zip::Archive.open(tmpfile.path, Zip::CREATE) do |ar| + ar.add_dir(zip_name) - Dir.glob("#{base_files_path}/**/*").each do |path| - relative_path = path.gsub(base_files_path+'/', '') - if File.directory?(path) - ar.add_dir(zip_name+'/'+relative_path) - else - ar.add_file(zip_name+'/'+relative_path, path) # add_file(, ) + Dir.glob("#{base_files_path}/**/*").each do |path| + relative_path = path.gsub(base_files_path+'/', '') + if File.directory?(path) + ar.add_dir(zip_name+'/'+relative_path) + else + ar.add_file(zip_name+'/'+relative_path, path) # add_file(, ) + end end end + rescue => e + tmpfile.unlink + raise e end tmpfile.path