From bdba7bdd13cb42e82a9c8559a4bf130fbe3b3b57 Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Sat, 18 Jul 2020 13:29:03 +0000 Subject: [PATCH] Show unreadable archives in web interface (#49) --- public/css/mango.css | 4 +++ src/library/entry.cr | 42 +++++++++++++++++++++--------- src/library/title.cr | 18 +++---------- src/routes/reader.cr | 5 +++- src/views/components/card.html.ecr | 34 ++++++++++++++++-------- src/views/reader-error.html.ecr | 31 ++++++++++++++++++++++ 6 files changed, 96 insertions(+), 38 deletions(-) create mode 100644 src/views/reader-error.html.ecr diff --git a/public/css/mango.css b/public/css/mango.css index 40d5637..2f452ea 100644 --- a/public/css/mango.css +++ b/public/css/mango.css @@ -97,3 +97,7 @@ td>.uk-dropdown { .item .uk-card-title { font-size: 1rem; } + +.grayscale { + filter: grayscale(100%); +} diff --git a/src/library/entry.cr b/src/library/entry.cr index 165b469..7e8d81d 100644 --- a/src/library/entry.cr +++ b/src/library/entry.cr @@ -1,20 +1,14 @@ class Entry property zip_path : String, book : Title, title : String, size : String, pages : Int32, id : String, title_id : String, - encoded_path : String, encoded_title : String, mtime : Time + encoded_path : String, encoded_title : String, mtime : Time, + err_msg : String? - def initialize(path, @book, @title_id, storage) - @zip_path = path - @encoded_path = URI.encode path - @title = File.basename path, File.extname path + def initialize(@zip_path, @book, @title_id, storage) + @encoded_path = URI.encode @zip_path + @title = File.basename @zip_path, File.extname @zip_path @encoded_title = URI.encode @title - @size = (File.size path).humanize_bytes - file = ArchiveFile.new path - @pages = file.entries.count do |e| - SUPPORTED_IMG_TYPES.includes? \ - MIME.from_filename? e.filename - end - file.close + @size = (File.size @zip_path).humanize_bytes id = storage.get_id @zip_path, false if id.nil? id = random_str @@ -26,6 +20,28 @@ class Entry end @id = id @mtime = File.info(@zip_path).modification_time + + unless File.readable? @zip_path + @err_msg = "File #{@zip_path} is not readable." + Logger.warn "#{@err_msg} Please make sure the " \ + "file permission is configured correctly." + return + end + + archive_exception = validate_archive @zip_path + unless archive_exception.nil? + @err_msg = "Archive error: #{archive_exception}" + Logger.warn "Unable to extract archive #{@zip_path}. " \ + "Ignoring it. #{@err_msg}" + return + end + + file = ArchiveFile.new @zip_path + @pages = file.entries.count do |e| + SUPPORTED_IMG_TYPES.includes? \ + MIME.from_filename? e.filename + end + file.close end def to_json(json : JSON::Builder) @@ -50,6 +66,7 @@ class Entry end def cover_url + return "#{Config.current.base_url}img/icon.png" if @err_msg url = "#{Config.current.base_url}api/page/#{@title_id}/#{@id}/1" TitleInfo.new @book.dir do |info| info_url = info.entry_cover_url[@title]? @@ -61,6 +78,7 @@ class Entry end def read_page(page_num) + raise "Unreadble archive. #{@err_msg}" if @err_msg img = nil ArchiveFile.open @zip_path do |file| page = file.entries diff --git a/src/library/title.cr b/src/library/title.cr index 22f75d3..f1e96dc 100644 --- a/src/library/title.cr +++ b/src/library/title.cr @@ -34,19 +34,8 @@ class Title next end if [".zip", ".cbz", ".rar", ".cbr"].includes? File.extname path - unless File.readable? path - Logger.warn "File #{path} is not readable. Please make sure the " \ - "file permission is configured correctly." - next - end - archive_exception = validate_archive path - unless archive_exception.nil? - Logger.warn "Unable to extract archive #{path}. Ignoring it. " \ - "Archive error: #{archive_exception}" - next - end entry = Entry.new path, self, @id, storage - @entries << entry if entry.pages > 0 + @entries << entry if entry.pages > 0 || entry.err_msg end end @@ -166,8 +155,9 @@ class Title def cover_url url = "#{Config.current.base_url}img/icon.png" - if @entries.size > 0 - url = @entries[0].cover_url + readable_entries = @entries.select &.err_msg.nil? + if readable_entries.size > 0 + url = readable_entries[0].cover_url end TitleInfo.new @dir do |info| info_url = info.cover_url diff --git a/src/routes/reader.cr b/src/routes/reader.cr index 69a4cc3..50d6bcb 100644 --- a/src/routes/reader.cr +++ b/src/routes/reader.cr @@ -4,11 +4,14 @@ class ReaderRouter < Router def initialize get "/reader/:title/:entry" do |env| begin + username = get_username env + title = (@context.library.get_title env.params.url["title"]).not_nil! entry = (title.get_entry env.params.url["entry"]).not_nil! + next layout "reader-error" if entry.err_msg + # load progress - username = get_username env page = entry.load_progress username # we go back 2 * `IMGS_PER_PAGE` pages. the infinite scroll # library perloads a few pages in advance, and the user diff --git a/src/views/components/card.html.ecr b/src/views/components/card.html.ecr index 0598d45..3eaf3bf 100644 --- a/src/views/components/card.html.ecr +++ b/src/views/components/card.html.ecr @@ -15,29 +15,36 @@ <% end %>>
<% end %> " <% if item.is_a? Entry %> - data-encoded-path="<%= item.encoded_path %>" - data-pages="<%= item.pages %>" - data-progress="<%= (progress * 100).round(1) %>" - data-encoded-book-title="<%= item.book.encoded_display_name %>" - data-encoded-title="<%= item.encoded_display_name %>" - data-book-id="<%= item.book.id %>" - data-id="<%= item.id %>" + <% if item.err_msg %> + onclick="location='<%= base_url %>reader/<%= item.book.id %>/<%= item.id %>'" + <% else %> + data-encoded-path="<%= item.encoded_path %>" + data-pages="<%= item.pages %>" + data-progress="<%= (progress * 100).round(1) %>" + data-encoded-book-title="<%= item.book.encoded_display_name %>" + data-encoded-title="<%= item.encoded_display_name %>" + data-book-id="<%= item.book.id %>" + data-id="<%= item.id %>" + <% end %> <% else %> onclick="location='<%= base_url %>book/<%= item.id %>'" <% end %>>
- + + class="grayscale" + <% end %>>
- <% unless progress < 0 || progress > 100 %> + <% unless progress < 0 || progress > 100 || progress.nan? %>
<%= (progress * 100).round(1) %>%
<% end %> @@ -51,7 +58,12 @@ <%= HTML.escape(item.book.display_name) %> <% end %> <% if item.is_a? Entry %> -

<%= item.pages %> pages

+ <% if item.err_msg %> +

Error

+
<%= item.err_msg %>
+ <% else %> +

<%= item.pages %> pages

+ <% end %> <% end %> <% if item.is_a? Title %> <% if grouped_count == 1 %> diff --git a/src/views/reader-error.html.ecr b/src/views/reader-error.html.ecr new file mode 100644 index 0000000..5dc7fd3 --- /dev/null +++ b/src/views/reader-error.html.ecr @@ -0,0 +1,31 @@ + + +<% content_for "script" do %> + +<% end %>