- create scan method to prepare for periodic scanning

This commit is contained in:
Alex Ling 2020-02-16 04:01:30 +00:00
parent 93e6d7cae6
commit e7583ce788
2 changed files with 15 additions and 10 deletions

View File

@ -144,17 +144,22 @@ end
class Library class Library
JSON.mapping dir: String, titles: Array(Title) JSON.mapping dir: String, titles: Array(Title)
def initialize(dir : String) def initialize(@dir)
@dir = dir # explicitly initialize @titles to bypass the compiler check. it will
unless Dir.exists? dir # be filled with actuall Titles in the `scan` call below
Dir.mkdir_p dir @titles = [] of Title
end scan
@titles = (Dir.entries dir)
.select { |path| File.directory? File.join dir, path }
.map { |path| Title.new File.join dir, path }
.select { |title| !title.entries.empty? }
end end
def get_title(name) def get_title(name)
@titles.find { |t| t.title == name } @titles.find { |t| t.title == name }
end end
def scan
unless Dir.exists? @dir
Dir.mkdir_p @dir
end
@titles = (Dir.entries @dir)
.select { |path| File.directory? File.join @dir, path }
.map { |path| Title.new File.join @dir, path }
.select { |title| !title.entries.empty? }
end
end end

View File

@ -265,7 +265,7 @@ class Server
post "/api/admin/scan" do |env| post "/api/admin/scan" do |env|
start = Time.utc start = Time.utc
@library = Library.new @config.@library_path @library.scan
ms = (Time.utc - start).total_milliseconds ms = (Time.utc - start).total_milliseconds
send_json env, { send_json env, {
"milliseconds" => ms, "milliseconds" => ms,