Sha256: f6abaa27464bdf8a65a21feee673b253178a51a090ee4ad793b98801ef618806

Contents?: true

Size: 1.97 KB

Versions: 65

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require 'active_support/json'
require 'eac_ruby_utils/fs/temp'
require 'ehbrs_ruby_utils/executables'

module EhbrsRubyUtils
  module WebUtils
    module Videos
      class FilesList
        enable_speaker
        enable_simple_cache

        common_constructor :type_class, :root_path, :options do
          self.root_path = root_path.to_pathname
          raise "\"#{root_path}\" is not a directory" unless root_path.directory?

          self.options = options.with_indifferent_access.freeze
        end

        def write_to(path = nil)
          path ||= ::EacRubyUtils::Fs::Temp.file.to_path
          ::File.write(path, data.to_json)
          path
        end

        def data
          {
            root_path: root_path.to_path,
            type: type_class,
            files: files_data
          }
        end

        def files_data
          case type_class
          when 'Videos::MovieFile' then movies_files_data
          when 'Videos::SeriesDirectory' then series_files_data
          else raise "Unknown type class: \"#{type_class}\""
          end
        end

        def movies_files_data
          r = []
          Dir["#{root_path}/**/*"].each do |path|
            r << file_data(path) if ::File.file?(path)
          end
          r
        end

        def series_files_data
          r = []
          Dir["#{root_path}/*"].each do |path|
            r << file_data(path) if ::File.directory?(path)
          end
          r
        end

        def file_data(path)
          r = { original_path: path }
          r[:ffprobe_data] = file_ffprobe_data(path) if ::File.file?(path)
          r
        end

        def file_ffprobe_data(path)
          return {} unless options.fetch(:ffprobe)

          infom "Probing \"#{path}\"..."
          ::JSON.parse(::EhbrsRubyUtils::Executables.ffprobe.command(
            '-hide_banner', '-print_format', 'json', '-show_format', '-show_streams', path
          ).execute!)
        end
      end
    end
  end
end

Version data entries

65 entries across 65 versions & 2 rubygems

Version Path
ehbrs_ruby_utils-0.44.3 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.44.2 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.44.1 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.44.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.41.1 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.41.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.40.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.39.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.38.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.37.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.36.1 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.36.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.35.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.33.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.32.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.31.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.30.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.29.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.28.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.27.1 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb