mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-03 11:25:29 -04:00
Use json builder in src/library.cr
instead of json mapping
This commit is contained in:
parent
8c7ced87f1
commit
82fb45b242
@ -14,10 +14,10 @@ struct Image
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Entry
|
class Entry
|
||||||
JSON.mapping zip_path: String, book_title: String, title: String,
|
property zip_path : String, book_title : String, 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, @title_id, storage)
|
||||||
@zip_path = path
|
@zip_path = path
|
||||||
@ -37,6 +37,19 @@ class Entry
|
|||||||
@cover_url = "/api/page/#{@title_id}/#{@id}/1"
|
@cover_url = "/api/page/#{@title_id}/#{@id}/1"
|
||||||
@mtime = File.info(@zip_path).modification_time
|
@mtime = File.info(@zip_path).modification_time
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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"] %}
|
||||||
|
json.field {{str}}, @{{str.id}}
|
||||||
|
{% end %}
|
||||||
|
json.field "pages" {json.number @pages}
|
||||||
|
json.field "mtime" {json.number @mtime.to_unix}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def read_page(page_num)
|
def read_page(page_num)
|
||||||
Zip::File.open @zip_path do |file|
|
Zip::File.open @zip_path do |file|
|
||||||
page = file.entries
|
page = file.entries
|
||||||
@ -60,9 +73,10 @@ class Entry
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Title
|
class Title
|
||||||
JSON.mapping dir: String, titles: Array(Title), entries: Array(Entry),
|
property dir : String, titles : Array(Title), entries : Array(Entry),
|
||||||
title: String, id: String, encoded_title: String, mtime: Time,
|
title : String, id : String, encoded_title : String, mtime : Time,
|
||||||
logger: MLogger
|
logger : MLogger
|
||||||
|
|
||||||
|
|
||||||
def initialize(dir : String, storage, @logger : MLogger)
|
def initialize(dir : String, storage, @logger : MLogger)
|
||||||
@dir = dir
|
@dir = dir
|
||||||
@ -96,6 +110,22 @@ class Title
|
|||||||
mtimes += @titles.map{|e| e.mtime}
|
mtimes += @titles.map{|e| e.mtime}
|
||||||
@mtime = mtimes.max
|
@mtime = mtimes.max
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def to_json(json : JSON::Builder)
|
||||||
|
json.object do
|
||||||
|
{% for str in ["dir", "title", "id", "encoded_title"] %}
|
||||||
|
json.field {{str}}, @{{str.id}}
|
||||||
|
{% end %}
|
||||||
|
json.field "mtime" {json.number @mtime.to_unix}
|
||||||
|
json.field "titles" do
|
||||||
|
json.raw @titles.to_json
|
||||||
|
end
|
||||||
|
json.field "entries" do
|
||||||
|
json.raw @entries.to_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# When downloading from MangaDex, the zip/cbz file would not be valid
|
# When downloading from MangaDex, the zip/cbz file would not be valid
|
||||||
# before the download is completed. If we scan the zip file,
|
# before the download is completed. If we scan the zip file,
|
||||||
# Entry.new would throw, so we use this method to check before
|
# Entry.new would throw, so we use this method to check before
|
||||||
@ -187,8 +217,8 @@ class TitleInfo
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Library
|
class Library
|
||||||
JSON.mapping dir: String, titles: Array(Title), scan_interval: Int32,
|
property dir : String, titles : Array(Title), scan_interval : Int32,
|
||||||
logger: MLogger, storage: Storage
|
logger : MLogger, storage : Storage
|
||||||
|
|
||||||
def initialize(@dir, @scan_interval, @logger, @storage)
|
def initialize(@dir, @scan_interval, @logger, @storage)
|
||||||
# explicitly initialize @titles to bypass the compiler check. it will
|
# explicitly initialize @titles to bypass the compiler check. it will
|
||||||
@ -206,6 +236,14 @@ class Library
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
def to_json(json : JSON::Builder)
|
||||||
|
json.object do
|
||||||
|
json.field "dir", @dir
|
||||||
|
json.field "titles" do
|
||||||
|
json.raw @titles.to_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
def get_title(tid)
|
def get_title(tid)
|
||||||
# top level
|
# top level
|
||||||
title = @titles.find { |t| t.id == tid }
|
title = @titles.find { |t| t.id == tid }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user