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,20 +3,27 @@ local cli = require("nvim-sync-ftp.cli")
local M = {}
function M.setup(opts)
local config = cli.getConfig()
vim.api.nvim_create_user_command("SyncFtpMapToRemote", function (params)
vim.api.nvim_create_user_command("SyncFtpMapToRemote", function(params)
cli.MapToRemote(params)
end,{
end, {
force = true,
nargs = '*',
range = true,
})
vim.api.nvim_create_user_command("SyncFtpUpload", function(params)
cli.Upload()
end,{
cli.UploadFile()
end, {
force = true,
nargs = '*',
range = true,
})
vim.api.nvim_create_user_command("SyncFtpUploadDir", function(params)
cli.UploadDir()
end, {
force = true,
nargs = '*',
range = true,
@@ -35,10 +42,7 @@ function M.setup(opts)
end
})
end
end
end
return M

View File

@@ -5,18 +5,56 @@ local config = {}
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()
return vim.loop.fs_stat(filePath) ~= nil
end
local function create_file()
local buf = vim.api.nvim_create_buf(true, true)
vim.api.nvim_buf_set_name(buf, filePath)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {
"host hostName" ,
"host hostName",
"user userName",
"password password",
"port 21",
@@ -24,7 +62,7 @@ local function create_file()
"upload_to_save false",
})
vim.api.nvim_buf_set_option(buf, "buftype", "")
vim.api.nvim_set_option_value("buftype", "", { buf = buf })
vim.api.nvim_buf_call(buf, function()
vim.cmd("write")
@@ -33,17 +71,14 @@ local function create_file()
vim.api.nvim_win_set_buf(0, buf)
message.success("File created successfully!!!")
end
function M.MapToRemote ()
function M.MapToRemote()
if file_exists() then
message.warn("File already exists!!!");
else
create_file()
end
end
local function read_file(path)
@@ -62,14 +97,13 @@ local function read_file(path)
end
function M.getConfig()
local configTemp = {};
if file_exists() then
local content, err = read_file(filePath)
if content then
for i, line in ipairs(content) do
for _, line in ipairs(content) do
local first_word, second_word = line:match("^(%S+)%s+(%S+)$")
configTemp[first_word] = second_word
end
@@ -81,8 +115,7 @@ function M.getConfig()
return configTemp
end
function M.Upload()
function M.UploadFile()
config = M.getConfig()
local current_buffer_path = vim.api.nvim_buf_get_name(0)
local directoryTemp = directory
@@ -101,11 +134,16 @@ function M.Upload()
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(
'curl -T "%s" ftp://%s/%s --user "%s:%s" > /dev/null 2>&1',
current_buffer_path, config.host, remotePath, config.user, config.password)
"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)
vim.loop.spawn("sh", {
vim.uv.spawn("sh", {
args = { "-c", command },
}, function(code, signal)
if code == 0 then
@@ -118,7 +156,48 @@ function M.Upload()
end)
end
end)
end
function M.UploadDir()
config = M.getConfig()
local current_buffer_path = vim.api.nvim_buf_get_name(0)
local directoryTemp = directory
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
return M

View File

@@ -23,7 +23,7 @@ M.error_once = vim.schedule_wrap(function(fmt, ...)
end)
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)
return M