From 891efc6ce370e6240809a90b12aac2dc43380611 Mon Sep 17 00:00:00 2001 From: jhs-panda Date: Wed, 20 Nov 2024 02:48:47 -0500 Subject: [PATCH] rename duplicate files if they exist (add _ver# at end) --- app/controllers/users_controller.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index adcd6b0c..7f87c538 100755 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -189,6 +189,15 @@ def download_all_submissions temp_file = Tempfile.new("autolab_submissions.zip") Zip::File.open(temp_file.path, Zip::File::CREATE) do |zipfile| + # track counts of each file name + track_duplicate_counts = Hash.new(0) + submissions.each do |s| + entry_name = download_filename(s.handin_file_path, s.assessment.name) + track_duplicate_counts[entry_name] += 1 + end + + # track which version is being processed (for naming purposes) + filename_counts = Hash.new(0) submissions.each do |s| p = s.handin_file_path course_name = s.course_user_datum.course.name @@ -196,6 +205,14 @@ def download_all_submissions course_directory = "#{filename}/#{course_name}" assignment_directory = "#{course_directory}/#{assignment_name}" entry_name = download_filename(p, assignment_name) + filename_counts[entry_name] += 1 + # append version number to submission if more than one of same name + if track_duplicate_counts[entry_name] > 1 + version_suffix = "_ver#{filename_counts[entry_name]}" + extname = File.extname(entry_name) + basename = File.basename(entry_name, extname) + entry_name = "#{basename}#{version_suffix}#{extname}" + end zipfile.add(File.join(assignment_directory, entry_name), p) end end