Select Git revision
uploads_controller.rb
Forked from
PortalMEC / portalmec
1291 commits behind the upstream repository.
-
Mateus Rambo Strey authoredMateus Rambo Strey authored
uploads_controller.rb 1.15 KiB
class V1::LearningObjects::UploadsController < ApplicationController
before_action :set_learning_object
before_action :authorize!
# POST /learning_objects/:learning_object_id/upload
def create
return render status: :bad_request unless valid_file?
publisher = LearningObjectPublisher.new(DspaceService.create_client)
publisher.upload @learning_object, saved_file
render status: :ok
end
private
# Use callbacks to share common setup or constraints between actions.
def set_learning_object
@learning_object = LearningObject.find(params[:id])
end
def authorize!
authorize(@learning_object || LearningObject.new, :update?)
end
def valid_file?
upload_params[:file].is_a? ActionDispatch::Http::UploadedFile
end
def saved_file
dir = "/tmp/#{@learning_object.id}"
FileUtils.mkdir(dir, mode: 0700) unless File.directory?(dir)
path = "#{dir}/#{upload_params[:file].original_filename}"
FileUtils.mv upload_params[:file].tempfile.path, path
path
end
# Never trust parameters from the scary internet, only allow the white list through.
def upload_params
params.permit(:id, :file)
end
end