Created
January 2, 2016 20:24
-
-
Save kardolus/68e56a77c5dc6c089153 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using libraries from http://w3.impa.br/~diego/software/luasocket/old/luasocket-2.0-beta/ftp.html