Skip to content
Snippets Groups Projects
Commit 1e6f4d04 authored by Mateus Rambo Strey's avatar Mateus Rambo Strey
Browse files

add worker to get mime type from dspace

parent 09f8954d
No related branches found
No related tags found
No related merge requests found
...@@ -47,8 +47,11 @@ class LearningObject < ActiveRecord::Base ...@@ -47,8 +47,11 @@ class LearningObject < ActiveRecord::Base
def default_attachment def default_attachment
return attachment unless attachment.nil? return attachment unless attachment.nil?
unless attachments.blank? unless attachments.blank?
update(attachment: attachments.first) at = attachments.where(bundle_name: 'ORIGINAL').first
at = attachments.first if at.nil?
update(attachment: at)
return attachment return attachment
end end
nil nil
......
class LearningObject::Attachment < ActiveRecord::Base class LearningObject::Attachment < ActiveRecord::Base
belongs_to :learning_object belongs_to :learning_object
has_attached_file :thumbnail, styles: {medium: "530x300", small: "250x140"} scope :unknown_mime_type, ->() { where(format: 'Unknown') }
validates_attachment_content_type :thumbnail, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
has_attached_file :thumbnail, styles: { medium: '530x300', small: '250x140' }
validates_attachment_content_type :thumbnail, content_type: ['image/jpg', 'image/jpeg', 'image/png', 'image/gif']
end end
class BitstreamMimeWorker
include Sidekiq::Worker
def perform(id)
attachment = LearningObject::Attachment.find(id)
client = DspaceService.create_client
begin
file = client.bitstreams.retrieve(id: attachment.id_dspace).open
mime = MimeMagic.by_magic(file)
attachment.update(format: mime.mediatype, mime_type: mime.type) unless mime.blank?
ensure
file.close unless file.nil?
end
end
end
namespace :attachments do
desc 'Set mime type where is unknown'
task set_unknown: :environment do
LearningObject::Attachment.unknown_mime_type.find_each { |a| BitstreamMimeWorker.perform_async(a.id) }
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