Skip to content

Commit

Permalink
Fixed bug with Not finding disk info in DB & Made the GUI a bit nicer…
Browse files Browse the repository at this point in the history
… when it comes to what is going on
  • Loading branch information
brand-it committed Apr 22, 2024
1 parent 89b104e commit faf544a
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 47 deletions.
70 changes: 38 additions & 32 deletions app/components/movie_title_table_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
<table class="table" id="<%= dom_id %>">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Duration</th>
<th>Size</th>
<th></th>
</tr>
</thead>
<tbody class="disk_titles">
<% disks.each do |disk| %>
<% disk.disk_titles.sort_by { |d| movie.runtime_range.include?(d.duration) ? 0 : 1 }.each do |disk_title| %>
<% text_class = 'text-primary-emphasis' if movie.runtime_range.include?(disk_title.duration) %>

<tr>
<th scope="row" class="<%= text_class %>"><%= disk_title.id %></th>
<td class="<%= text_class %>"><%= disk_title.name %></td>
<td class="<%= text_class %>"><%= distance_of_time_in_words(disk_title.duration.seconds) %></td>
<td class="<%= disk_title.size >= free_disk_space ? 'text-danger' : text_class %>">
<%= number_to_human_size(disk_title.size, precision: 3) %>
<% if disk_title.size >= free_disk_space %>
WARNING: Not enough space available to rip need another <%= number_to_human_size(disk_title.size - free_disk_space, precision: 3) %>
<% end %>
</td>
<td class="<%= text_class %>">
<%= link_to 'Rip', video_disk_title_path(movie, disk_title), data: { turbo_method: :patch } %>
</td>
</tr>
<% if disks.any? %>
<table class="table" id="<%= dom_id %>">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Duration (<%= distance_of_time_in_words @movie.movie_runtime.seconds %>)</th>
<th>Size</th>
<th></th>
</tr>
</thead>
<tbody class="disk_titles">
<% disks.each do |disk| %>
<% disk.disk_titles.sort_by { |d| movie.runtime_range.include?(d.duration) ? 0 : 1 }.each do |disk_title| %>
<% text_class = 'text-primary-emphasis' if movie.runtime_range.include?(disk_title.duration) %>
<% text_class = 'text-success-emphasis' if movie.disk_title&.name == disk_title.name %>
<tr>
<th scope="row" class="<%= text_class %>"><%= disk_title.id %></th>
<td class="<%= text_class %>"><%= disk_title.name %></td>
<td class="<%= text_class %>"><%= distance_of_time_in_words(disk_title.duration.seconds) %></td>
<td class="<%= disk_title.size >= free_disk_space ? 'text-danger' : text_class %>">
<%= number_to_human_size(disk_title.size, precision: 3) %>
<% if disk_title.size >= free_disk_space %>
WARNING: Not enough space available to rip need another <%= number_to_human_size(disk_title.size - free_disk_space, precision: 3) %>
<% end %>
</td>
<td class="<%= text_class %>">
<%= link_to 'Rip', video_disk_title_path(movie, disk_title), data: { turbo_method: :patch } %>
</td>
</tr>
<% end %>
<% end %>

<% end %>
</tbody>
</table>
</tbody>
</table>
<% elsif LoadDiskWorker.job.pending? %>
<p class="text-info">Loading Disk this might takes a while. Page will reload once ready. Fell free to refresh the page.</p>
<% else %>
<p class="text-warn">Sorry no Disk is currently loaded or could be detected. Refresh page if your seeing this warning but know a disk is present.</p>
<% end %>
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
37 changes: 30 additions & 7 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|
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
8 changes: 3 additions & 5 deletions app/services/list_drives_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

class ListDrivesService
extend Dry::Initializer
include Shell
include MkvParser

option :noscan, Types::Bool, default: -> { false }

class << self
def results(*)
new(*).results
def results(...)
new(...).results
end
end

Expand All @@ -32,7 +30,7 @@ def info
end

def drives
@drives ||= parse_mkv_string(info.stdout_str).select do |i|
@drives ||= info.parsed_mkv.select do |i|
i.is_a?(MkvParser::DRV) && i.enabled.to_i.positive?
end
end
Expand Down
12 changes: 11 additions & 1 deletion app/tool_box/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
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

def success?
status.success?
end

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

def capture3(*cmd)
Rails.logger.debug { "command: #{cmd.join(', ')}" }
Expand Down
3 changes: 3 additions & 0 deletions app/views/movies/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<%= link_to_movie_db_movie @movie.the_movie_db_id %>
</small>
</p>
<p class="text-primary-emphasis">Blue Means the disk title is within the runtime range of the movie.</p>
<p class="text-success-emphasis">Gream Means the disk has been ripped before.</p>

<div id='disk-selector'>
<%= render MovieTitleTableComponent.new(disks: @disks, movie: @movie) %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/workers/rip_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create_mkv(disk_title)

def upload_mkv(disk_title)
sleep 1 while UploadWorker.job.pending?
UploadWorker.perform_async(disk_title)
UploadWorker.perform_async(disk_title:)
end

def disk_titles
Expand Down

0 comments on commit faf544a

Please sign in to comment.