upload dir

This commit is contained in:
cdricms
2025-03-12 23:57:33 +01:00
parent 9bc332b833
commit 9ec1860ca7
3 changed files with 199 additions and 116 deletions

View File

@@ -3,42 +3,46 @@ local cli = require("nvim-sync-ftp.cli")
local M = {} local M = {}
function M.setup(opts) function M.setup(opts)
local config = cli.getConfig()
local config = cli.getConfig() vim.api.nvim_create_user_command("SyncFtpMapToRemote", function(params)
cli.MapToRemote(params)
end, {
force = true,
nargs = '*',
range = true,
})
vim.api.nvim_create_user_command("SyncFtpMapToRemote", function (params) vim.api.nvim_create_user_command("SyncFtpUpload", function(params)
cli.MapToRemote(params) cli.UploadFile()
end,{ end, {
force = true, force = true,
nargs = '*', nargs = '*',
range = true, range = true,
}) })
vim.api.nvim_create_user_command("SyncFtpUpload", function(params) vim.api.nvim_create_user_command("SyncFtpUploadDir", function(params)
cli.Upload() cli.UploadDir()
end,{ end, {
force = true, force = true,
nargs = '*', nargs = '*',
range = true, range = true,
}) })
if next(config) ~= nil then if next(config) ~= nil then
-- Config for on save -- Config for on save
if config.upload_to_save == 'true' then if config.upload_to_save == 'true' then
vim.api.nvim_create_augroup("SyncFtpUploadGroup", { clear = true }) vim.api.nvim_create_augroup("SyncFtpUploadGroup", { clear = true })
vim.api.nvim_create_autocmd("BufWritePost", {
group = "SyncFtpUploadGroup",
pattern = "*.*",
callback = function(args)
cli.Upload()
end
})
end
end
vim.api.nvim_create_autocmd("BufWritePost", {
group = "SyncFtpUploadGroup",
pattern = "*.*",
callback = function(args)
cli.Upload()
end
})
end
end
end end
return M return M

View File

@@ -5,120 +5,199 @@ local config = {}
local M = {} local M = {}
local function path_components(path)
-- Remove trailing slashes
path = path:gsub("/+$", "")
-- Extract the directory (dirname)
local dir = path:match("^(.*)/[^/]*$") or "."
if dir == "" then dir = "/" end -- Handle edge case for root-level paths like "/file.txt"
-- Extract the basename
local base = path:match("[^/]+$") or path
if base == "" then base = "/" end -- Handle edge case for "/" or empty path
-- Extract the parent directory (one level up from dir)
local parent
if dir == "." then
parent = ".." -- Parent of current directory
elseif dir == "/" then
parent = "/" -- Root's parent is itself
else
parent = dir:match("^(.*)/[^/]*$") or "/"
if parent == "" then parent = "/" end -- Handle edge case for "/dir"
end
return {
dir = dir, -- Directory containing the file (dirname)
base = base, -- Filename without directory (basename)
parent = parent -- Parent directory one level up from dir
}
end
local function dirname(str)
if str:match(".-/.-") then
local name = string.gsub(str, "(.*/)(.*)", "%1")
return name
else
return ''
end
end
local function file_exists() local function file_exists()
return vim.loop.fs_stat(filePath) ~= nil return vim.loop.fs_stat(filePath) ~= nil
end end
local function create_file() local function create_file()
local buf = vim.api.nvim_create_buf(true, true)
local buf = vim.api.nvim_create_buf(true, true) vim.api.nvim_buf_set_name(buf, filePath)
vim.api.nvim_buf_set_name(buf, filePath) vim.api.nvim_buf_set_lines(buf, 0, -1, false, {
"host hostName",
"user userName",
"password password",
"port 21",
"remote_path /remote/",
"upload_to_save false",
})
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { vim.api.nvim_set_option_value("buftype", "", { buf = buf })
"host hostName" ,
"user userName",
"password password",
"port 21",
"remote_path /remote/",
"upload_to_save false",
})
vim.api.nvim_buf_set_option(buf, "buftype", "") vim.api.nvim_buf_call(buf, function()
vim.cmd("write")
end)
vim.api.nvim_buf_call(buf, function() vim.api.nvim_win_set_buf(0, buf)
vim.cmd("write")
end)
vim.api.nvim_win_set_buf(0, buf)
message.success("File created successfully!!!")
message.success("File created successfully!!!")
end end
function M.MapToRemote () function M.MapToRemote()
if file_exists() then
if file_exists() then message.warn("File already exists!!!");
message.warn("File already exists!!!"); else
else create_file()
create_file() end
end
end end
local function read_file(path) local function read_file(path)
local file = io.open(path, "r") local file = io.open(path, "r")
if not file then if not file then
return nil, "Config file not found!" return nil, "Config file not found!"
end end
local lines = {} local lines = {}
for line in file:lines() do for line in file:lines() do
table.insert(lines, line) table.insert(lines, line)
end end
file:close() file:close()
return lines return lines
end end
function M.getConfig() function M.getConfig()
local configTemp = {};
local configTemp = {}; if file_exists() then
local content, err = read_file(filePath)
if file_exists() then if content then
local content, err = read_file(filePath) for _, line in ipairs(content) do
local first_word, second_word = line:match("^(%S+)%s+(%S+)$")
configTemp[first_word] = second_word
end
else
message(err);
end
end
if content then return configTemp
for i, line in ipairs(content) do
local first_word, second_word = line:match("^(%S+)%s+(%S+)$")
configTemp[first_word] = second_word
end
else
message(err);
end
end
return configTemp
end end
function M.Upload() function M.UploadFile()
config = M.getConfig()
local current_buffer_path = vim.api.nvim_buf_get_name(0)
local directoryTemp = directory
config = M.getConfig() message.warn("Send file ... Wait")
local current_buffer_path = vim.api.nvim_buf_get_name(0)
local directoryTemp = directory
message.warn("Send file ... Wait") if not directoryTemp:match("/$") then
directoryTemp = directoryTemp .. "/"
end
if not directoryTemp:match("/$") then local relative_path = current_buffer_path:sub(#directoryTemp + 1)
directoryTemp = directoryTemp .. "/"
end
local relative_path = current_buffer_path:sub(#directoryTemp + 1) if not config.remote_path:match("/$") then
config.remote_path = config.remote_path .. "/"
end
if not config.remote_path:match("/$") then local remotePath = config.remote_path .. relative_path
config.remote_path = config.remote_path .. "/"
end
local remotePath = config.remote_path .. relative_path print("remotePath:", remotePath)
print("current_buffer_path:", current_buffer_path)
local dir = dirname(remotePath)
local command = string.format(
"lftp -u '%s,%s' ftp://%s -e 'set ssl:verify-certificate no;mkdir -p %s; cd %s;put %s; quit' > /dev/null 2>&1",
config.user, config.password, config.host, dir, dir, current_buffer_path
)
print("Generated command:", command)
local command = string.format( vim.uv.spawn("sh", {
'curl -T "%s" ftp://%s/%s --user "%s:%s" > /dev/null 2>&1', args = { "-c", command },
current_buffer_path, config.host, remotePath, config.user, config.password) }, function(code, signal)
if code == 0 then
vim.schedule(function()
message.info("File uploaded successfully!")
end)
else
vim.schedule(function()
message.error("File upload failed with exit code: " .. code)
end)
end
end)
end
vim.loop.spawn("sh", { function M.UploadDir()
args = { "-c", command }, config = M.getConfig()
}, function(code, signal) local current_buffer_path = vim.api.nvim_buf_get_name(0)
if code == 0 then local directoryTemp = directory
vim.schedule(function()
message.info("File uploaded successfully!")
end)
else
vim.schedule(function()
message.error("File upload failed with exit code: " .. code)
end)
end
end)
message.warn("Send directory... Wait")
if not directoryTemp:match("/$") then
directoryTemp = directoryTemp .. "/"
end
local relative_path = current_buffer_path:sub(#directoryTemp + 1)
if not config.remote_path:match("/$") then
config.remote_path = config.remote_path .. "/"
end
local remotePath = config.remote_path .. relative_path
local remote = path_components(remotePath)
local localDir = dirname(current_buffer_path)
local command = string.format(
"lftp -u '%s,%s' ftp://%s -e 'set ssl:verify-certificate no;mkdir -p %q;cd %q;mirror -R %q; quit'",
config.user, config.password, config.host, remote.parent, remote.parent, localDir
)
print("Generated command:", command)
vim.uv.spawn("sh", {
args = { "-c", command },
}, function(code, signal)
if code == 0 then
vim.schedule(function()
message.info("Directory uploaded successfully!")
end)
else
vim.schedule(function()
message.error("Directory upload failed with exit code: " .. code)
end)
end
end)
end end
return M return M

View File

@@ -4,26 +4,26 @@ local M = {}
--- @type fun(fmt: string, ...: string) --- @type fun(fmt: string, ...: string)
M.warn = vim.schedule_wrap(function(fmt, ...) M.warn = vim.schedule_wrap(function(fmt, ...)
vim.notify(fmt:format(...), levels.WARN, { title = 'Sync FTP' }) vim.notify(fmt:format(...), levels.WARN, { title = 'Sync FTP' })
end) end)
--- @type fun(fmt: string, ...: string) --- @type fun(fmt: string, ...: string)
M.info = vim.schedule_wrap(function(fmt, ...) M.info = vim.schedule_wrap(function(fmt, ...)
vim.notify(fmt:format(...), vim.log.INFO, { title = 'Sync FTP' }) vim.notify(fmt:format(...), vim.log.INFO, { title = 'Sync FTP' })
end) end)
--- @type fun(fmt: string, ...: string) --- @type fun(fmt: string, ...: string)
M.error = vim.schedule_wrap(function(fmt, ...) M.error = vim.schedule_wrap(function(fmt, ...)
vim.notify(fmt:format(...), vim.log.levels.ERROR, { title = 'Sync FTP' }) vim.notify(fmt:format(...), vim.log.levels.ERROR, { title = 'Sync FTP' })
end) end)
--- @type fun(fmt: string, ...: string) --- @type fun(fmt: string, ...: string)
M.error_once = vim.schedule_wrap(function(fmt, ...) M.error_once = vim.schedule_wrap(function(fmt, ...)
vim.notify_once(fmt:format(...), vim.log.levels.ERROR, { title = 'Sync FTP' }) vim.notify_once(fmt:format(...), vim.log.levels.ERROR, { title = 'Sync FTP' })
end) end)
M.success = vim.schedule_wrap(function(fmt, ...) M.success = vim.schedule_wrap(function(fmt, ...)
vim.notify(fmt:format(...), vim.log.INFO, { title = 'Sync FTP'}) vim.notify(fmt:format(...), vim.log.INFO, { title = 'Sync FTP' })
end) end)
return M return M