Merge pull request #75 from openHPI/download-submission-backend
download complete submission as one zip file
This commit is contained in:
1
Gemfile
1
Gemfile
@ -38,6 +38,7 @@ gem 'faye-websocket'
|
||||
gem 'nokogiri'
|
||||
gem 'd3-rails'
|
||||
gem 'rest-client'
|
||||
gem 'rubyzip'
|
||||
|
||||
group :development do
|
||||
gem 'better_errors', platform: :ruby
|
||||
|
@ -397,6 +397,7 @@ DEPENDENCIES
|
||||
rubocop
|
||||
rubocop-rspec
|
||||
rubytree
|
||||
rubyzip
|
||||
sass-rails (~> 4.0.3)
|
||||
sdoc (~> 0.4.0)
|
||||
selenium-webdriver
|
||||
|
@ -179,7 +179,10 @@ $(function() {
|
||||
var downloadCode = function(event) {
|
||||
event.preventDefault();
|
||||
createSubmission(this, null,function(response) {
|
||||
var url = response.download_url.replace(FILENAME_URL_PLACEHOLDER, active_file.filename);
|
||||
var url = response.download_url;
|
||||
|
||||
// to download just a single file, use the following url
|
||||
//var url = response.download_file_url.replace(FILENAME_URL_PLACEHOLDER, active_file.filename);
|
||||
window.location = url;
|
||||
});
|
||||
};
|
||||
|
@ -6,9 +6,9 @@ class SubmissionsController < ApplicationController
|
||||
include SubmissionScoring
|
||||
include Tubesock::Hijack
|
||||
|
||||
before_action :set_submission, only: [:download_file, :render_file, :run, :score, :show, :statistics, :stop, :test]
|
||||
before_action :set_submission, only: [:download, :download_file, :render_file, :run, :score, :show, :statistics, :stop, :test]
|
||||
before_action :set_docker_client, only: [:run, :test]
|
||||
before_action :set_files, only: [:download_file, :render_file, :show]
|
||||
before_action :set_files, only: [:download, :download_file, :render_file, :show]
|
||||
before_action :set_file, only: [:download_file, :render_file]
|
||||
before_action :set_mime_type, only: [:download_file, :render_file]
|
||||
skip_before_action :verify_authenticity_token, only: [:download_file, :render_file]
|
||||
@ -53,6 +53,20 @@ class SubmissionsController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def download
|
||||
# files = @submission.files.map{ }
|
||||
# zipline( files, 'submission.zip')
|
||||
# send_data(@file.content, filename: @file.name_with_extension)
|
||||
require 'zip'
|
||||
stringio = Zip::OutputStream.write_buffer do |zio|
|
||||
@files.each do |file|
|
||||
zio.put_next_entry(file.name_with_extension)
|
||||
zio.write(file.content)
|
||||
end
|
||||
end
|
||||
send_data(stringio.string, filename: @submission.exercise.title.tr(" ", "_") + ".zip")
|
||||
end
|
||||
|
||||
def download_file
|
||||
if @file.native_file?
|
||||
send_file(@file.native_file.path)
|
||||
|
@ -28,13 +28,17 @@ class Submission < ActiveRecord::Base
|
||||
ancestors.merge(descendants).values
|
||||
end
|
||||
|
||||
[:download, :render, :run, :test].each do |action|
|
||||
[:download_file, :render, :run, :test].each do |action|
|
||||
filename = FILENAME_URL_PLACEHOLDER.gsub(/\W/, '')
|
||||
define_method("#{action}_url") do
|
||||
Rails.application.routes.url_helpers.send(:"#{action}_submission_path", self, filename).sub(filename, FILENAME_URL_PLACEHOLDER)
|
||||
end
|
||||
end
|
||||
|
||||
def download_url
|
||||
Rails.application.routes.url_helpers.send(:download_submission_path, self)
|
||||
end
|
||||
|
||||
def main_file
|
||||
collect_files.detect(&:main_file?)
|
||||
end
|
||||
|
@ -8,7 +8,7 @@ class SubmissionPolicy < ApplicationPolicy
|
||||
everyone
|
||||
end
|
||||
|
||||
[:download_file?, :render_file?, :run?, :score?, :show?, :statistics?, :stop?, :test?].each do |action|
|
||||
[:download?, :download_file?, :render_file?, :run?, :score?, :show?, :statistics?, :stop?, :test?].each do |action|
|
||||
define_method(action) { admin? || author? }
|
||||
end
|
||||
|
||||
|
@ -1 +1 @@
|
||||
json.extract! @submission, :download_url, :id, :score_url, :render_url, :run_url, :stop_url, :test_url, :files
|
||||
json.extract! @submission, :download_url, :download_file_url, :id, :score_url, :render_url, :run_url, :stop_url, :test_url, :files
|
||||
|
@ -89,7 +89,8 @@ Rails.application.routes.draw do
|
||||
|
||||
resources :submissions, only: [:create, :index, :show] do
|
||||
member do
|
||||
get 'download/:filename', as: :download, constraints: {filename: FILENAME_REGEXP}, to: :download_file
|
||||
get 'download', as: :download, to: :download
|
||||
get 'download/:filename', as: :download_file, constraints: {filename: FILENAME_REGEXP}, to: :download_file
|
||||
get 'render/:filename', as: :render, constraints: {filename: FILENAME_REGEXP}, to: :render_file
|
||||
get 'run/:filename', as: :run, constraints: {filename: FILENAME_REGEXP}, to: :run
|
||||
get :score
|
||||
|
Reference in New Issue
Block a user