2017-04-02 22:06:59 +00:00
|
|
|
local DocumentRegistry = require("document/documentregistry")
|
2016-12-20 07:34:00 +00:00
|
|
|
local ftp = require("socket.ftp")
|
|
|
|
local ltn12 = require("ltn12")
|
2018-05-04 15:06:58 +00:00
|
|
|
local util = require("util")
|
2016-12-20 07:34:00 +00:00
|
|
|
local url = require("socket.url")
|
|
|
|
|
|
|
|
local FtpApi = {
|
|
|
|
}
|
|
|
|
|
2018-02-22 23:02:32 +00:00
|
|
|
function FtpApi:generateUrl(address, user, pass)
|
|
|
|
local colon_sign = ""
|
|
|
|
local at_sign = ""
|
|
|
|
if user ~= "" then
|
|
|
|
at_sign = "@"
|
|
|
|
end
|
|
|
|
if pass ~= "" then
|
|
|
|
colon_sign = ":"
|
|
|
|
end
|
2018-05-04 15:06:58 +00:00
|
|
|
local generated_url = "ftp://" .. user .. colon_sign .. pass .. at_sign .. address:gsub("ftp://", "")
|
2018-02-22 23:02:32 +00:00
|
|
|
return generated_url
|
|
|
|
end
|
|
|
|
|
2021-04-25 11:08:49 +00:00
|
|
|
function FtpApi:ftpGet(u, command, sink)
|
2016-12-20 07:34:00 +00:00
|
|
|
local p = url.parse(u)
|
2018-05-04 15:06:58 +00:00
|
|
|
p.user = util.urlDecode(p.user)
|
|
|
|
p.password = util.urlDecode(p.password)
|
|
|
|
p.command = command
|
2021-04-25 11:08:49 +00:00
|
|
|
p.sink = sink
|
2020-07-20 20:53:52 +00:00
|
|
|
p.type = "i" -- binary
|
2016-12-20 07:34:00 +00:00
|
|
|
local r, e = ftp.get(p)
|
2021-04-25 11:08:49 +00:00
|
|
|
return r, e
|
2016-12-20 07:34:00 +00:00
|
|
|
end
|
|
|
|
|
2018-05-04 15:06:58 +00:00
|
|
|
function FtpApi:listFolder(address_path, folder_path)
|
2016-12-20 07:34:00 +00:00
|
|
|
local ftp_list = {}
|
|
|
|
local ftp_file = {}
|
|
|
|
local type
|
|
|
|
local extension
|
|
|
|
local file_name
|
2021-04-25 11:08:49 +00:00
|
|
|
local tbl = {}
|
|
|
|
local sink = ltn12.sink.table(tbl)
|
2021-05-16 10:46:26 +00:00
|
|
|
local ls_ftp, e = self:ftpGet(address_path, "nlst", sink)
|
|
|
|
if ls_ftp == nil then
|
|
|
|
return false, e
|
|
|
|
end
|
2017-05-20 21:21:49 +00:00
|
|
|
if folder_path == "/" then
|
|
|
|
folder_path = ""
|
|
|
|
end
|
2021-04-25 11:08:49 +00:00
|
|
|
for item in (table.concat(tbl)..'\n'):gmatch'(.-)\r?\n' do
|
2016-12-20 07:34:00 +00:00
|
|
|
if item ~= '' then
|
|
|
|
file_name = item:match("([^/]+)$")
|
|
|
|
extension = item:match("^.+(%..+)$")
|
|
|
|
if not extension then
|
|
|
|
type = "folder"
|
|
|
|
table.insert(ftp_list, {
|
|
|
|
text = file_name .. "/",
|
2017-05-20 21:21:49 +00:00
|
|
|
url = string.format("%s/%s",folder_path, file_name),
|
2016-12-20 07:34:00 +00:00
|
|
|
type = type,
|
|
|
|
})
|
|
|
|
--show only file with supported formats
|
2019-07-21 19:45:02 +00:00
|
|
|
elseif extension and (DocumentRegistry:hasProvider(item)
|
|
|
|
or G_reader_settings:isTrue("show_unsupported")) then
|
2016-12-20 07:34:00 +00:00
|
|
|
type = "file"
|
|
|
|
table.insert(ftp_file, {
|
|
|
|
text = file_name,
|
2017-05-20 21:21:49 +00:00
|
|
|
url = string.format("%s/%s",folder_path, file_name),
|
2016-12-20 07:34:00 +00:00
|
|
|
type = type,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
--sort
|
|
|
|
table.sort(ftp_list, function(v1,v2)
|
|
|
|
return v1.text < v2.text
|
|
|
|
end)
|
|
|
|
table.sort(ftp_file, function(v1,v2)
|
|
|
|
return v1.text < v2.text
|
|
|
|
end)
|
|
|
|
for _, files in ipairs(ftp_file) do
|
|
|
|
table.insert(ftp_list, {
|
|
|
|
text = files.text,
|
|
|
|
url = files.url,
|
|
|
|
type = files.type
|
|
|
|
})
|
|
|
|
end
|
|
|
|
return ftp_list
|
|
|
|
end
|
|
|
|
|
2018-02-22 23:02:32 +00:00
|
|
|
function FtpApi:delete(file_path)
|
|
|
|
local p = url.parse(file_path)
|
2018-05-04 15:06:58 +00:00
|
|
|
p.user = util.urlDecode(p.user)
|
|
|
|
p.password = util.urlDecode(p.password)
|
2018-02-22 23:02:32 +00:00
|
|
|
p.argument = string.gsub(p.path, "^/", "")
|
|
|
|
p.command = "dele"
|
|
|
|
p.check = 250
|
|
|
|
return ftp.command(p)
|
|
|
|
end
|
|
|
|
|
2016-12-20 07:34:00 +00:00
|
|
|
return FtpApi
|