Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved the GUI and system checks that handle uploading video data #319

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions app/assets/stylesheets/bg_process.scss
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
.bg-processes {
position: fixed;
right: 0;
top: 0;
z-index: 4;
}

.bg-process {
--bs-toast-bg: rgba(var(--bs-body-bg-rgb), 0.85);
--bs-toast-border-color: var(--bs-border-color-translucent);
Expand All @@ -31,7 +24,6 @@
margin: 2em;
max-width: 100%;
pointer-events: auto;
width: var(--bs-toast-max-width);

.header {
display: flex;
Expand Down
2 changes: 1 addition & 1 deletion app/components/movie_title_table_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class MovieTitleTableComponent < ViewComponent::Base
extend Dry::Initializer

option :disks, Types::Array.of(Types.Instance(Disk))
option :disks, Types::Coercible::Array.of(Types.Instance(Disk))
option :movie, Types.Instance(Movie)

def dom_id
Expand Down
17 changes: 14 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
class ApplicationController < ActionController::Base
include Rescuer

before_action :plex_config
before_action :movie_db_config
before_action :mkv_config
before_action :continue_upload
before_action :load_disk_worker
before_action :mkv_config
before_action :movie_db_config
before_action :plex_config
helper_method :free_disk_space, :total_disk_space

def current_user
Expand All @@ -27,6 +28,16 @@ def load_disk_worker
LoadDiskWorker.perform_async
end

def continue_upload
return if UploadWorker.job.pending?

Movie.find_each do |movie|
next unless movie.tmp_plex_path_exists?

UploadWorker.perform_async(disk_title: movie.disk_title)
end
end

private

def stats
Expand Down
18 changes: 12 additions & 6 deletions app/listeners/upload_progress_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ def update_component
cable_ready.broadcast
end

def component
ProgressBarComponent.new \
model: DiskTitle,
completed: percentage,
status: :info,
message: title
def component # rubocop:disable Metrics/MethodLength
progress_bar = render(
ProgressBarComponent.new(
model: Video,
completed: percentage,
status: :info,
message: title
), layout: false
)
component = ProcessComponent.new(worker: UploadWorker)
component.with_body { progress_bar }
component
end

def percentage
Expand Down
12 changes: 8 additions & 4 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ def plex_path
@plex_path ||= Pathname.new("#{Config::Plex.newest.settings_movie_path}/#{plex_named_path}")
end

def tmp_plex_path
@tmp_plex_path ||= Rails.root.join("tmp/movies/#{plex_named_path}")
def tmp_plex_dir
@tmp_plex_dir ||= Rails.root.join("tmp/movies/#{plex_name}")
end

def plex_named_path
"#{plex_name}/#{plex_name}.mkv"
def tmp_plex_path
@tmp_plex_path ||= "#{tmp_plex_dir}/#{plex_name}.mkv"
end

def plex_name
Expand All @@ -66,4 +66,8 @@ def update_maxlength(max)

config.maxlength = nil
end

def tmp_plex_path_exists?
File.exist?(tmp_plex_path)
end
end
4 changes: 0 additions & 4 deletions app/models/video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,4 @@ def ratings
def release_or_air_date
release_date || episode_first_air_date
end

def tmp_plex_path_exists?
File.exist?(tmp_plex_path)
end
end
39 changes: 31 additions & 8 deletions app/services/find_existing_disks_service.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
# frozen_string_literal: true

class FindExistingDisksService
MOUNT_LINE = %r{\A(?<disk_name>\S+)\son\s(?:/Volumes/|)(?<name>\S+)}
MOUNT_LINE = %r{\A(?<disk_name>\S+)\son\s(?:/Volumes/|)(?<name>.*)\s[(]}
Device = Struct.new(:name, :disk_name) do
def rdisk_name
disk_name.gsub('/dev/', '/dev/r')
end
end

class << self
delegate :call, to: :new
end

# example line:
# /dev/disk4 on /Volumes/PLANET51 (udf, local, nodev, nosuid, read-only, noowners)
def call
mounts = `mount`
disks = []
mounts.each_line do |line|
def call # rubocop:disable Metrics/MethodLength
index = 0
devices.reduce(Disk.all) do |disks, device|
if index.zero?
disks.where(
name: device.name,
disk_name: [device.disk_name, device.rdisk_name]
)
else
disks.or(
Disk.where(
name: device.name,
disk_name: [device.disk_name, device.rdisk_name]
)
)
end.tap { index += 1 }
end
end

private

def devices
@devices ||= `mount`.each_line.filter_map do |line|
next unless line.start_with?('/dev/')

match = line.match(MOUNT_LINE)
rdisk_name = match[:disk_name].gsub('/dev/', '/dev/r')
disks << Disk.find_by(name: match[:name], disk_name: [match[:disk_name], rdisk_name])
Device.new(match[:name], match[:disk_name])
end
disks.compact
end
end
10 changes: 9 additions & 1 deletion app/tool_box/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
module Shell
class Error < StandardError; end

Standard = Struct.new(:stdout_str, :stderr_str, :status, keyword_init: true)
Standard = Struct.new(:stdout_str, :stderr_str, :status, keyword_init: true) do
include MkvParser

delegate :success?, to: :status

def parsed_mkv
@parsed_mkv ||= parse_mkv_string(stdout_str)
end
end

def capture3(*cmd)
Rails.logger.debug { "command: #{cmd.join(', ')}" }
Expand Down
52 changes: 52 additions & 0 deletions app/views/layouts/_bg_progress.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<div class="bg-processes">
<%= render ProcessComponent.new worker: ScanPlexWorker do |c| %>
<%= c.with_body do %>
<% if ScanPlexWorker.job.pending? %>
<%=
render(
ProgressBarComponent.new(
model: Movie,
completed: (ScanPlexWorker.job.worker.completed.zero? ? 20 : ScanPlexWorker.job.worker.completed),
status: :success,
message: 'scanning plex for movies',
show_percentage: false
)
)
%>
<% else %>
<span>Done! you have a total of <%= pluralize(Video.count, 'video') %> on plex.</span>
<% end %>
<% end %>
<% end %>

<%= render ProcessComponent.new worker: LoadDiskWorker do |c| %>
<%= c.with_body do %>
<% if LoadDiskWorker.job.pending? %>
<span>Loading the disk info</span>
<% elsif (disks = FindExistingDisksService.call).any? %>
<span><%= disks.map(&:name).join(', ') %> is ready to be ripped.</span>
<% else %>
<span>No disks found</span>
<% end %>
<% end %>
<% end %>
<%= render ProcessComponent.new worker: UploadWorker do |c| %>
<%= c.with_body do %>
<% if UploadWorker.job.pending? %>

<%=
render(
ProgressBarComponent.new(
model: DiskTitle,
show_percentage: false,
status: :info,
message: 'upload in progress'
)
)
%>
<% else %>
<span>Nothing is being uploaded to <%= Config::Plex.newest.settings_ftp_host %> </span>
<% end %>
<% end %>
<% end %>
</div>
48 changes: 10 additions & 38 deletions app/views/layouts/application.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,21 @@
<%= render 'layouts/head' %>
</head>
<body>
<div class="bg-processes">
<%= render ProcessComponent.new worker: ScanPlexWorker do |c| %>
<%= c.with_body do %>
<% if ScanPlexWorker.job.pending? %>
<%=
render(
ProgressBarComponent.new(
model: Movie,
completed: (ScanPlexWorker.job.worker.completed.zero? ? 20 : ScanPlexWorker.job.worker.completed),
status: :success,
message: 'scanning plex for movies',
show_percentage: false
)
)
%>
<% else %>
<span>Done! you have a total of <%= pluralize(Video.count, 'video') %> on plex.</span>
<% end %>
<% end %>
<% end %>

<%= render ProcessComponent.new worker: LoadDiskWorker do |c| %>
<%= c.with_body do %>
<% if LoadDiskWorker.job.pending? %>
<span>Loading the disk info</span>
<% elsif (disks = FindExistingDisksService.call).any? %>
<span><%= disks.map(&:name).join(', ') %> is ready to be ripped.</span>
<% else %>
<span>No disks found</span>
<% end %>
<% end %>
<% end %>
</div>

<div class="d-flex w-100 h-90 mx-auto flex-column">
<%= render ToastComponent.new do |c| %>
<%= c.body do %>
testing
<% end %>
<% end %>
<%= render 'layouts/header' %>

<main role="main" class="inner p-3 h-100"><%= yield %></main>
<main role="main" class="inner p-3 h-100">
<div class="col-12 row">
<div class="col-4">
<%= render 'layouts/bg_progress' %>
</div>
<div class="col-8">
<%= yield %>
</div>
</div>
</main>
</div>
</body>
</html>
11 changes: 1 addition & 10 deletions app/workers/load_disk_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@

class LoadDiskWorker < ApplicationWorker
def perform
cable_ready[DiskTitleChannel.channel_name].morph \
selector: "##{component.dom_id}",
html: render(component, layout: false)
cable_ready[DiskTitleChannel.channel_name].reload if existing_disks.nil?
cable_ready[DiskTitleChannel.channel_name].reload if existing_disks.nil? && disks.present?
cable_ready.broadcast
end

def component
component = ProcessComponent.new(worker: ScanPlexWorker)
component.with_body { disks.map(&:name).join(', ') }
component
end

def disks
@disks ||= existing_disks || CreateDisksService.call
end
Expand Down
5 changes: 2 additions & 3 deletions app/workers/rip_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ def create_mkv(disk_title)
end

def upload_mkv(disk_title)
@progress_listener = UploadProgressListener.new(file_size: disk_title.size)
Ftp::UploadMkvService.call disk_title:,
progress_listener:
sleep 1 while UploadWorker.job.pending?
UploadWorker.perform_async(disk_title)
end

def disk_titles
Expand Down
14 changes: 14 additions & 0 deletions app/workers/upload_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class UploadWorker < ApplicationWorker
option :disk_title, Types.Instance(DiskTitle)

def perform
progress_listener = UploadProgressListener.new(
title: "Uploading #{disk_title.video.title}",
file_size: disk_title.size
)
Ftp::UploadMkvService.call disk_title:,
progress_listener:
end
end
Loading