CloudStorage (FTP): Do not buffer entire download in memory (#7597)

pull/7599/head
Andy Bao 3 years ago committed by GitHub
parent 5d312d85a0
commit ea6576d2b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,18 +22,16 @@ end
function Ftp:downloadFile(item, address, user, pass, path, close)
local url = FtpApi:generateUrl(address, util.urlEncode(user), util.urlEncode(pass)) .. item.url
logger.dbg("downloadFile url", url)
local response = FtpApi:ftpGet(url, "retr")
path = util.fixUtf8(path, "_")
local file, err = io.open(path, "w")
if not file then
UIManager:show(InfoMessage:new{
text = T(_("Could not save file to %1:\n%2"), BD.filepath(path), err),
})
return
end
local response = FtpApi:ftpGet(url, "retr", ltn12.sink.file(file))
if response ~= nil then
path = util.fixUtf8(path, "_")
local file, err = io.open(path, "w")
if not file then
UIManager:show(InfoMessage:new{
text = T(_("Could not save file to %1:\n%2"), BD.filepath(path), err),
})
return
end
file:write(response)
file:close()
local __, filename = util.splitFilePathName(path)
if G_reader_settings:isTrue("show_unsupported") and not DocumentRegistry:hasProvider(filename) then
UIManager:show(InfoMessage:new{

@ -20,16 +20,15 @@ function FtpApi:generateUrl(address, user, pass)
return generated_url
end
function FtpApi:ftpGet(u, command)
local t = {}
function FtpApi:ftpGet(u, command, sink)
local p = url.parse(u)
p.user = util.urlDecode(p.user)
p.password = util.urlDecode(p.password)
p.command = command
p.sink = ltn12.sink.table(t)
p.sink = sink
p.type = "i" -- binary
local r, e = ftp.get(p)
return r and table.concat(t), e
return r, e
end
function FtpApi:listFolder(address_path, folder_path)
@ -38,12 +37,14 @@ function FtpApi:listFolder(address_path, folder_path)
local type
local extension
local file_name
local ls_ftp = self:ftpGet(address_path, "nlst")
local tbl = {}
local sink = ltn12.sink.table(tbl)
local ls_ftp = self:ftpGet(address_path, "nlst", sink)
if ls_ftp == nil then return false end
if folder_path == "/" then
folder_path = ""
end
for item in (ls_ftp..'\n'):gmatch'(.-)\r?\n' do
for item in (table.concat(tbl)..'\n'):gmatch'(.-)\r?\n' do
if item ~= '' then
file_name = item:match("([^/]+)$")
extension = item:match("^.+(%..+)$")

Loading…
Cancel
Save