From 968c2f4ad5b53866c62deec7a7f37099c514d4da Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Fri, 23 Oct 2020 12:29:20 +0000 Subject: [PATCH] Update DB to save thumbnails --- src/library/types.cr | 10 ++++++++++ src/storage.cr | 29 ++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/library/types.cr b/src/library/types.cr index b51671b..4e83135 100644 --- a/src/library/types.cr +++ b/src/library/types.cr @@ -57,6 +57,16 @@ struct Image def initialize(@data, @mime, @filename, @size) end + + def self.from_db(res : DB::ResultSet) + img = Image.allocate + res.read String + img.data = res.read Bytes + img.filename = res.read String + img.mime = res.read String + img.size = res.read Int32 + img + end end class TitleInfo diff --git a/src/storage.cr b/src/storage.cr index afbba8d..90646be 100644 --- a/src/storage.cr +++ b/src/storage.cr @@ -35,9 +35,11 @@ class Storage MainFiber.run do DB.open "sqlite3://#{@path}" do |db| begin - # We create the `ids` table first. even if the uses has an - # early version installed and has the `user` table only, - # we will still be able to create `ids` + db.exec "create table thumbnails " \ + "(id text, data blob, filename text, " \ + "mime text, size integer)" + db.exec "create unique index tn_index on thumbnails (id)" + db.exec "create table ids" \ "(path text, id text, is_title integer)" db.exec "create unique index path_idx on ids (path)" @@ -243,6 +245,27 @@ class Storage end end + def save_thumbnail(id : String, img : Image) + MainFiber.run do + get_db do |db| + db.exec "insert into thumbnails values (?, ?, ?, ?, ?)", id, img.data, + img.filename, img.mime, img.size + end + end + end + + def get_thumbnail(id : String) : Image? + img = nil + MainFiber.run do + get_db do |db| + db.query_one? "select * from thumbnails where id = (?)", id do |res| + img = Image.from_db res + end + end + end + img + end + def close MainFiber.run do unless @db.nil?