From 75661abd5985b82d2a34f2b15d9859b3954a91d7 Mon Sep 17 00:00:00 2001 From: Glen Sawyer Date: Sat, 10 Apr 2021 12:07:24 -0600 Subject: [PATCH] Calibre - pull files from device (#7492) Add a proper implementation of `GET_BOOK_FILE_SEGMENT`, so calibre is now able to pull files from the device. --- plugins/calibre.koplugin/wireless.lua | 28 ++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/plugins/calibre.koplugin/wireless.lua b/plugins/calibre.koplugin/wireless.lua index 671b69fed..d0af782fa 100644 --- a/plugins/calibre.koplugin/wireless.lua +++ b/plugins/calibre.koplugin/wireless.lua @@ -13,6 +13,7 @@ local InputDialog = require("ui/widget/inputdialog") local InfoMessage = require("ui/widget/infomessage") local NetworkMgr = require("ui/network/manager") local UIManager = require("ui/uimanager") +local lfs = require("libs/libkoreader-lfs") local logger = require("logger") local rapidjson = require("rapidjson") local sha = require("ffi/sha2") @@ -644,9 +645,30 @@ end function CalibreWireless:sendToCalibre(arg) logger.dbg("GET_BOOK_FILE_SEGMENT", arg) - -- not implemented yet, we just send an invalid opcode to raise a control error in calibre. - -- If we don't do this calibre will wait *a lot* for the file(s) - self:sendJsonData('NOOP', {}) + local inbox_dir = G_reader_settings:readSetting("inbox_dir") + local path = inbox_dir .. "/" .. arg.lpath + + local file_size = lfs.attributes(path, "size") + if not file_size then + self:sendJsonData("NOOP", {}) + return + end + + local file = io.open(path, "rb") + if not file then + self:sendJsonData("NOOP", {}) + return + end + + self:sendJsonData("OK", { fileLength = file_size }) + + while true do + local data = file:read(4096) + if not data then break end + self.calibre_socket:send(data) + end + + file:close() end function CalibreWireless:isCalibreAtLeast(x,y,z)