Skip to content

Commit

Permalink
Improved the GUI and system checks that handle uploading video data
Browse files Browse the repository at this point in the history
commit-id:00ffc232
  • Loading branch information
brand-it committed Apr 22, 2024
1 parent d316ad5 commit 45f8a68
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 77 deletions.
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
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?

Video.find_each do |video|
next unless video.tmp_plex_path_exists?

UploadWorker.perform_async(disk_title: video.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
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

0 comments on commit 45f8a68

Please sign in to comment.