Skip to content
Snippets Groups Projects
thumbnail_generate_worker.rb 2.24 KiB
Newer Older
bfs15's avatar
bfs15 committed

# Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana
#
# This file is part of portalmec.
#
# portalmec is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# portalmec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with portalmec.  If not, see <http://www.gnu.org/licenses/>.

class ThumbnailGenerateWorker
  include Sidekiq::Worker

  def perform(attachment_id, media_path = nil, force = false)
    @attachment = ::LearningObject::Attachment.find(attachment_id)
    return false if @attachment.nil? || (!@attachment.thumbnail.blank? && !force)

    # object from dspace
    if media_path.nil?
      media = bitstream_download(@attachment.id_dspace)
      create_thumbnail(media)
      File.unlink(media) if File.exist?(media.path)
    # new object
    else
      create_thumbnail(create_media(media_path))
      FileUtils.rm_rf(File.dirname(media_path))
  def create_media(path)
    return ::Screencap::Fetcher.new(path) if path =~ URI::regexp
  def create_thumbnail(media)
    thumbnail = thumbnail_service.create_thumbnail(media)
    return false if thumbnail.blank?

    @attachment.update(thumbnail: thumbnail)
    File.unlink(thumbnail) if File.exist?(thumbnail.path)
  end

  def bitstream_download(id)
    client = DspaceService.create_client
    client.bitstreams.retrieve(id: id).open
  end

Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
    @@thumbnailService ||= ThumbnailService.new(generators)
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
  def generators
    [
      # Deactivated thumbnails for videos
      # Thumbnail::Strategies::VideoThumbnailGenerator.new,
      Thumbnail::Strategies::ImageThumbnailGenerator.new,
      Thumbnail::Strategies::PdfThumbnailGenerator.new,
      Thumbnail::Strategies::UrlThumbnailGenerator.new
Matheus Agio Nerone's avatar
Matheus Agio Nerone committed
    ]