diff --git a/app/controllers/task_download_controller.rb b/app/controllers/task_download_controller.rb new file mode 100644 index 00000000..2140d5f4 --- /dev/null +++ b/app/controllers/task_download_controller.rb @@ -0,0 +1,23 @@ +class TaskDownloadController < ApplicationController + include ActionController::Live + + before_action :require_admin + + def create + task_id = params[:task_id] + task = Task.find task_id + + response.headers['Content-Disposition'] = "attachment; filename=\"homework.tar.gz\"" + + TarGzipWriter.wrap(response.stream) do |tar| + task.solutions.each do |solution| + filename = "#{solution.user.id}-#{solution.user.faculty_number}.#{Language.extension}" + code = solution.code + + tar.add_file_simple(filename, 0644, code.bytes.size) { |io| io.write(code) } + end + end + ensure + response.stream.close + end +end diff --git a/app/services/tar_gzip_writer.rb b/app/services/tar_gzip_writer.rb new file mode 100644 index 00000000..e329acf8 --- /dev/null +++ b/app/services/tar_gzip_writer.rb @@ -0,0 +1,11 @@ +require 'rubygems/package' + +module TarGzipWriter + def self.wrap(io) + Zlib::GzipWriter.wrap(io) do |gzip| + Gem::Package::TarWriter.new(gzip) do |tar| + yield tar + end + end + end +end diff --git a/app/views/solutions/index.html.haml b/app/views/solutions/index.html.haml index 30057ff1..27044d81 100644 --- a/app/views/solutions/index.html.haml +++ b/app/views/solutions/index.html.haml @@ -13,6 +13,7 @@ за повече информация. - else = link_to 'Пусни тестовете', task_check_path(@task), method: :create, class: :action + = link_to 'Свали като архив', task_download_path(@task), method: :post, class: :action %p %strong= @solutions.count diff --git a/config/routes.rb b/config/routes.rb index 1366c7d3..002e45a1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,6 +21,7 @@ end resource :my_solution, only: %w(show update) resource :check, controller: :task_checks, only: :create + resource :download, controller: :task_download, only: :create end resources :challenges, except: :destroy do