Skip to content
Snippets Groups Projects
Commit c6366231 authored by Mauricio Giacomini Girardello's avatar Mauricio Giacomini Girardello
Browse files

raise exceptions when attachment already exists

parent 7f86dcb6
No related branches found
No related tags found
No related merge requests found
......@@ -59,11 +59,16 @@ class ChunksController < ApplicationController
puts "File saved to #{resumable_filename}"
end
publisher = LearningObjectPublisher.new(DspaceService.create_client)
publisher.post @learning_object, resumable_filename
begin
status = 200
publisher = LearningObjectPublisher.new(DspaceService.create_client)
publisher.post @learning_object, resumable_filename
rescue LearningObject::DuplicateAttachmentError => e
status = 406
end
end
render :nothing => true, :status => 200
render :nothing => true, :status => status
end
private
......
module LearningObject::DuplicateAttachmentChecker
def self.check_duplicates(learning_object, media_path)
file_size = File.size media_path
file_name = File.basename media_path
learning_object.attachments.each do |learning_object|
if attachment.size == file_size && attachment.name == file_name
return true
end
end
end
def self.check_duplicates!(learning_object, media_path)
if check_duplicates learning_object, media_path
raise DuplicateAttachmentError, 'The attachment already exists!'
end
end
end
class LearningObject::DuplicateAttachmentError < StandardError; end
\ No newline at end of file
......@@ -13,7 +13,10 @@ class LearningObjectPublisher
learning_object_repository.create draft
end
#post *media_path* to *learning_object* on dspace
#raise LearningObject::DuplicateAttachmentError if *media_path* already exists
def post(learning_object, media_path)
check_duplicate_attachments! learning_object, media_path
DspaceUploadWorker.perform_async learning_object.id, learning_object.id_dspace, media_path
end
......@@ -25,6 +28,10 @@ class LearningObjectPublisher
private
def check_duplicate_attachments!(learning_object, media_path)
LearningObject::DuplicateAttachmentChecker.check_duplicates! learning_object, media_path
end
# change *learning_object* status to active
def activate!(learning_object)
learning_object.status = 'active'
......
......@@ -144,6 +144,10 @@
$('[data-uniqueId=' + file.uniqueIdentifier + ']').find('.progress-bar').addClass('progress-bar-success');
});
r.on('fileError', function (file, message) {
$('[data-uniqueId=' + file.uniqueIdentifier + ']').find('.progress-bar').addClass('progress-bar-danger');
$('[data-uniqueId=' + file.uniqueIdentifier + ']').find('.progress-bar').html('&nbsp;' + messages);
});
r.on('uploadStart', function () {
$('#upload_message').text('Enviando, aguarde...');
......
......@@ -4,12 +4,12 @@ class DspaceUploadWorker
@@dspace= nil
def perform(learning_object_id, item_id, media_path)
def perform(learning_object_id, item_id, media_path, *options)
# create file instance
file = File.new(media_path, 'r')
#create bitstream in dspace
bitstream = dspace.items.add_bitstream(file, id: item_id, name: File.basename(media_path), description: 'beta upload')
bitstream = dspace.items.add_bitstream(file, id: item_id, name: File.basename(media_path), description: get_description(options))
#find learning object
learning_object = learning_object_repository.find learning_object_id
......@@ -35,4 +35,12 @@ class DspaceUploadWorker
@@dspace ||= DspaceService.create_client
end
def get_description(*options)
description = nil
if options.has_key? :description
description = options[:description]
end
description
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment