Lua: Retrieve partial ftp file at specific range


Read more: https://tools.ietf.org/id/draft-bryan-ftp-range-03.html

We use extension library luasocket

Edit file: https://github.com/lunarmodules/luasocket/blob/master/src/ftp.lua
Local location: /usr/share/lua/5.1/socket/ftp.lua

Add function:

function metat.__index:rang(rang)	 
    self.try(self.tp:command("rang", rang))
    self.try(self.tp:check(350))
    return 1
end

Edit function tget , add line if gett.rang then f:rang(gett.rang) end:

local function tget(gett)
    gett = override(gett)
    socket.try(gett.host, "missing hostname")
    local f = _M.open(gett.host, gett.port, gett.create)
    f:greet()
    f:login(gett.user, gett.password)
    if gett.type then f:type(gett.type) end
	if gett.rang then f:rang(gett.rang) end --added 4/22/2022
    f:epsv()
    f:receive(gett)
    f:quit()
    return f:close()
end

Code lua:

local ftp = require("socket.ftp")
local ltn12 = require("ltn12")
local url = require("socket.url")
 
-- Retrieve a remote file
function download(u,rang)
   local t = {}
   local p = url.parse(u)
   p.command = "retr"
   p.type = "i"
   p.sink = ltn12.sink.table(t)
   p.argument = string.gsub(p.path, "^/", "")
   if p.argument == "" then p.argument = nil end
   p.check = 250
   if rang then
      p.rang = rang
   end
   ftp.get(p)
   return table.concat(t,'')
end

Example:

local s,r = download("ftp://user:pass@my.your-storagebox.de/file.mp4","10 1000")
print(s)

Read more about Filters Sources And Sinks

Read more: Lua: Retrieve partial ftp file at specific range – Method 2

1 Comment

Leave a Reply