Skip to content

Instantly share code, notes, and snippets.

@kardolus
Created January 2, 2016 20:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kardolus/68e56a77c5dc6c089153 to your computer and use it in GitHub Desktop.
Save kardolus/68e56a77c5dc6c089153 to your computer and use it in GitHub Desktop.
--[[
URLs MUST conform to RFC 1738, that is, an URL is a string in the form:
[ftp://][<user>[:<password>]@]<host>[:<port>][/<path>][type=a|i]
--]]
local ftp = require("socket.ftp")
local ltn12 = require("ltn12")
local url = require("socket.url")
local socket = require("socket")
local pretty = require("pl.pretty")
print(socket._VERSION)
print(url._VERSION)
-- List files in a remote directory
local function list(u)
local t = {}
local p = url.parse(u)
p.command = "nlst"
p.argument = string.gsub(p.path, "^/", "")
if p.argument == "" then p.argument = nil end
p.sink = ltn12.sink.table(t)
local r, e = ftp.get(p)
pretty.dump(t)
return r and table.concat(t), e
end
-- Delete a remote file
local function delete(u)
local p = url.parse(u)
p.command = "dele"
p.argument = string.gsub(p.path, "^/", "")
if p.argument == "" then p.argument = nil end
p.check = 250
return ftp.command(p)
end
-- Upload a file
function upload(remotePath, localFile)
local p = url.parse(remotePath)
p.command = "appe"
p.argument = string.gsub(p.path, "^/", "")
if p.argument == "" then p.argument = nil end
p.check = 250
p.source = ltn12.source.file(io.open(localFile, "r"))
return ftp.command(p)
end
-- Retrieve a remote file
function download(u)
local t = {}
local p = url.parse(u)
return ftp.get(p)
end
@kardolus
Copy link
Author

kardolus commented Jan 2, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment