Expose page ratios through API

This commit is contained in:
Alex Ling
2020-09-10 13:16:10 +00:00
parent ef1ab940f5
commit 88394d4636
4 changed files with 56 additions and 0 deletions
+27
View File
@@ -1,3 +1,5 @@
require "image_size"
class Entry
property zip_path : String, book : Title, title : String,
size : String, pages : Int32, id : String, encoded_path : String,
@@ -99,6 +101,31 @@ class Entry
img
end
def page_aspect_ratios
ratios = [] of Float64
ArchiveFile.open @zip_path do |file|
file.entries
.select { |e|
SUPPORTED_IMG_TYPES.includes? \
MIME.from_filename? e.filename
}
.sort { |a, b|
compare_numerically a.filename, b.filename
}
.each_with_index do |e, i|
begin
data = file.read_entry(e).not_nil!
size = ImageSize.get data
ratios << size.height / size.width
rescue
Logger.warn "Failed to read page #{i} of entry #{@id}"
ratios << 1_f64
end
end
end
ratios
end
def next_entry(username)
entries = @book.sorted_entries username
idx = entries.index self