#!/usr/bin/lua

local default_dir = arg[1] or "/tmp"
local content_type = os.getenv("CONTENT_TYPE") or ""
local content_length = tonumber(os.getenv("CONTENT_LENGTH") or "0") or 0
local pending = ""
local target_tmp = nil

local function cleanup()
	if target_tmp then
		os.remove(target_tmp)
	end
end

local function fail(message)
	cleanup()
	io.stderr:write("ОШИБКА: " .. message .. "\n")
	os.exit(1)
end

local function ok(message)
	io.stdout:write(message .. "\n")
end

local function valid_path(path)
	return type(path) == "string" and path:match("^/[A-Za-z0-9%._/%-]+$") ~= nil
end

local function shell_quote(value)
	return "'" .. tostring(value):gsub("'", "'\\''") .. "'"
end

local function safe_filename(name)
	name = tostring(name or ""):gsub("\\", "/")
	name = name:match("([^/]+)$") or ""
	name = name:gsub("[^A-Za-z0-9%._%-]", "_")
	return name
end

local function trim_crlf(line)
	return tostring(line or ""):gsub("\r\n$", ""):gsub("\n$", "")
end

local function read_from_stdin(size)
	return io.stdin:read(size or 8192)
end

local function read_line()
	while true do
		local pos = pending:find("\n", 1, true)
		if pos then
			local line = pending:sub(1, pos)
			pending = pending:sub(pos + 1)
			return line
		end

		local chunk = read_from_stdin(1024)
		if not chunk or #chunk == 0 then
			if #pending > 0 then
				local line = pending
				pending = ""
				return line
			end
			return nil
		end

		pending = pending .. chunk
	end
end

local boundary = content_type:match('boundary="?([^";]+)"?')
if not boundary or boundary == "" then
	fail("multipart boundary не найден")
end

if content_length <= 0 then
	fail("пустой upload")
end

local marker = "--" .. boundary
local delimiter = "\r\n" .. marker

local first_line = trim_crlf(read_line())
if first_line ~= marker then
	fail("multipart boundary не найден в теле запроса")
end

local function read_headers()
	local headers = {}

	while true do
		local line = read_line()
		if not line then
			fail("не удалось прочитать заголовки upload")
		end

		line = trim_crlf(line)
		if line == "" then
			break
		end

		headers[#headers + 1] = line
	end

	return table.concat(headers, "\n")
end

local function read_part(out, max_text)
	local keep = #delimiter + 4
	local buffer = ""
	local text = {}
	local text_size = 0
	local part_size = 0

	local function consume(data)
		if not data or #data == 0 then
			return
		end

		part_size = part_size + #data
		if out then
			local wrote, err = out:write(data)
			if not wrote then
				fail("не удалось записать upload: " .. tostring(err))
			end
			return
		end

		text_size = text_size + #data
		if text_size > (max_text or 4096) then
			fail("поле формы слишком большое")
		end
		text[#text + 1] = data
	end

	while true do
		local chunk
		if #pending > 0 then
			chunk = pending
			pending = ""
		else
			chunk = read_from_stdin(8192)
		end

		if not chunk then
			fail("не найден конец multipart upload")
		end

		buffer = buffer .. chunk
		local start_pos, end_pos = buffer:find(delimiter, 1, true)
		if start_pos then
			consume(buffer:sub(1, start_pos - 1))

			local after = end_pos + 1
			local final = false
			if buffer:sub(after, after + 1) == "--" then
				final = true
				after = after + 2
			end
			if buffer:sub(after, after + 1) == "\r\n" then
				after = after + 2
			end

			pending = buffer:sub(after)
			return final, table.concat(text), part_size
		end

		if #buffer > keep then
			consume(buffer:sub(1, #buffer - keep))
			buffer = buffer:sub(#buffer - keep + 1)
		end
	end
end

local upload_dir = default_dir
local saved_path = nil
local saved_size = 0
local final = false

while not final do
	local headers = read_headers()
	local disposition = headers:match("[Cc]ontent%-[Dd]isposition:%s*([^\n]+)")
	local name = disposition and disposition:match('name="([^"]+)"')
	local filename = disposition and disposition:match('filename="([^"]*)"')

	if name == "upload_dir" and not filename then
		local value
		final, value = read_part(nil, 4096)
		upload_dir = tostring(value or ""):gsub("[%z\r\n]", "")
	elseif name == "archive" and filename and filename ~= "" then
		if not valid_path(upload_dir) then
			fail("неверная папка для загрузки: " .. tostring(upload_dir))
		end

		local file_name = safe_filename(filename)
		if file_name == "" then
			fail("файл архива не выбран")
		end
		if not file_name:match("%.tar%.gz$") then
			fail("можно загружать только архивы .tar.gz")
		end

		os.execute("mkdir -p " .. shell_quote(upload_dir))

		local target = upload_dir:gsub("/+$", "") .. "/" .. file_name
		target_tmp = target .. ".uploading." .. tostring(os.time()) .. "." .. tostring(content_length)
		local out, err = io.open(target_tmp, "wb")
		if not out then
			fail("не удалось открыть файл для записи: " .. target .. ": " .. tostring(err))
		end

		local _
		final, _, saved_size = read_part(out, 0)
		local closed, close_err = out:close()
		if not closed then
			fail("не удалось закрыть файл: " .. target .. ": " .. tostring(close_err))
		end
		if saved_size <= 0 then
			fail("файл пустой")
		end

		local renamed, rename_err = os.rename(target_tmp, target)
		if not renamed then
			fail("не удалось сохранить файл: " .. target .. ": " .. tostring(rename_err))
		end

		target_tmp = nil
		saved_path = target
	else
		final = read_part(nil, 1024)
	end
end

if not saved_path then
	fail("файл архива не выбран")
end

ok("Архив загружен: " .. saved_path)
ok("Размер: " .. tostring(saved_size) .. " байт")
