- implement login and load images from zip

This commit is contained in:
Alex Ling
2020-02-12 03:47:10 +00:00
parent a3203dccba
commit cc031c9aef
5 changed files with 148 additions and 14 deletions

View File

@@ -1,4 +1,14 @@
require "zip"
require "mime"
class Image
property data : Bytes
property mime : String
property filename : String
property size : Int32
def initialize(@data, @mime, @filename, @size)
end
end
class Entry
property zip_path : String
@@ -10,6 +20,29 @@ class Entry
@title = File.basename path, ".zip"
@size = (File.size path).humanize_bytes
end
def read_page(page_num)
Zip::File.open @zip_path do |file|
page = file.entries
.select { |e|
["image/jpeg", "image/png"].includes? \
MIME.from_filename? e.filename
}
.sort { |a, b| a.filename <=> b.filename }
.[page_num]
page.open do |io|
slice = Bytes.new page.uncompressed_size
bytes_read = io.read_fully? slice
unless bytes_read
return nil
end
return Image.new slice, MIME.from_filename(page.filename),\
page.filename, bytes_read
end
end
end
def get_cover()
read_page 0
end
end
class Title
@@ -25,6 +58,9 @@ class Title
.map { |path| Entry.new File.join dir, path }
.sort { |a, b| a.title <=> b.title }
end
def get_cover()
@entries[0].get_cover
end
end
class Library