mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-05 04:15:35 -04:00
Set up API endpoint for setting display names
This commit is contained in:
parent
d2da8d0b9a
commit
51d19328be
@ -15,12 +15,12 @@ struct Image
|
||||
end
|
||||
|
||||
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,
|
||||
title_id : String, encoded_path : String, encoded_title : String,
|
||||
mtime : Time
|
||||
|
||||
def initialize(path, @book_title, @title_id, storage)
|
||||
def initialize(path, @book, @title_id, storage)
|
||||
@zip_path = path
|
||||
@encoded_path = URI.encode path
|
||||
@title = File.basename path, File.extname path
|
||||
@ -41,11 +41,11 @@ class Entry
|
||||
|
||||
def to_json(json : JSON::Builder)
|
||||
json.object do
|
||||
{% for str in ["zip_path", "book_title", "title", "size",
|
||||
"cover_url", "id", "title_id", "encoded_path",
|
||||
"encoded_title"] %}
|
||||
{% for str in ["zip_path", "title", "size", "cover_url", "id",
|
||||
"title_id", "encoded_path", "encoded_title"] %}
|
||||
json.field {{str}}, @{{str.id}}
|
||||
{% end %}
|
||||
json.field "display_name", @book.display_name @title
|
||||
json.field "pages" {json.number @pages}
|
||||
json.field "mtime" {json.number @mtime.to_unix}
|
||||
end
|
||||
@ -80,14 +80,14 @@ class Title
|
||||
entries : Array(Entry), title : String, id : String,
|
||||
encoded_title : String, mtime : Time
|
||||
|
||||
def initialize(dir : String, @parent_id, storage,
|
||||
def initialize(@dir : String, @parent_id, storage,
|
||||
@logger : MLogger, @library : Library)
|
||||
@dir = dir
|
||||
@id = storage.get_id @dir, true
|
||||
@title = File.basename dir
|
||||
@encoded_title = URI.encode @title
|
||||
@title_ids = [] of String
|
||||
@entries = [] of Entry
|
||||
@mtime = File.info(dir).modification_time
|
||||
|
||||
Dir.entries(dir).each do |fn|
|
||||
next if fn.starts_with? "."
|
||||
@ -101,11 +101,16 @@ class Title
|
||||
end
|
||||
if [".zip", ".cbz"].includes? File.extname 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
|
||||
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|
|
||||
compare_alphanumerically @library.title_hash[a].title,
|
||||
@library.title_hash[b].title
|
||||
@ -113,11 +118,6 @@ class Title
|
||||
@entries.sort! do |a, b|
|
||||
compare_alphanumerically a.title, b.title
|
||||
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
|
||||
|
||||
def to_json(json : JSON::Builder)
|
||||
@ -125,6 +125,7 @@ class Title
|
||||
{% for str in ["dir", "title", "id", "encoded_title"] %}
|
||||
json.field {{str}}, @{{str.id}}
|
||||
{% end %}
|
||||
json.field "display_name", display_name
|
||||
json.field "mtime" {json.number @mtime.to_unix}
|
||||
json.field "titles" do
|
||||
json.raw self.titles.to_json
|
||||
@ -179,21 +180,51 @@ class Title
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def get_entry(eid)
|
||||
@entries.find { |e| e.id == eid }
|
||||
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
|
||||
# instead of IDs in info.json
|
||||
def save_progress(username, entry, page)
|
||||
info = TitleInfo.new @dir
|
||||
if info.progress[username]?.nil?
|
||||
info.progress[username] = {entry => page}
|
||||
info.save @dir
|
||||
info.save
|
||||
return
|
||||
end
|
||||
info.progress[username][entry] = page
|
||||
info.save @dir
|
||||
info.save
|
||||
end
|
||||
|
||||
def load_progress(username, entry)
|
||||
info = TitleInfo.new @dir
|
||||
if info.progress[username]?.nil?
|
||||
@ -204,6 +235,7 @@ class Title
|
||||
end
|
||||
info.progress[username][entry]
|
||||
end
|
||||
|
||||
def load_percetage(username, entry)
|
||||
info = TitleInfo.new @dir
|
||||
page = load_progress username, entry
|
||||
@ -211,6 +243,7 @@ class Title
|
||||
return 0.0 if entry_obj.nil?
|
||||
page / entry_obj.pages
|
||||
end
|
||||
|
||||
def load_percetage(username)
|
||||
return 0.0 if @entries.empty?
|
||||
read_pages = total_pages = 0
|
||||
@ -220,6 +253,7 @@ class Title
|
||||
end
|
||||
read_pages / total_pages
|
||||
end
|
||||
|
||||
def next_entry(current_entry_obj)
|
||||
idx = @entries.index current_entry_obj
|
||||
return nil if idx.nil? || idx == @entries.size - 1
|
||||
@ -228,26 +262,30 @@ class Title
|
||||
end
|
||||
|
||||
class TitleInfo
|
||||
# { user1: { entry1: 10, entry2: 0 } }
|
||||
include JSON::Serializable
|
||||
|
||||
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)
|
||||
info = nil
|
||||
@[JSON::Field(ignore: true)]
|
||||
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
|
||||
info = TitleInfo.from_json File.read json_path
|
||||
else
|
||||
info = TitleInfo.from_json "{\"progress\": {}}"
|
||||
@progress = info.progress.clone
|
||||
@display_name = info.display_name
|
||||
@entry_display_name = info.entry_display_name.clone
|
||||
end
|
||||
|
||||
@progress = info.progress.clone
|
||||
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
|
||||
end
|
||||
end
|
||||
|
@ -90,6 +90,29 @@ class APIRouter < Router
|
||||
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|
|
||||
begin
|
||||
id = env.params.url["id"]
|
||||
|
Loading…
x
Reference in New Issue
Block a user