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

Returning created attachment in the end of a file upload

parent ca84d088
No related branches found
No related tags found
No related merge requests found
...@@ -13,14 +13,11 @@ class V1::LearningObjects::ChunksController < ApplicationController ...@@ -13,14 +13,11 @@ class V1::LearningObjects::ChunksController < ApplicationController
if last_chunk? if last_chunk?
combine_file! combine_file!
post_file! attachment = post_file!
render attachment, status: 200
# TODO render new attachment ID else
render status: 200 render status: 200
return
end end
render status: 200
rescue rescue
render status: 500 render status: 500
end end
...@@ -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!
......
...@@ -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 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
...@@ -12,13 +14,24 @@ class DspaceUploadWorker ...@@ -12,13 +14,24 @@ class DspaceUploadWorker
# 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