Sha256: 57055a43cdcd50e434cbb183ec2e6e1ac922f54ddcf0a22ee638b45e96dedce0

Contents?: true

Size: 1.98 KB

Versions: 15

Compression:

Stored size: 1.98 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_console_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

15 entries across 15 versions & 2 rubygems

Version Path
ehbrs-tools-0.20.0 vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs-tools-0.19.0 vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs-tools-0.18.0 vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs-tools-0.17.0 vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.5.1 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs-tools-0.16.5 vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs-tools-0.16.4 vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs-tools-0.16.3 vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.5.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs-tools-0.16.2 vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.4.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs-tools-0.16.1 vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs-tools-0.16.0 vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs_ruby_utils-0.3.0 lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb
ehbrs-tools-0.15.0 vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/web_utils/videos/files_list.rb