Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/controllers/task_download_controller.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions app/services/tar_gzip_writer.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/views/solutions/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down