From c51cb28df210cef6444fb9d11927e251b504ef33 Mon Sep 17 00:00:00 2001 From: Leeingnyo Date: Mon, 25 Jan 2021 23:13:35 +0900 Subject: [PATCH 1/4] make filename extension downcase for comparing --- src/library/title.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/library/title.cr b/src/library/title.cr index 001e9af..88c165b 100644 --- a/src/library/title.cr +++ b/src/library/title.cr @@ -36,7 +36,7 @@ class Title @title_ids << title.id next end - if [".zip", ".cbz", ".rar", ".cbr"].includes? File.extname path + if [".zip", ".cbz", ".rar", ".cbr"].includes? (File.extname path).downcase entry = Entry.new path, self @entries << entry if entry.pages > 0 || entry.err_msg end From 2b0cf4133694dc21176951e0eb382dd446730b9d Mon Sep 17 00:00:00 2001 From: Leeingnyo Date: Tue, 26 Jan 2021 04:14:17 +0900 Subject: [PATCH 2/4] add and apply util method is_interesting_file --- src/library/title.cr | 2 +- src/util/signature.cr | 4 +++- src/util/util.cr | 13 +++++++++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/library/title.cr b/src/library/title.cr index 88c165b..3f3926a 100644 --- a/src/library/title.cr +++ b/src/library/title.cr @@ -36,7 +36,7 @@ class Title @title_ids << title.id next end - if [".zip", ".cbz", ".rar", ".cbr"].includes? (File.extname path).downcase + if is_interesting_file path entry = Entry.new path, self @entries << entry if entry.pages > 0 || entry.err_msg end diff --git a/src/util/signature.cr b/src/util/signature.cr index 0db6b21..7dc738b 100644 --- a/src/util/signature.cr +++ b/src/util/signature.cr @@ -1,3 +1,5 @@ +require "./util" + class File abstract struct Info def inode @@ -11,7 +13,7 @@ class File # ensures that moving (unless to another device) and renaming the file # preserves the signature, while copying or editing the file changes it. def self.signature(filename) : UInt64 - return 0u64 unless %w(.zip .rar .cbz .cbr).includes? File.extname filename + return 0u64 unless is_interesting_file filename info = File.info filename signatures = [ info.inode, diff --git a/src/util/util.cr b/src/util/util.cr index f174c39..17fe105 100644 --- a/src/util/util.cr +++ b/src/util/util.cr @@ -1,7 +1,8 @@ -IMGS_PER_PAGE = 5 -ENTRIES_IN_HOME_SECTIONS = 8 -UPLOAD_URL_PREFIX = "/uploads" -STATIC_DIRS = ["/css", "/js", "/img", "/favicon.ico"] +IMGS_PER_PAGE = 5 +ENTRIES_IN_HOME_SECTIONS = 8 +UPLOAD_URL_PREFIX = "/uploads" +STATIC_DIRS = ["/css", "/js", "/img", "/favicon.ico"] +INTERESTING_FILE_EXTNAMES = [".zip", ".cbz", ".rar", ".cbr"] def random_str UUID.random.to_s.gsub "-", "" @@ -31,6 +32,10 @@ def register_mime_types end end +def is_interesting_file(path) + INTERESTING_FILE_EXTNAMES.includes? (File.extname path).downcase +end + struct Int def or(other : Int) if self == 0 From 0ba2409c9a07d52d5811323145faacf34249188e Mon Sep 17 00:00:00 2001 From: Leeingnyo Date: Tue, 26 Jan 2021 04:18:09 +0900 Subject: [PATCH 3/4] add tests about is_interesting_file --- spec/util_spec.cr | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spec/util_spec.cr b/spec/util_spec.cr index 94c326c..5401eb1 100644 --- a/spec/util_spec.cr +++ b/spec/util_spec.cr @@ -35,6 +35,26 @@ describe "compare_numerically" do end end +describe "is_interesting_file" do + it "returns true when filename has interesting file extension" do + filename = "manga.cbz" + result = is_interesting_file filename + result.should eq true + end + + it "returns false if file extension is not interesting" do + filename = "info.json" + result = is_interesting_file filename + result.should eq false + end + + it "returns true when fileext has uppercase" do + filename = "manga.ZiP" + result = is_interesting_file filename + result.should eq true + end +end + describe "chapter_sort" do it "sorts correctly" do ary = ["Vol.1 Ch.01", "Vol.1 Ch.02", "Vol.2 Ch. 2.5", "Ch. 3", "Ch.04"] From def64d9f98be18e2baf091360b0b4c8df53aef81 Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Tue, 26 Jan 2021 10:55:50 +0000 Subject: [PATCH 4/4] Rename interesting files to supported files --- spec/util_spec.cr | 17 +++++++---------- src/library/title.cr | 2 +- src/util/signature.cr | 2 +- src/util/util.cr | 14 +++++++------- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/spec/util_spec.cr b/spec/util_spec.cr index 5401eb1..3ee4aac 100644 --- a/spec/util_spec.cr +++ b/spec/util_spec.cr @@ -35,23 +35,20 @@ describe "compare_numerically" do end end -describe "is_interesting_file" do - it "returns true when filename has interesting file extension" do +describe "is_supported_file" do + it "returns true when the filename has a supported extension" do filename = "manga.cbz" - result = is_interesting_file filename - result.should eq true + is_supported_file(filename).should eq true end - it "returns false if file extension is not interesting" do + it "returns true when the filename does not have a supported extension" do filename = "info.json" - result = is_interesting_file filename - result.should eq false + is_supported_file(filename).should eq false end - it "returns true when fileext has uppercase" do + it "is case insensitive" do filename = "manga.ZiP" - result = is_interesting_file filename - result.should eq true + is_supported_file(filename).should eq true end end diff --git a/src/library/title.cr b/src/library/title.cr index 3f3926a..4c439e7 100644 --- a/src/library/title.cr +++ b/src/library/title.cr @@ -36,7 +36,7 @@ class Title @title_ids << title.id next end - if is_interesting_file path + if is_supported_file path entry = Entry.new path, self @entries << entry if entry.pages > 0 || entry.err_msg end diff --git a/src/util/signature.cr b/src/util/signature.cr index 7dc738b..05eb62d 100644 --- a/src/util/signature.cr +++ b/src/util/signature.cr @@ -13,7 +13,7 @@ class File # ensures that moving (unless to another device) and renaming the file # preserves the signature, while copying or editing the file changes it. def self.signature(filename) : UInt64 - return 0u64 unless is_interesting_file filename + return 0u64 unless is_supported_file filename info = File.info filename signatures = [ info.inode, diff --git a/src/util/util.cr b/src/util/util.cr index 17fe105..873a226 100644 --- a/src/util/util.cr +++ b/src/util/util.cr @@ -1,8 +1,8 @@ -IMGS_PER_PAGE = 5 -ENTRIES_IN_HOME_SECTIONS = 8 -UPLOAD_URL_PREFIX = "/uploads" -STATIC_DIRS = ["/css", "/js", "/img", "/favicon.ico"] -INTERESTING_FILE_EXTNAMES = [".zip", ".cbz", ".rar", ".cbr"] +IMGS_PER_PAGE = 5 +ENTRIES_IN_HOME_SECTIONS = 8 +UPLOAD_URL_PREFIX = "/uploads" +STATIC_DIRS = ["/css", "/js", "/img", "/favicon.ico"] +SUPPORTED_FILE_EXTNAMES = [".zip", ".cbz", ".rar", ".cbr"] def random_str UUID.random.to_s.gsub "-", "" @@ -32,8 +32,8 @@ def register_mime_types end end -def is_interesting_file(path) - INTERESTING_FILE_EXTNAMES.includes? (File.extname path).downcase +def is_supported_file(path) + SUPPORTED_FILE_EXTNAMES.includes? File.extname(path).downcase end struct Int