Set up API endpoint for setting display names

This commit is contained in:
Alex Ling 2020-03-27 04:19:21 +00:00
parent d2da8d0b9a
commit 51d19328be
2 changed files with 87 additions and 26 deletions

View File

@ -15,12 +15,12 @@ struct Image
end end
class Entry class Entry
property zip_path : String, book_title : String, title : String, property zip_path : String, book : Title, title : String,
size : String, pages : Int32, cover_url : String, id : String, size : String, pages : Int32, cover_url : String, id : String,
title_id : String, encoded_path : String, encoded_title : String, title_id : String, encoded_path : String, encoded_title : String,
mtime : Time mtime : Time
def initialize(path, @book_title, @title_id, storage) def initialize(path, @book, @title_id, storage)
@zip_path = path @zip_path = path
@encoded_path = URI.encode path @encoded_path = URI.encode path
@title = File.basename path, File.extname path @title = File.basename path, File.extname path
@ -41,11 +41,11 @@ class Entry
def to_json(json : JSON::Builder) def to_json(json : JSON::Builder)
json.object do json.object do
{% for str in ["zip_path", "book_title", "title", "size", {% for str in ["zip_path", "title", "size", "cover_url", "id",
"cover_url", "id", "title_id", "encoded_path", "title_id", "encoded_path", "encoded_title"] %}
"encoded_title"] %}
json.field {{str}}, @{{str.id}} json.field {{str}}, @{{str.id}}
{% end %} {% end %}
json.field "display_name", @book.display_name @title
json.field "pages" {json.number @pages} json.field "pages" {json.number @pages}
json.field "mtime" {json.number @mtime.to_unix} json.field "mtime" {json.number @mtime.to_unix}
end end
@ -80,14 +80,14 @@ class Title
entries : Array(Entry), title : String, id : String, entries : Array(Entry), title : String, id : String,
encoded_title : String, mtime : Time encoded_title : String, mtime : Time
def initialize(dir : String, @parent_id, storage, def initialize(@dir : String, @parent_id, storage,
@logger : MLogger, @library : Library) @logger : MLogger, @library : Library)
@dir = dir
@id = storage.get_id @dir, true @id = storage.get_id @dir, true
@title = File.basename dir @title = File.basename dir
@encoded_title = URI.encode @title @encoded_title = URI.encode @title
@title_ids = [] of String @title_ids = [] of String
@entries = [] of Entry @entries = [] of Entry
@mtime = File.info(dir).modification_time
Dir.entries(dir).each do |fn| Dir.entries(dir).each do |fn|
next if fn.starts_with? "." next if fn.starts_with? "."
@ -101,11 +101,16 @@ class Title
end end
if [".zip", ".cbz"].includes? File.extname path if [".zip", ".cbz"].includes? File.extname path
next if !valid_zip path next if !valid_zip path
entry = Entry.new path, @title, @id, storage entry = Entry.new path, self, @id, storage
@entries << entry if entry.pages > 0 @entries << entry if entry.pages > 0
end end
end end
mtimes = [@mtime]
mtimes += @title_ids.map{|e| @library.title_hash[e].mtime}
mtimes += @entries.map{|e| e.mtime}
@mtime = mtimes.max
@title_ids.sort! do |a, b| @title_ids.sort! do |a, b|
compare_alphanumerically @library.title_hash[a].title, compare_alphanumerically @library.title_hash[a].title,
@library.title_hash[b].title @library.title_hash[b].title
@ -113,11 +118,6 @@ class Title
@entries.sort! do |a, b| @entries.sort! do |a, b|
compare_alphanumerically a.title, b.title compare_alphanumerically a.title, b.title
end end
mtimes = [File.info(dir).modification_time]
mtimes += @title_ids.map{|e| @library.title_hash[e].mtime}
mtimes += @entries.map{|e| e.mtime}
@mtime = mtimes.max
end end
def to_json(json : JSON::Builder) def to_json(json : JSON::Builder)
@ -125,6 +125,7 @@ class Title
{% for str in ["dir", "title", "id", "encoded_title"] %} {% for str in ["dir", "title", "id", "encoded_title"] %}
json.field {{str}}, @{{str.id}} json.field {{str}}, @{{str.id}}
{% end %} {% end %}
json.field "display_name", display_name
json.field "mtime" {json.number @mtime.to_unix} json.field "mtime" {json.number @mtime.to_unix}
json.field "titles" do json.field "titles" do
json.raw self.titles.to_json json.raw self.titles.to_json
@ -179,21 +180,51 @@ class Title
return false return false
end end
end end
def get_entry(eid) def get_entry(eid)
@entries.find { |e| e.id == eid } @entries.find { |e| e.id == eid }
end end
def display_name
info = TitleInfo.new @dir
dn = info.display_name
dn.empty? ? @title : dn
end
def display_name(entry_name)
info = TitleInfo.new @dir
dn = info.entry_display_name[entry_name]?
unless dn.nil? || dn.empty?
return dn
end
entry_name
end
def set_display_name(dn)
info = TitleInfo.new @dir
info.display_name = dn
info.save
end
def set_display_name(entry_name : String, dn)
info = TitleInfo.new @dir
info.entry_display_name[entry_name] = dn
info.save
end
# For backward backward compatibility with v0.1.0, we save entry titles # For backward backward compatibility with v0.1.0, we save entry titles
# instead of IDs in info.json # instead of IDs in info.json
def save_progress(username, entry, page) def save_progress(username, entry, page)
info = TitleInfo.new @dir info = TitleInfo.new @dir
if info.progress[username]?.nil? if info.progress[username]?.nil?
info.progress[username] = {entry => page} info.progress[username] = {entry => page}
info.save @dir info.save
return return
end end
info.progress[username][entry] = page info.progress[username][entry] = page
info.save @dir info.save
end end
def load_progress(username, entry) def load_progress(username, entry)
info = TitleInfo.new @dir info = TitleInfo.new @dir
if info.progress[username]?.nil? if info.progress[username]?.nil?
@ -204,6 +235,7 @@ class Title
end end
info.progress[username][entry] info.progress[username][entry]
end end
def load_percetage(username, entry) def load_percetage(username, entry)
info = TitleInfo.new @dir info = TitleInfo.new @dir
page = load_progress username, entry page = load_progress username, entry
@ -211,6 +243,7 @@ class Title
return 0.0 if entry_obj.nil? return 0.0 if entry_obj.nil?
page / entry_obj.pages page / entry_obj.pages
end end
def load_percetage(username) def load_percetage(username)
return 0.0 if @entries.empty? return 0.0 if @entries.empty?
read_pages = total_pages = 0 read_pages = total_pages = 0
@ -220,6 +253,7 @@ class Title
end end
read_pages / total_pages read_pages / total_pages
end end
def next_entry(current_entry_obj) def next_entry(current_entry_obj)
idx = @entries.index current_entry_obj idx = @entries.index current_entry_obj
return nil if idx.nil? || idx == @entries.size - 1 return nil if idx.nil? || idx == @entries.size - 1
@ -228,26 +262,30 @@ class Title
end end
class TitleInfo class TitleInfo
# { user1: { entry1: 10, entry2: 0 } }
include JSON::Serializable include JSON::Serializable
property comment = "Generated by Mango. DO NOT EDIT!" property comment = "Generated by Mango. DO NOT EDIT!"
property progress : Hash(String, Hash(String, Int32)) # { user1: { entry1: 10, entry2: 0 } }
property progress = {} of String => Hash(String, Int32)
property display_name = ""
# { entry1 : "display name" }
property entry_display_name = {} of String => String
def initialize(title_dir) @[JSON::Field(ignore: true)]
info = nil property dir : String = ""
json_path = File.join title_dir, "info.json" def initialize(@dir)
json_path = File.join @dir, "info.json"
if File.exists? json_path if File.exists? json_path
info = TitleInfo.from_json File.read json_path info = TitleInfo.from_json File.read json_path
else @progress = info.progress.clone
info = TitleInfo.from_json "{\"progress\": {}}" @display_name = info.display_name
@entry_display_name = info.entry_display_name.clone
end end
@progress = info.progress.clone
end end
def save(title_dir)
json_path = File.join title_dir, "info.json" def save
json_path = File.join @dir, "info.json"
File.write json_path, self.to_pretty_json File.write json_path, self.to_pretty_json
end end
end end

View File

@ -90,6 +90,29 @@ class APIRouter < Router
end end
end end
post "/api/admin/display_name/:title/:name" do |env|
begin
title = (@context.library.get_title env.params.url["title"])
.not_nil!
entry = title.get_entry env.params.query["entry"]?
name = env.params.url["name"]
if entry.nil?
title.set_display_name name
else
title.set_display_name entry.title, name
end
rescue e
@context.error e
send_json env, {
"success" => false,
"error" => e.message
}.to_json
else
send_json env, {"success" => true}.to_json
end
end
get "/api/admin/mangadex/manga/:id" do |env| get "/api/admin/mangadex/manga/:id" do |env|
begin begin
id = env.params.url["id"] id = env.params.url["id"]