diff --git a/app/assets/javascripts/learning_object/chunks.coffee b/app/assets/javascripts/learning_object/chunks.coffee new file mode 100644 index 0000000000000000000000000000000000000000..24f83d18bbd38c24c4f7c3c2fc360cd68e857a2a --- /dev/null +++ b/app/assets/javascripts/learning_object/chunks.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/controllers/learning_object/chunks_controller.rb b/app/controllers/learning_object/chunks_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..4a7e2308a838dd553fb79de5502c1abd77449bfa --- /dev/null +++ b/app/controllers/learning_object/chunks_controller.rb @@ -0,0 +1,66 @@ +class LearningObject::ChunksController < ApplicationController + layout nil + + #GET /chunk + def show + #chunk folder path based on the parameters + dir = "/tmp/#{params[:resumableIdentifier]}" + #chunk path based on the parameters + chunk = "#{dir}/#{params[:resumableFilename]}.part#{params[:resumableChunkNumber]}" + + if File.exists?(chunk) + #Let resumable.js know this chunk already exists + render :nothing => true, :status => 200 + else + #Let resumable.js know this chunk doesnt exists and needs to be uploaded + render :nothing => true, :status => 404 + end + + end + + #POST /chunk + def create + + #chunk folder path based on the parameters + dir = "/tmp/#{params[:resumableIdentifier]}" + #chunk path based on the parameters + chunk = "#{dir}/#{params[:resumableFilename]}.part#{params[:resumableChunkNumber]}" + + #Create chunks directory when not present on system + if !File.directory?(dir) + FileUtils.mkdir(dir, :mode => 0700) + end + + #Move the uploaded chunk to the directory + FileUtils.mv params[:file].tempfile, chunk + + #Concatenate all the partial files into the original file + + currentSize = params[:resumableChunkNumber].to_i * params[:resumableChunkSize].to_i + filesize = params[:resumableTotalSize].to_i + + #When all chunks are uploaded + if (currentSize + params[:resumableCurrentChunkSize].to_i) >= filesize + + #Create a target file + File.open("#{dir}/#{params[:resumableFilename]}", "a") do |target| + #Loop trough the chunks + for i in 1..params[:resumableChunkNumber].to_i + #Select the chunk + chunk = File.open("#{dir}/#{params[:resumableFilename]}.part#{i}", 'r').read + + #Write chunk into target file + chunk.each_line do |line| + target << line + end + + #Deleting chunk + FileUtils.rm "#{dir}/#{params[:resumableFilename]}.part#{i}", :force => true + end + puts "File saved to #{dir}/#{params[:resumableFilename]}" + end + end + + render :nothing => true, :status => 200 + end +end diff --git a/config/routes.rb b/config/routes.rb index 67e975cbfc96f6fbad8ed47bcbd70c6843852bb7..f9d11ca1189474798f306ad25d27a31f9d646d91 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -66,6 +66,8 @@ Rails.application.routes.draw do post :like post :bookmarks get :collections + + resource :chunk, only: [:create, :show] end end diff --git a/test/controllers/learning_object/chunks_controller_test.rb b/test/controllers/learning_object/chunks_controller_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..8ec754064fe875f7bbec6acf486f813b125f2073 --- /dev/null +++ b/test/controllers/learning_object/chunks_controller_test.rb @@ -0,0 +1,14 @@ +require 'test_helper' + +class LearningObject::ChunksControllerTest < ActionController::TestCase + test "should get show" do + get :show + assert_response :success + end + + test "should get create" do + get :create + assert_response :success + end + +end