Use is_valid?

This commit is contained in:
Alex Ling 2022-05-22 05:12:43 +00:00
parent 872e6dc6d6
commit e6dbeb623b
5 changed files with 21 additions and 8 deletions

View File

@ -107,4 +107,8 @@ class ArchiveEntry < Entry
def examine : Bool def examine : Bool
File.exists? @zip_path File.exists? @zip_path
end end
def self.is_valid?(path : String) : Bool
is_supported_file path
end
end end

View File

@ -25,7 +25,7 @@ class DirEntry < Entry
return return
end end
unless DirEntry.validate_directory_entry @dir_path unless DirEntry.is_valid? @dir_path
@err_msg = "Directory #{@dir_path} is not valid directory entry." @err_msg = "Directory #{@dir_path} is not valid directory entry."
Logger.warn "#{@err_msg} Please make sure the " \ Logger.warn "#{@err_msg} Please make sure the " \
"directory has valid images." "directory has valid images."
@ -129,7 +129,7 @@ class DirEntry < Entry
.sort { |a, b| compare_numerically a, b } .sort { |a, b| compare_numerically a, b }
end end
def self.validate_directory_entry(dir_path) def self.is_valid?(path : String) : Bool
image_files(dir_path).size > 0 image_files(path).size > 0
end end
end end

View File

@ -28,8 +28,7 @@ abstract class Entry
if err_msg if err_msg
json.field "err_msg", err_msg json.field "err_msg", err_msg
end end
# for API backward compatability json.field "zip_path", path # for API backward compatability
json.field "zip_path", path
json.field "title_id", @book.id json.field "title_id", @book.id
json.field "title_title", @book.title json.field "title_title", @book.title
json.field "sort_title", sort_title json.field "sort_title", sort_title
@ -220,6 +219,16 @@ abstract class Entry
date_added.not_nil! # is it ok to set not_nil! here? date_added.not_nil! # is it ok to set not_nil! here?
end end
# Hack to have abstract class methods
# https://github.com/crystal-lang/crystal/issues/5956
private module ClassMethods
abstract def is_valid?(path : String) : Bool
end
macro inherited
extend ClassMethods
end
abstract def path : String abstract def path : String
abstract def read_page(page_num) abstract def read_page(page_num)

View File

@ -53,7 +53,7 @@ class Title
Library.default.title_hash[title.id] = title Library.default.title_hash[title.id] = title
@title_ids << title.id @title_ids << title.id
end end
if DirEntry.validate_directory_entry path if DirEntry.is_valid? path
entry = DirEntry.new path, self entry = DirEntry.new path, self
@entries << entry if entry.pages > 0 || entry.err_msg @entries << entry if entry.pages > 0 || entry.err_msg
end end
@ -146,7 +146,7 @@ class Title
path = File.join dir, fn path = File.join dir, fn
if File.directory? path if File.directory? path
unless remained_entry_paths.includes? path unless remained_entry_paths.includes? path
if DirEntry.validate_directory_entry path if DirEntry.is_valid? path
entry = DirEntry.new path, self entry = DirEntry.new path, self
if entry.pages > 0 || entry.err_msg if entry.pages > 0 || entry.err_msg
@entries << entry @entries << entry

View File

@ -64,7 +64,7 @@ class Dir
path = File.join dirname, fn path = File.join dirname, fn
if File.directory? path if File.directory? path
signatures << Dir.contents_signature path, cache signatures << Dir.contents_signature path, cache
if DirEntry.validate_directory_entry path if DirEntry.is_valid? path
signatures << fn signatures << fn
end end
else else