From 3c9044fbd01dccde6b83c76b81e0f3d729e387cc Mon Sep 17 00:00:00 2001
From: Bruno Nocera Zanette <bnzanette@inf.ufpr.br>
Date: Tue, 20 Oct 2015 23:58:07 -0200
Subject: [PATCH] Refactor ThumbnailGeneratorWorker to only use Lib

---
 app/workers/thumbnail_generator_worker.rb |  93 +------------
 lib/thumbnail/creation.rb                 |  75 ----------
 lib/thumbnail/generate.rb                 | 159 ++++++++++++++++++++++
 3 files changed, 161 insertions(+), 166 deletions(-)
 delete mode 100644 lib/thumbnail/creation.rb
 create mode 100644 lib/thumbnail/generate.rb

diff --git a/app/workers/thumbnail_generator_worker.rb b/app/workers/thumbnail_generator_worker.rb
index 9718d52f3..4fe90fee6 100644
--- a/app/workers/thumbnail_generator_worker.rb
+++ b/app/workers/thumbnail_generator_worker.rb
@@ -1,99 +1,10 @@
 class ThumbnailGeneratorWorker
 
   include Sidekiq::Worker
-  include RepositoriesProxy
-  include Thumbnail::Creation
-  include Thumbnail::Formats
+  include Thumbnail::Generate
 
   def perform(learning_object_id)
-
-    file = get_accepted_file learning_object_id
-    unless file.nil?
-      item.thumbnail = default_thumbnail
-    else
-      item.thumbnail = generate_thumbnail(file)
-      delete_file(file)
-    end
-
-    learning_object_repository.update_property(item,"thumbnail",item.thumbnail)
-
-  end
-
-  private
-
-  def get_accepted_file learning_object_id
-    item = learning_object_repository.find learning_object_id
-    filename = item.get_filename
-    file_format = get_file_format filename
-
-    unless accepted_formats.include? file_format or archive_formats.include? file_format
-      return nil
-    else
-      begin
-        retrieve_link = item.get_retrievelink
-        file = download_bitstream(retrieve_link,filename)
-      rescue
-        puts "ERROR!!! Some error occurred during file download."
-      end
-    end
-
-    if archive_formats.include? file_format
-      return extract_accepted_file file
-    else
-      return file
-    end
-  end
-
-  def delete_file(file)
-    File.unlink(file) if File.exist?(file)
-  end
-
-  def get_tmpfile_path filename
-    output_dir = "/tmp"
-    output_file = "#{output_dir}/#{filename}"
-  end
-
-  def extract_accepted_file(filename)
-    extracted_file = nil
-
-    # Open Archive
-    Archive.read_open_filename(archive_file) do |archive|
-
-      # Iterate through all Archive's files (entry)
-      while entry = archive.next_header
-        if accepted_formats.include? get_file_format entry.pathname
-
-          # Extract Accepted file and break iteration
-          extracted_file = get_tmpfile_path entry.pathname
-          File.open(extracted_file, 'wb') do |f|
-            archive.read_data(1024) do |d|
-             f << d
-            end
-          end
-          break
-
-        end
-      end
-    end
-
-    return extracted_file
-  end
-
-  def download_bitstream(url, filename)
-    downloaded_file = get_tmpfile_path filename
-
-    c = Curl::Easy.new(url)
-    c.ssl_verify_peer = false
-    c.ssl_verify_host = false
-    File.open(output_file, 'wb') do |f|
-      # c.on_progress { |dl_total, dl_now, ul_total, ul_now|
-      #   puts "#{dl_total}, #{dl_now}, #{ul_total}, #{ul_now}"; true
-      # }
-      c.on_body {|data| f << data; data.size }
-      c.perform
-    end
-
-    return downloaded_file
+    thumbnail = generate_thumbnail(learning_object_id)
   end
 
 end
diff --git a/lib/thumbnail/creation.rb b/lib/thumbnail/creation.rb
deleted file mode 100644
index bbcb67667..000000000
--- a/lib/thumbnail/creation.rb
+++ /dev/null
@@ -1,75 +0,0 @@
-module Thumbnail
-  module Creation
-
-    include Formats
-
-    def generate_thumbnail(input)
-
-      size = "530x298"
-      
-      filename = File.basename input
-      file_format = get_file_format filename
-
-      unless accepted_formats.include? file_format
-        return default_thumbnail
-      else
-        thumbnail = thumbnail_path(filename, size)
-        output = "#{root_dir}#{thumbnail}"
-        begin
-          if accepted_video_formats.include? file_format
-            generate_video_thumbnail(input, output, size)
-          else
-            generate_image_thumbnail(input, output, size)
-          end
-        rescue
-          return default_thumbnail
-        else
-          return thumbnail
-        end
-      end
-
-    end
-
-    def default_thumbnail
-      @default_thumbnail ||= nil
-    end
-
-    private
-
-    def generate_video_thumbnail(input,output,size)
-      movie = FFMPEG::Movie.new(input)
-      frame = (movie.duration * 25/100).floor
-      movie.screenshot(output,
-                        { seek_time: frame, resolution: size },
-                        preserve_aspect_ratio: :width)
-    end
-
-    def generate_image_thumbnail(input,output,size)
-      # Read the image and resize it. The `change_geometry' method
-      # computes the new image geometry and yields to a block. The
-      # return value of the block is the return value of the method.
-      img = Magick::Image.read(input)[0]
-      img.alpha(Magick::DeactivateAlphaChannel)
-      img.change_geometry!(size) { |cols, rows| img.thumbnail! cols, rows }
-      img.write(output)
-    end
-
-    def encode_hash_from(object)
-      Digest::SHA1.hexdigest object
-    end
-
-    def thumbnail_path(filename, size)
-      thumbnail_name = encode_hash_from filename
-      thumbnail_path = "#{thumbnails_dir}/#{thumbnail_name}_#{size}.jpg"
-    end
-
-    def thumbnails_dir
-      @thumbnails_dir ||= "/thumbnails"
-    end
-
-    def root_dir
-      @root_dir ||= Rails.root.join('public')
-    end
-
-  end
-end
diff --git a/lib/thumbnail/generate.rb b/lib/thumbnail/generate.rb
new file mode 100644
index 000000000..65944b058
--- /dev/null
+++ b/lib/thumbnail/generate.rb
@@ -0,0 +1,159 @@
+module Thumbnail
+  module Generate
+
+    include Formats
+    include RepositoriesProxy
+
+    def generate_thumbnail(learning_object_id)
+
+      size = "530x298"
+
+      item = learning_object_repository.find learning_object_id
+
+      input = get_accepted_file item
+      filename = get_file_basename input
+      file_format = get_file_extname input
+
+      if file.nil?
+        thumbnail = default_thumbnail
+      else
+        thumbnail = thumbnail_path(filename, size)
+        output = "#{root_dir}#{thumbnail}"
+        begin
+          if accepted_video_formats.include? file_format
+            generate_video_thumbnail(input, output, size)
+          else
+            generate_image_thumbnail(input, output, size)
+          end
+          delete_file(input)
+        rescue
+          puts error
+          thumbnail = default_thumbnail
+        end
+      end
+
+      # learning_object_repository.update_property(item,"thumbnail",thumbnail)
+
+    end
+
+    def default_thumbnail
+      @default_thumbnail ||= nil
+    end
+
+    private
+
+    def generate_video_thumbnail(input,output,size)
+      movie = FFMPEG::Movie.new(input)
+      frame = (movie.duration * 25/100).floor
+      movie.screenshot(output,
+                        { seek_time: frame, resolution: size },
+                        preserve_aspect_ratio: :width)
+    end
+
+    def generate_image_thumbnail(input,output,size)
+      # Read the image and resize it. The `change_geometry' method
+      # computes the new image geometry and yields to a block. The
+      # return value of the block is the return value of the method.
+      img = Magick::Image.read(input)[0]
+      img.alpha(Magick::DeactivateAlphaChannel)
+      img.change_geometry!(size) { |cols, rows| img.thumbnail! cols, rows }
+      img.write(output)
+    end
+
+    def encode_hash_from(object)
+      Digest::SHA1.hexdigest object
+    end
+
+    def tmpfile_path(filename)
+      output_dir = "/tmp"
+      output_file = "#{output_dir}/#{filename}"
+    end
+
+    def thumbnail_path(filename, size)
+      thumbnail_name = encode_hash_from filename
+      thumbnail_path = "#{thumbnails_dir}/#{thumbnail_name}_#{size}.jpg"
+    end
+
+    def thumbnails_dir
+      @thumbnails_dir ||= "/thumbnails"
+    end
+
+    def root_dir
+      @root_dir ||= Rails.root.join('public')
+    end
+
+    def delete_file(file)
+      File.unlink(file) if File.exist?(file)
+    end
+
+    def get_accepted_file(item)
+
+      filename = item.get_filename
+      file_format = get_file_extname filename
+
+      unless accepted_formats.include? file_format or archive_formats.include? file_format
+        return nil
+      else
+        begin
+          retrieve_link = item.get_retrievelink
+          file = download_bitstream(retrieve_link,filename)
+        rescue
+          puts "ERROR!!! Some error occurred during file download."
+          return nil
+        else
+          if archive_formats.include? file_format
+            accepted_file = extract_accepted_file file
+          else
+            accepted_file = file
+          end
+        end
+      end
+
+      return accepted_file
+    end
+
+    def extract_accepted_file(filename)
+      extracted_file = nil
+
+      # Open Archive
+      Archive.read_open_filename(filename) do |archive|
+
+        # Iterate through all Archive's files (entry)
+        while entry = archive.next_header
+          if accepted_formats.include? get_file_extname(entry.pathname)
+
+            # Extract Accepted file and break iteration
+            extracted_file = tmpfile_path get_file_basename(entry.pathname)
+            File.open(extracted_file, 'wb') do |f|
+              archive.read_data(1024) do |d|
+               f << d
+              end
+            end
+            break
+
+          end
+        end
+      end
+
+      return extracted_file
+    end
+
+    def download_bitstream(url, filename)
+      downloaded_file = tmpfile_path filename
+
+      c = Curl::Easy.new(url)
+      c.ssl_verify_peer = false
+      c.ssl_verify_host = false
+      File.open(downloaded_file, 'wb') do |f|
+        # c.on_progress { |dl_total, dl_now, ul_total, ul_now|
+        #   puts "#{dl_total}, #{dl_now}, #{ul_total}, #{ul_now}"; true
+        # }
+        c.on_body {|data| f << data; data.size }
+        c.perform
+      end
+
+      return downloaded_file
+    end
+
+  end
+end
-- 
GitLab