Add nested library support (WIP)

This commit is contained in:
Alex Ling 2020-03-12 20:37:03 +00:00
parent 9536ce62e6
commit 8c7ced87f1
4 changed files with 56 additions and 15 deletions

View File

@ -60,25 +60,40 @@ class Entry
end end
class Title class Title
JSON.mapping dir: String, entries: Array(Entry), title: String, JSON.mapping dir: String, titles: Array(Title), entries: Array(Entry),
id: String, encoded_title: String, mtime: Time, logger: MLogger title: String, id: String, encoded_title: String, mtime: Time,
logger: MLogger
def initialize(dir : String, storage, @logger : MLogger) def initialize(dir : String, storage, @logger : MLogger)
@dir = dir @dir = dir
@id = storage.get_id @dir, true @id = storage.get_id @dir, true
@title = File.basename dir @title = File.basename dir
@encoded_title = URI.encode @title @encoded_title = URI.encode @title
@entries = (Dir.entries dir) @titles = [] of Title
.select { |path| [".zip", ".cbz"].includes? File.extname path } @entries = [] of Entry
.map { |path| File.join dir, path }
.select { |path| valid_zip path } Dir.entries(dir).each do |fn|
.map { |path| next if fn.starts_with? "."
Entry.new path, @title, @id, storage path = File.join dir, fn
} if File.directory? path
.select { |e| e.pages > 0 } title = Title.new path, storage, @logger
.sort { |a, b| a.title <=> b.title } next if title.entries.size == 0 && title.titles.size == 0
@titles << title
next
end
if [".zip", ".cbz"].includes? File.extname path
next if !valid_zip path
entry = Entry.new path, @title, @id, storage
@entries << entry if entry.pages > 0
end
end
@titles.sort! { |a,b| a.title <=> b.title }
@entries.sort! { |a,b| a.title <=> b.title }
mtimes = [File.info(dir).modification_time] mtimes = [File.info(dir).modification_time]
mtimes += @entries.map{|e| e.mtime} mtimes += @entries.map{|e| e.mtime}
mtimes += @titles.map{|e| e.mtime}
@mtime = mtimes.max @mtime = mtimes.max
end end
# When downloading from MangaDex, the zip/cbz file would not be valid # When downloading from MangaDex, the zip/cbz file would not be valid
@ -192,7 +207,9 @@ class Library
end end
end end
def get_title(tid) def get_title(tid)
@titles.find { |t| t.id == tid } # top level
title = @titles.find { |t| t.id == tid }
return title if !title.nil?
end end
def scan def scan
unless Dir.exists? @dir unless Dir.exists? @dir
@ -201,9 +218,11 @@ class Library
Dir.mkdir_p @dir Dir.mkdir_p @dir
end end
@titles = (Dir.entries @dir) @titles = (Dir.entries @dir)
.select { |path| File.directory? File.join @dir, path } .select { |fn| !fn.starts_with? "." }
.map { |path| Title.new File.join(@dir, path), @storage, @logger } .map { |fn| File.join @dir, fn }
.select { |title| !title.entries.empty? } .select { |path| File.directory? path }
.map { |path| Title.new path, @storage, @logger }
.select { |title| !(title.entries.empty? && title.titles.empty?) }
.sort { |a, b| a.title <=> b.title } .sort { |a, b| a.title <=> b.title }
@logger.debug "Scan completed" @logger.debug "Scan completed"
end end

View File

@ -46,6 +46,8 @@ class MainRouter < Router
username = get_username env username = get_username env
percentage = title.entries.map { |e| percentage = title.entries.map { |e|
title.load_percetage username, e.title } title.load_percetage username, e.title }
titles_percentage = title.titles.map { |t|
title.load_percetage username, t.title }
layout "title" layout "title"
rescue e rescue e
@context.error e @context.error e

View File

@ -25,9 +25,11 @@
<div class="item" data-mtime="<%= t.mtime.to_unix %>" data-progress="<%= percentage[i] %>"> <div class="item" data-mtime="<%= t.mtime.to_unix %>" data-progress="<%= percentage[i] %>">
<a class="acard" href="/book/<%= t.id %>"> <a class="acard" href="/book/<%= t.id %>">
<div class="uk-card uk-card-default"> <div class="uk-card uk-card-default">
<%- if t.entries.size > 0 -%>
<div class="uk-card-media-top"> <div class="uk-card-media-top">
<img src="<%= t.entries[0].cover_url %>" alt=""> <img src="<%= t.entries[0].cover_url %>" alt="">
</div> </div>
<%- end -%>
<div class="uk-card-body"> <div class="uk-card-body">
<div class="uk-card-badge uk-label"><%= (percentage[i] * 100).round(1) %>%</div> <div class="uk-card-badge uk-label"><%= (percentage[i] * 100).round(1) %>%</div>
<h3 class="uk-card-title"><%= t.title %></h3> <h3 class="uk-card-title"><%= t.title %></h3>

View File

@ -23,6 +23,24 @@
</div> </div>
</div> </div>
<div id="item-container" class="uk-child-width-1-4@m uk-child-width-1-2" uk-grid> <div id="item-container" class="uk-child-width-1-4@m uk-child-width-1-2" uk-grid>
<%- title.titles.each_with_index do |t, i| -%>
<div class="item" data-mtime="<%= t.mtime.to_unix %>" data-progress="<%= titles_percentage[i] %>">
<a class="acard" href="/book/<%= t.id %>">
<div class="uk-card uk-card-default">
<%- if t.entries.size > 0 -%>
<div class="uk-card-media-top">
<img src="<%= t.entries[0].cover_url %>" alt="">
</div>
<%- end -%>
<div class="uk-card-body">
<div class="uk-card-badge uk-label"><%= (titles_percentage[i] * 100).round(1) %>%</div>
<h3 class="uk-card-title"><%= t.title %></h3>
<p><%= t.entries.size %> entries</p>
</div>
</div>
</a>
</div>
<%- end -%>
<%- title.entries.each_with_index do |e, i| -%> <%- title.entries.each_with_index do |e, i| -%>
<div class="item" data-mtime="<%= e.mtime.to_unix %>" data-progress="<%= percentage[i] %>"> <div class="item" data-mtime="<%= e.mtime.to_unix %>" data-progress="<%= percentage[i] %>">
<a class="acard"> <a class="acard">