Skip to content
Snippets Groups Projects
chunks_controller.rb 1.3 KiB
Newer Older
class V1::LearningObjects::ChunksController < ApplicationController
  before_action :authorize!
  before_action :chunk_service

  # GET /learning_objects/:learning_object_id/chunk
  def show
    if @chunk.exist?
      post_file
      render nothing: true, status: :ok
    else
      render nothing: true, status: :not_found # chunk doesnt exists and needs to be uploaded
    end
  end

  # POST /learning_objects/:learning_object_id/chunk
  def create
    @chunk.save

    post_file

    render nothing: true, status: :ok
  end

  private

  def chunk_service
    @chunk = ChunksService.new(chunks_params)
    return render nothing: true, status: :unsupported_media_type if @chunk.nil?
  end

  def post_file
    return false unless @chunk.last?

    publisher = LearningObjectPublisher.new(DspaceService.create_client)
    publisher.post @chunk.learning_object, @chunk.resumable_filename
  end

  def authorize!
    learning_object = LearningObject.find chunks_params[:learning_object_id]
    authorize(learning_object || LearningObject.new, :update?)
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def chunks_params
    params.permit(:file, :learning_object_id, :resumableIdentifier, :resumableFilename, :resumableChunkNumber, :resumableTotalChunks, :resumableChunkSize)
  end