Skip to content
Snippets Groups Projects
Commit 9cd70996 authored by Israel Barreto Sant'Anna's avatar Israel Barreto Sant'Anna
Browse files

Merge branch 'master' of gitlab.c3sl.ufpr.br:portalmec/portalmec

parents efaf005b 7b17c7e6
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ class V1::LearningObjects::AttachmentController < ApplicationController ...@@ -6,7 +6,7 @@ class V1::LearningObjects::AttachmentController < ApplicationController
def destroy def destroy
return render status: :not_found if @learning_object.nil? || @attachment.nil? return render status: :not_found if @learning_object.nil? || @attachment.nil?
destroy_attachment_in_dspace(@attachment.id_dspace) DeleteBitstreamWorker.perform_async(@attachment.id)
@attachment.destroy @attachment.destroy
render status: :ok render status: :ok
...@@ -28,9 +28,4 @@ class V1::LearningObjects::AttachmentController < ApplicationController ...@@ -28,9 +28,4 @@ class V1::LearningObjects::AttachmentController < ApplicationController
authorize(@learning_object, :destroy?) authorize(@learning_object, :destroy?)
end end
def destroy_attachment_in_dspace(id)
client = DspaceService.create_client
client.bitstreams.delete(id: id)
end
end end
...@@ -13,16 +13,13 @@ class V1::LearningObjects::ChunksController < ApplicationController ...@@ -13,16 +13,13 @@ class V1::LearningObjects::ChunksController < ApplicationController
if last_chunk? if last_chunk?
combine_file! combine_file!
post_file! attachment = post_file!
render json: attachment, status: :ok
# TODO render new attachment ID else
render status: 200 render status: :ok
return
end end
render status: 200
rescue rescue
render status: 500 render status: :internal_server_error
end end
private private
...@@ -34,17 +31,7 @@ class V1::LearningObjects::ChunksController < ApplicationController ...@@ -34,17 +31,7 @@ class V1::LearningObjects::ChunksController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through. # Never trust parameters from the scary internet, only allow the white list through.
def chunks_params def chunks_params
params.permit( params.permit(:id, :file, :_chunkNumber, :_totalChunks, :_chunkFilename, :_chunkIdentifier, :_chunkSize, :_currentChunkSize, :_totalSize)
:id,
:file,
:_chunkNumber,
:_totalChunks,
:_chunkFilename,
:_chunkIdentifier,
:_chunkSize,
:_currentChunkSize,
:_totalSize
)
end end
def post_file! def post_file!
......
...@@ -14,7 +14,7 @@ class V1::LearningObjectsController < ApplicationController ...@@ -14,7 +14,7 @@ class V1::LearningObjectsController < ApplicationController
before_action :set_learning_object, only: [:show, :update, :destroy, :subjecting, :unsubjecting, :add_stages, :remove_stages] before_action :set_learning_object, only: [:show, :update, :destroy, :subjecting, :unsubjecting, :add_stages, :remove_stages]
before_action :set_new_learning_object, only: :index before_action :set_new_learning_object, only: :index
before_action :authorize!, except: [:create, :tagging, :untagging, :download] before_action :authorize!, except: [:create, :tagging, :untagging, :download]
before_action :set_paper_trail_whodunnit before_action :set_paper_trail_whodunnit, except: [:index, :show]
def index def index
learning_objects = paginate LearningObject.includes(:tags, :publisher, :language, :license, :subjects, :educational_stages, :reviews) learning_objects = paginate LearningObject.includes(:tags, :publisher, :language, :license, :subjects, :educational_stages, :reviews)
......
...@@ -20,7 +20,9 @@ class LearningObjectPublisher ...@@ -20,7 +20,9 @@ class LearningObjectPublisher
# post *media_path* to *learning_object* on dspace # post *media_path* to *learning_object* on dspace
def upload(learning_object, media_path) def upload(learning_object, media_path)
DspaceUploadWorker.perform_async learning_object.id, media_path, learning_object.description attachment = learning_object.attachments.create(name: File.basename(media_path))
DspaceUploadWorker.perform_async(learning_object.id, media_path, attachment.id, learning_object.description)
attachment
end end
## publish *learning_object* ## publish *learning_object*
...@@ -29,7 +31,7 @@ class LearningObjectPublisher ...@@ -29,7 +31,7 @@ class LearningObjectPublisher
return true if learning_object.published? return true if learning_object.published?
learning_object.state = LearningObject.states[:published] learning_object.state = LearningObject.states[:published]
learning_object.published_at = Time.now learning_object.published_at = Time.current
ThumbnailGenerateWorker.perform_async learning_object.id, learning_object.link if learning_object.link? ThumbnailGenerateWorker.perform_async learning_object.id, learning_object.link if learning_object.link?
learning_object.save learning_object.save
end end
......
require_dependency 'dspace'
class DeleteBitstreamWorker
include Sidekiq::Worker
def perform(attachment_id = nil)
attachment = LearningObject::Attachment.find(attachment_id)
return false if attachment.blank? || !wait_bitstream(attachment)
# Require dspace gem
Bundler.require(*Rails.groups)
DspaceService.create_client.bitstreams.delete(id: attachment.id_dspace)
end
private
def wait_bitstream(attachment)
if attachment.id_dspace.blank?
Timeout.timeout(60) do
sleep(1.0) while attachment.id_dspace.blank?
end
end
true
rescue Timeout::Error
false
end
end
require_dependency 'dspace'
class DspaceUploadWorker class DspaceUploadWorker
include Sidekiq::Worker include Sidekiq::Worker
@@dspace = nil @@dspace = nil
def perform(learning_object_id, media_path, description = nil) def perform(learning_object_id, media_path, attachment_id = nil, description = nil)
# find learning object # find learning object
learning_object = LearningObject.find learning_object_id learning_object = LearningObject.find learning_object_id
return false if learning_object.blank? return false if learning_object.blank?
# Require dspace gem
Bundler.require(*Rails.groups)
# create attachment # create attachment
file = File.new(media_path, 'r') file = File.new(media_path, 'r')
bitstream = dspace.items.add_bitstream(file, id: learning_object.id_dspace, name: File.basename(media_path), description: description) bitstream = dspace.items.add_bitstream(file, id: learning_object.id_dspace, name: File.basename(media_path), description: description)
attachment = learning_object.attachments.create map_bitstream2attachment(bitstream) attachment = create_attachment(learning_object, bitstream, attachment_id)
ThumbnailGenerateWorker.perform_async attachment.id, media_path ThumbnailGenerateWorker.perform_async attachment.id, media_path
end end
private private
def create_attachment(learning_object, bitstream, attachment_id = nil)
unless attachment_id.nil?
attachment = learning_object.attachments.find(attachment_id)
unless attachment.nil?
attachment.update(map_bitstream2attachment(bitstream))
return attachment
end
end
learning_object.attachments.create map_bitstream2attachment(bitstream)
end
def publisher def publisher
@publisher ||= LearningObjectPublisher.new(dspace) @publisher ||= LearningObjectPublisher.new(dspace)
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