Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send2Ebook (receiver) plugin #3681

Merged
merged 27 commits into from Feb 22, 2018
Merged
Changes from 11 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0bea043
Sent-to-kindle init version
Feb 4, 2018
d7cd972
Merge remote-tracking branch 'upstream/master' into send-to-koreader-…
Feb 4, 2018
f8207fd
changes, fixes
Feb 5, 2018
e6e53f7
Merge remote-tracking branch 'upstream/master' into send-to-koreader-…
Feb 5, 2018
4da445b
externalized ftp config file
Feb 15, 2018
603246b
fix crash when wifi was off before download & auto turn on wifi is en…
Feb 15, 2018
e943548
lua check fix
Feb 15, 2018
6ecd191
"Remove read articles" instead "remove all"
Feb 16, 2018
562cb6c
code review fixes
Feb 16, 2018
2bd0c3c
fixes
Feb 16, 2018
2431835
fix removing read files sdr dir
Feb 16, 2018
33baf46
fix name, code review change
Feb 17, 2018
7a71f52
UI for ftp connection
Feb 17, 2018
5462200
fix crash
Feb 17, 2018
a39e123
help text update
Feb 17, 2018
c5fd4e7
Clarification, better label
mwoz123 Feb 17, 2018
74439c9
minor fixes (fixed names etc)
Feb 18, 2018
5a53c99
Merge branch 'send-to-koreader-receiver' of https://github.com/mwoz12…
Feb 18, 2018
67c21b3
remove anonim functions
Feb 18, 2018
905de0a
Lower case folder name
mwoz123 Feb 18, 2018
f45a46a
workaround no init ftp settings error.
Feb 22, 2018
17a9ff1
Merge branch 'send-to-koreader-receiver' of https://github.com/mwoz12…
Feb 22, 2018
caadd1c
Merge remote-tracking branch 'upstream/master' into send-to-koreader-…
Feb 22, 2018
ab7ee80
ftp.lua include order
Frenzie Feb 22, 2018
a778d9a
stray newline
Frenzie Feb 22, 2018
d47ae54
code review fixes + other minors
Feb 22, 2018
12f6cff
Merge branch 'send-to-koreader-receiver' of https://github.com/mwoz12…
Feb 22, 2018
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+254 −17
Diff settings

Always

Just for now

@@ -8,30 +8,18 @@ local UIManager = require("ui/uimanager")
local util = require("util")
local _ = require("gettext")
local T = require("ffi/util").template
local logger = require("logger")

local Ftp = {
}
local function generateUrl(address, user, pass)
local colon_sign = ""
local at_sign = ""
if user ~= "" then
at_sign = "@"
end
if pass ~= "" then
colon_sign = ":"
end
local replace = "://" .. user .. colon_sign .. pass .. at_sign
local url = string.gsub(address, "://", replace)
return url
end
local Ftp = {}

function Ftp:run(address, user, pass, path)
local url = generateUrl(address, user, pass) .. path
local url = FtpApi:generateUrl(address, user, pass) .. path
return FtpApi:listFolder(url, path)
end

function Ftp:downloadFile(item, address, user, pass, path, close)
local url = generateUrl(address, user, pass) .. item.url
local url = FtpApi:generateUrl(address, user, pass) .. item.url
logger.dbg("downloadFile url", url)
local response = FtpApi:downloadFile(url)
if response ~= nil then
path = util.fixUtf8(path, "_")
@@ -6,6 +6,20 @@ local url = require("socket.url")
local FtpApi = {
}

function FtpApi:generateUrl(address, user, pass)
local colon_sign = ""
local at_sign = ""
if user ~= "" then
at_sign = "@"
end
if pass ~= "" then
colon_sign = ":"
end
local replace = "://" .. user .. colon_sign .. pass .. at_sign
local generated_url = string.gsub(address, "://", replace)
return generated_url
end

function FtpApi:nlst(u)
local t = {}
local p = url.parse(u)
@@ -69,4 +83,13 @@ function FtpApi:downloadFile(file_path)
return ftp.get(file_path ..";type=i")
end

function FtpApi:delete(file_path)
local p = url.parse(file_path)
p.argument = string.gsub(p.path, "^/", "")
p.command = "dele"
p.check = 250
return ftp.command(p)
end


return FtpApi
@@ -37,6 +37,7 @@ local order = {
"cloud_storage",
"read_timer",
"news_downloader",
"send_to_koreader",
"----------------------------",
"more_plugins",
"----------------------------",
@@ -58,6 +58,7 @@ local order = {
"progress_sync",
"zsync",
"news_downloader",
"send_to_koreader",
"----------------------------",
"more_plugins",
},
@@ -0,0 +1,10 @@
return {

--hint: host needs to start with 'ftp://' prefix otherwise won't work

host='ftp://server.com',
user='koreader',
password='secret',
folder='/ebooks/'

}
@@ -0,0 +1,214 @@
local DataStorage = require("datastorage")
local DocSettings = require("frontend/docsettings")
local ReadHistory = require("readhistory")
local FFIUtil = require("ffi/util")
local FtpApi = require("apps/cloudstorage/ftpapi")
local FtpConnectionConfig = require("ftpconnectionconfigfile")
local InfoMessage = require("ui/widget/infomessage")
local LuaSettings = require("frontend/luasettings")
local UIManager = require("ui/uimanager")
local NetworkMgr = require("ui/network/manager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local ffi = require("ffi")
local logger = require("logger")
local util = require("util")
local _ = require("gettext")
local T = FFIUtil.template

local Send2Ebook = WidgetContainer:new{}

local initialized = false
local wifi_enabled_before_action = true
local ftp_connection_config_file = "ftpconnectionconfig.lua"
local send_to_koreader_config_file = "send_to_koreader_settings.lua"
local config_key_custom_dl_dir = "custom_dl_dir";
local default_download_dir_name = "Send2Ebook"
local download_dir_path

local function stringEnds(str,suffix)
return suffix=="" or string.sub(str,-string.len(suffix))==suffix
end

function Send2Ebook:downloadFileAndRemove(connection_url, remote_path, local_download_path)
local url = connection_url .. remote_path
local response = FtpApi:downloadFile(url)

if response ~= nil then
local_download_path = util.fixUtf8(local_download_path, "_")
local file = io.open(local_download_path, "w")
file:write(response)
file:close()
FtpApi:delete(url)
return 1
else
logger.err("Error. Invalid connection data? ")
return 0
end
end


-- TODO: implement as NetworkMgr:afterWifiAction with configuration options
function Send2Ebook:afterWifiAction()
if not wifi_enabled_before_action then
NetworkMgr:promptWifiOff()
end
end

function Send2Ebook:init()
self.ui.menu:registerToMainMenu(self)
end

function Send2Ebook:addToMainMenu(menu_items)
self:lazyInitialization()
menu_items.send_to_koreader = {
text = _("Send2Ebook (Receiver)"),
sub_item_table = {
{
text = _("Download and remove from server"),
callback = function()
if not NetworkMgr:isOnline() then
wifi_enabled_before_action = false
NetworkMgr:beforeWifiAction(self.process)
else
self:process()
end
end,
},
{
text = _("Go to download folder"),
callback = function()
local FileManager = require("apps/filemanager/filemanager")
if FileManager.instance then
FileManager.instance:reinit(download_dir_path)
else
FileManager:showFiles(download_dir_path)
end
end,
},
{
text = _("Remove read articles"),
callback = function() self:removeReadActicles() end,
},
{
text = _("Set custom download directory"),
callback = function() self:setCustomDownloadDirectory() end,
},
{
text = _("Help"),
callback = function()
UIManager:show(InfoMessage:new{
text = T(_('Send2Ebook lets you send articles found on PC/Android phone to your Ebook reader (using ftp server). More details: https://github.com/koreader/android-send2ebook\n Configuration - please manually edit: "ftpconnectionconfig.lua". Downloads to local folder:', download_dir_path),
download_dir_path)
})
end,
},
},
}
end

function Send2Ebook:lazyInitialization()
if not initialized then
logger.dbg("Send2Ebook: obtaining download folder")
local send_to_koreader_settings = LuaSettings:open(("%s/%s"):format(DataStorage:getSettingsDir(), send_to_koreader_config_file))
if send_to_koreader_settings:has(config_key_custom_dl_dir) then
download_dir_path = send_to_koreader_settings:readSetting(config_key_custom_dl_dir)
else
download_dir_path = ("%s/%s/"):format(DataStorage:getFullDataDir(), default_download_dir_name)
end

if not lfs.attributes(download_dir_path, "mode") then
logger.dbg("Send2Ebook: Creating initial directory")
lfs.mkdir(download_dir_path)
end
end
end

function Send2Ebook:process()
local info = InfoMessage:new{ text = _("Connecting …") }
UIManager:show(info)
logger.dbg("Send2Ebook: force repaint due to upcoming blocking calls")
UIManager:forceRePaint()
UIManager:close(info)

local count = 1
local connection_url = FtpApi:generateUrl(FtpConnectionConfig.host, FtpConnectionConfig.user, FtpConnectionConfig.password)

local fileTable = FtpApi:listFolder(connection_url .. FtpConnectionConfig.folder, FtpConnectionConfig.folder) --args looks strange but otherwise resonse with invalid paths

if not fileTable then
info = InfoMessage:new{ text = T(_("Could not get file list for server: %1, user: %2, folder: %3"), FtpConnectionConfig.host, FtpConnectionConfig.user, FtpConnectionConfig.folder) }

This comment has been minimized.

Copy link
@Frenzie

Frenzie Feb 22, 2018

Member

indentation

else
local total_entries = table.getn(fileTable)
logger.dbg("total_entries ", total_entries)
if total_entries > 1 then total_entries = total_entries -2 end --remove result "../" (upper folder) and "./" (current folder)
for idx, ftpFile in ipairs(fileTable) do

This comment has been minimized.

Copy link
@Frenzie

Frenzie Feb 22, 2018

Member

ftp_file is also not a function

logger.dbg("ftpFile ", ftpFile)
if ftpFile["type"] == "file" and stringEnds(ftpFile["text"], ".epub") then

info = InfoMessage:new{ text = T(_("Processing %1/%2"), count, total_entries) }
UIManager:show(info)
UIManager:forceRePaint()
UIManager:close(info)

local remote_file_path = ftpFile["url"]
logger.dbg("remote_file_path", remote_file_path)
local local_file_path = download_dir_path .. ftpFile["text"]
count = count + Send2Ebook:downloadFileAndRemove(connection_url, remote_file_path, local_file_path)
end
info = InfoMessage:new{ text = T(_("Processing finished. Success: %1"), count -1) }
end
end
UIManager:show(info)
Send2Ebook:afterWifiAction()
end

function Send2Ebook:removeReadActicles()
logger.dbg("Send2Ebook: Removing read articles from :", download_dir_path)
for entry in lfs.dir(download_dir_path) do
if entry ~= "." and entry ~= ".." and entry ~= ftp_connection_config_file then
local entry_path = download_dir_path .. entry
if DocSettings:hasSidecarFile(entry_path) then

This comment has been minimized.

Copy link
@mwoz123

mwoz123 Feb 18, 2018

Author Contributor

Does this pr https://github.com/koreader/koreader/pull/3675/files
Make any influence to above check? Cause after merge master it doesn't seem to work correctly (on Inkbook), which it did on emulator (and old master)

This comment has been minimized.

Copy link
@mwoz123

mwoz123 Feb 18, 2018

Author Contributor

Maybe I should use something else here?

This comment has been minimized.

Copy link
@Frenzie

Frenzie Feb 22, 2018

Member

No, it's the same way files are displayed read or unread in the filemanager. What are the steps?

Edit: sorry, ignore this one.

This comment has been minimized.

Copy link
@mwoz123

mwoz123 Feb 22, 2018

Author Contributor

It seems to be working fine on emulator, after todays master merge

local entry_mode = lfs.attributes(entry_path, "mode")
if entry_mode == "file" then
ffi.C.remove(entry_path)
local sdr_dir = DocSettings:getSidecarDir(entry_path)
logger.dbg("Send2Ebook: sdr dir to be removed:", sdr_dir)
FFIUtil.purgeDir(sdr_dir)
end
end
end
end
UIManager:show(InfoMessage:new{
text = _("All read articles removed.")
})
end

function Send2Ebook:setCustomDownloadDirectory()
UIManager:show(InfoMessage:new{
text = _("To select a folder press down and hold it for 1 second.")
})
require("ui/downloadmgr"):new{
title = _("Choose download directory"),
onConfirm = function(path)
logger.dbg("Send2Ebook: set download directory to: ", path)
local send_to_koreader_settings = LuaSettings:open(("%s/%s"):format(DataStorage:getSettingsDir(), send_to_koreader_config_file))
send_to_koreader_settings:saveSetting(config_key_custom_dl_dir, ("%s/"):format(path))
send_to_koreader_settings:flush()

initialized = false
self:lazyInitialization()
end,
}:chooseDir()
end

function Send2Ebook:onCloseDocument()
local document_full_path = self.ui.document.file
if document_full_path and download_dir_path == string.sub(document_full_path, 1, string.len(download_dir_path)) then
logger.dbg("Send2Ebook: document_full_path:", document_full_path)
logger.dbg("Send2Ebook: download_dir_path:", download_dir_path)
logger.dbg("Send2Ebook: removing Send2Ebook file from history.")
ReadHistory:removeItemByPath(document_full_path)
end
end

return Send2Ebook
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.