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

View File

@ -32,6 +32,10 @@ shards:
github: mamantoha/http_proxy github: mamantoha/http_proxy
version: 0.7.1 version: 0.7.1
image_size:
github: hkalexling/image_size.cr
version: 0.1.1
kemal: kemal:
github: kemalcr/kemal github: kemalcr/kemal
version: 0.26.1 version: 0.26.1

View File

@ -34,3 +34,5 @@ dependencies:
github: kostya/myhtml github: kostya/myhtml
http_proxy: http_proxy:
github: mamantoha/http_proxy github: mamantoha/http_proxy
image_size:
github: hkalexling/image_size.cr

View File

@ -1,3 +1,5 @@
require "image_size"
class Entry class Entry
property zip_path : String, book : Title, title : String, property zip_path : String, book : Title, title : String,
size : String, pages : Int32, id : String, encoded_path : String, size : String, pages : Int32, id : String, encoded_path : String,
@ -99,6 +101,31 @@ class Entry
img img
end 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) def next_entry(username)
entries = @book.sorted_entries username entries = @book.sorted_entries username
idx = entries.index self idx = entries.index self

View File

@ -332,5 +332,28 @@ class APIRouter < Router
}.to_json }.to_json
end end
end end
get "/api/ratios/:tid/:eid" do |env|
begin
tid = env.params.url["tid"]
eid = env.params.url["eid"]
title = @context.library.get_title tid
raise "Title ID `#{tid}` not found" if title.nil?
entry = title.get_entry eid
raise "Entry ID `#{eid}` of `#{title.title}` not found" if entry.nil?
ratios = entry.page_aspect_ratios
send_json env, {
"success" => true,
"ratios" => ratios,
}.to_json
rescue e
send_json env, {
"success" => false,
"error" => e.message,
}.to_json
end
end
end end
end end