Upload files to "lua_script"
This commit is contained in:
parent
bf61285759
commit
d4edc5c2aa
144
lua_script/urjenc.lua
Normal file
144
lua_script/urjenc.lua
Normal file
@ -0,0 +1,144 @@
|
||||
#!/usr/bin/lua
|
||||
|
||||
-- Функция для вывода текста красным цветом
|
||||
function print_red(text)
|
||||
-- Коды ANSI для красного цвета
|
||||
io.write("\27[31m" .. text .. "\27[0m\n")
|
||||
end
|
||||
|
||||
-- Функция для кодирования URL
|
||||
function url_encode(str)
|
||||
if str == nil then
|
||||
return ""
|
||||
end
|
||||
|
||||
local result = ""
|
||||
for i = 1, #str do
|
||||
local c = str:sub(i, i)
|
||||
local byte = string.byte(c)
|
||||
|
||||
-- Оставляем буквы, цифры и некоторые специальные символы как есть
|
||||
if (byte >= 48 and byte <= 57) or -- 0-9
|
||||
(byte >= 65 and byte <= 90) or -- A-Z
|
||||
(byte >= 97 and byte <= 122) or -- a-z
|
||||
c == '-' or c == '_' or c == '.' or c == '~' or
|
||||
c == '/' or c == ':' or c == '?' or c == '&' or
|
||||
c == '=' or c == '#' or c == '@' or c == '!' or
|
||||
c == '$' or c == "'" or c == '(' or c == ')' or
|
||||
c == '*' or c == '+' or c == ',' or c == ';' then
|
||||
result = result .. c
|
||||
else
|
||||
-- Кодируем все остальные символы
|
||||
result = result .. string.format("%%%02X", byte)
|
||||
end
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
-- Функция для проверки формата URL
|
||||
function validate_url_format(url)
|
||||
-- Проверяем базовую структуру URL
|
||||
local pattern = "^http://localhost:9080/quick/create/users/[%w_]+/.*$"
|
||||
if not url:match(pattern) then
|
||||
return false, "URL does not match expected format"
|
||||
end
|
||||
|
||||
-- Проверяем наличие JSON части
|
||||
local json_start = url:find("{")
|
||||
if not json_start then
|
||||
return false, "No JSON part found in URL"
|
||||
end
|
||||
|
||||
-- Проверяем, что JSON часть корректна (хотя бы базово)
|
||||
local json_part = url:sub(json_start)
|
||||
-- Используем простую проверку на наличие фигурных скобок
|
||||
local open_brace = json_part:find("{")
|
||||
local close_brace = json_part:find("}")
|
||||
|
||||
if not open_brace or not close_brace or close_brace < open_brace then
|
||||
return false, "Invalid JSON format"
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
-- Функция для обработки URL
|
||||
function process_url(url)
|
||||
-- Проверяем формат URL
|
||||
local is_valid, error_msg = validate_url_format(url)
|
||||
if not is_valid then
|
||||
error("Error. A URL-formatted string was expected")
|
||||
end
|
||||
|
||||
-- Находим начало JSON части
|
||||
local json_start = url:find("{")
|
||||
|
||||
-- Разделяем URL на части: до JSON и сам JSON
|
||||
local url_part = url:sub(1, json_start - 1)
|
||||
local json_part = url:sub(json_start)
|
||||
|
||||
-- Кодируем JSON часть
|
||||
local encoded_json = url_encode(json_part)
|
||||
|
||||
-- Объединяем результаты
|
||||
return url_part .. encoded_json
|
||||
end
|
||||
|
||||
-- Основная функция
|
||||
function main()
|
||||
print() -- Пустая строка перед Urjenc
|
||||
print("Urjenc")
|
||||
print() -- Пустая строка после Urjenc
|
||||
|
||||
while true do
|
||||
-- Печатаем приглашение без перевода строки
|
||||
io.write("Enter a URL:$ ")
|
||||
io.flush()
|
||||
|
||||
-- Читаем ввод пользователя
|
||||
local input = io.read()
|
||||
|
||||
-- Проверяем команды выхода
|
||||
if input == "exit" or input == "quit" then
|
||||
break
|
||||
end
|
||||
|
||||
-- Проверяем, что ввод не пустой
|
||||
if input and input ~= "" then
|
||||
local success, result = pcall(function()
|
||||
-- Обрабатываем URL
|
||||
local transformed_url = process_url(input)
|
||||
|
||||
-- Записываем результат в файл
|
||||
local file = io.open("transformation.txt", "w")
|
||||
if not file then
|
||||
error("Cannot open file for writing")
|
||||
end
|
||||
|
||||
file:write(transformed_url)
|
||||
file:close()
|
||||
|
||||
return transformed_url
|
||||
end)
|
||||
|
||||
-- Проверяем результат операции
|
||||
if success then
|
||||
print("transformation successfull")
|
||||
else
|
||||
print_red("transformation failed")
|
||||
if result:match("Error. A URL%-formatted string was expected") then
|
||||
print_red("Error. A URL-formatted string was expected")
|
||||
else
|
||||
print_red("Error: " .. tostring(result))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Добавляем пустую строку перед следующим приглашением
|
||||
print()
|
||||
end
|
||||
end
|
||||
|
||||
-- Запускаем программу
|
||||
main()
|
||||
Loading…
x
Reference in New Issue
Block a user