mirror of
https://github.com/hkalexling/Mango.git
synced 2026-03-20 00:00:48 -04:00
Finish the API endpoint for cover upload
This commit is contained in:
140
src/library.cr
140
src/library.cr
@@ -16,9 +16,8 @@ end
|
||||
|
||||
class Entry
|
||||
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
|
||||
size : String, pages : Int32, id : String, title_id : String,
|
||||
encoded_path : String, encoded_title : String, mtime : Time
|
||||
|
||||
def initialize(path, @book, @title_id, storage)
|
||||
@zip_path = path
|
||||
@@ -33,17 +32,17 @@ class Entry
|
||||
end
|
||||
file.close
|
||||
@id = storage.get_id @zip_path, false
|
||||
@cover_url = "/api/page/#{@title_id}/#{@id}/1"
|
||||
@mtime = File.info(@zip_path).modification_time
|
||||
end
|
||||
|
||||
def to_json(json : JSON::Builder)
|
||||
json.object do
|
||||
{% for str in ["zip_path", "title", "size", "cover_url", "id",
|
||||
"title_id", "encoded_path", "encoded_title"] %}
|
||||
{% for str in ["zip_path", "title", "size", "id", "title_id",
|
||||
"encoded_path", "encoded_title"] %}
|
||||
json.field {{str}}, @{{str.id}}
|
||||
{% end %}
|
||||
json.field "display_name", @book.display_name @title
|
||||
json.field "cover_url", cover_url
|
||||
json.field "pages" { json.number @pages }
|
||||
json.field "mtime" { json.number @mtime.to_unix }
|
||||
end
|
||||
@@ -57,6 +56,17 @@ class Entry
|
||||
URI.encode display_name
|
||||
end
|
||||
|
||||
def cover_url
|
||||
url = "/api/page/#{@title_id}/#{@id}/1"
|
||||
TitleInfo.new @book.dir do |info|
|
||||
info_url = info.entry_cover_url[@title]?
|
||||
unless info_url.nil? || info_url.empty?
|
||||
url = info_url
|
||||
end
|
||||
end
|
||||
url
|
||||
end
|
||||
|
||||
def read_page(page_num)
|
||||
Zip::File.open @zip_path do |file|
|
||||
page = file.entries
|
||||
@@ -132,6 +142,7 @@ class Title
|
||||
json.field {{str}}, @{{str.id}}
|
||||
{% end %}
|
||||
json.field "display_name", display_name
|
||||
json.field "cover_url", cover_url
|
||||
json.field "mtime" { json.number @mtime.to_unix }
|
||||
json.field "titles" do
|
||||
json.raw self.titles.to_json
|
||||
@@ -190,9 +201,12 @@ class Title
|
||||
end
|
||||
|
||||
def display_name
|
||||
info = TitleInfo.new @dir
|
||||
dn = info.display_name
|
||||
dn.empty? ? @title : dn
|
||||
dn = @title
|
||||
TitleInfo.new @dir do |info|
|
||||
info_dn = info.display_name
|
||||
dn = info_dn unless info_dn.empty?
|
||||
end
|
||||
dn
|
||||
end
|
||||
|
||||
def encoded_display_name
|
||||
@@ -200,48 +214,80 @@ class Title
|
||||
end
|
||||
|
||||
def display_name(entry_name)
|
||||
info = TitleInfo.new @dir
|
||||
dn = info.entry_display_name[entry_name]?
|
||||
unless dn.nil? || dn.empty?
|
||||
return dn
|
||||
dn = entry_name
|
||||
TitleInfo.new @dir do |info|
|
||||
info_dn = info.entry_display_name[entry_name]?
|
||||
unless info_dn.nil? || info_dn.empty?
|
||||
dn = info_dn
|
||||
end
|
||||
end
|
||||
entry_name
|
||||
dn
|
||||
end
|
||||
|
||||
def set_display_name(dn)
|
||||
info = TitleInfo.new @dir
|
||||
info.display_name = dn
|
||||
info.save
|
||||
TitleInfo.new @dir do |info|
|
||||
info.display_name = dn
|
||||
info.save
|
||||
end
|
||||
end
|
||||
|
||||
def set_display_name(entry_name : String, dn)
|
||||
info = TitleInfo.new @dir
|
||||
info.entry_display_name[entry_name] = dn
|
||||
info.save
|
||||
TitleInfo.new @dir do |info|
|
||||
info.entry_display_name[entry_name] = dn
|
||||
info.save
|
||||
end
|
||||
end
|
||||
|
||||
def cover_url
|
||||
url = "img/icon.png"
|
||||
if @entries.size > 0
|
||||
url = @entries[0].cover_url
|
||||
end
|
||||
TitleInfo.new @dir do |info|
|
||||
info_url = info.cover_url
|
||||
unless info_url.nil? || info_url.empty?
|
||||
url = info_url
|
||||
end
|
||||
end
|
||||
url
|
||||
end
|
||||
|
||||
def set_cover_url(url : String)
|
||||
TitleInfo.new @dir do |info|
|
||||
info.cover_url = url
|
||||
info.save
|
||||
end
|
||||
end
|
||||
|
||||
def set_cover_url(entry_name : String, url : String)
|
||||
TitleInfo.new @dir do |info|
|
||||
info.entry_cover_url[entry_name] = url
|
||||
info.save
|
||||
end
|
||||
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}
|
||||
TitleInfo.new @dir do |info|
|
||||
if info.progress[username]?.nil?
|
||||
info.progress[username] = {entry => page}
|
||||
else
|
||||
info.progress[username][entry] = page
|
||||
end
|
||||
info.save
|
||||
return
|
||||
end
|
||||
info.progress[username][entry] = page
|
||||
info.save
|
||||
end
|
||||
|
||||
def load_progress(username, entry)
|
||||
info = TitleInfo.new @dir
|
||||
if info.progress[username]?.nil?
|
||||
return 0
|
||||
progress = 0
|
||||
TitleInfo.new @dir do |info|
|
||||
unless info.progress[username]?.nil? ||
|
||||
info.progress[username][entry]?.nil?
|
||||
progress = info.progress[username][entry]
|
||||
end
|
||||
end
|
||||
if info.progress[username][entry]?.nil?
|
||||
return 0
|
||||
end
|
||||
info.progress[username][entry]
|
||||
progress
|
||||
end
|
||||
|
||||
def load_percetage(username, entry)
|
||||
@@ -272,22 +318,32 @@ class TitleInfo
|
||||
include JSON::Serializable
|
||||
|
||||
property comment = "Generated by Mango. DO NOT EDIT!"
|
||||
# { 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
|
||||
property cover_url = ""
|
||||
property entry_cover_url = {} of String => String
|
||||
|
||||
@[JSON::Field(ignore: true)]
|
||||
property dir : String = ""
|
||||
|
||||
def initialize(@dir)
|
||||
json_path = File.join @dir, "info.json"
|
||||
if File.exists? json_path
|
||||
info = TitleInfo.from_json File.read json_path
|
||||
@progress = info.progress.clone
|
||||
@display_name = info.display_name
|
||||
@entry_display_name = info.entry_display_name.clone
|
||||
@@mutex_hash = {} of String => Mutex
|
||||
|
||||
def self.new(dir, &)
|
||||
if @@mutex_hash[dir]?
|
||||
mutex = @@mutex_hash[dir]
|
||||
else
|
||||
mutex = Mutex.new
|
||||
@@mutex_hash[dir] = mutex
|
||||
end
|
||||
mutex.synchronize do
|
||||
instance = TitleInfo.allocate
|
||||
json_path = File.join dir, "info.json"
|
||||
if File.exists? json_path
|
||||
instance = TitleInfo.from_json File.read json_path
|
||||
end
|
||||
instance.dir = dir
|
||||
yield instance
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user