view src/luan/modules/host/Hosting.luan @ 1597:cd2a0c41b23f

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 26 Mar 2021 20:20:21 -0600
parents a9ff30fb5d89
children
line wrap: on
line source
local Luan = require "luan:Luan.luan"
local error = Luan.error
local ipairs = Luan.ipairs or error()
local pairs = Luan.pairs or error()
local set_metatable = Luan.set_metatable or error()
local stringify = Luan.stringify or error()
local Io = require "luan:Io.luan"
local print = Io.print or error()
local Rpc = require "luan:Rpc.luan"
local String = require "luan:String.luan"
local matches = String.matches or error()
local substring = String.sub or error()
local split = String.split or error()
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "Hosting"


local Hosting = {}

function Hosting.push(domain,password,dir)
	local my_dir = Io.uri("file:"..dir)
	my_dir.exists() or error("directory '"..dir.."' not found")
	my_dir.is_directory() or error("'"..dir.."' is not a directory")
	local host = Rpc.remote(domain)
	local tree = host.get(domain,password)
	if tree == nil then
		print("creating "..domain)
		tree = host.create(domain,password)
	end

	local function process(there_parent,there,here)
		if here.is_file() then
			if there == nil or there.checksum ~= here.checksum() then
				print("copying "..here.to_string())
				host.copy_file(domain,password,there_parent.path,here.name(),here.read_binary())
			end
		elseif here.is_directory() then
			if here.name() == "local" then
				return
			end
			if there == nil then
				there = host.mkdir(domain,password,there_parent.path,here.name())
			end
			for _, here_child in ipairs(here.children()) do
				local name = here_child.name()
				if not matches(name,[[^\.]]) then
					process(there,there.children[name],here_child)
					there.children[name] = nil
				end
			end
			for _, there_child in pairs(there.children) do
				if host.delete_unused(domain,password,there_child.path) then
					print("deleted "..here.to_string().."/"..there_child.name)
				end
			end
		else
			error "not file or dir"
		end
	end

	process( nil, tree, my_dir )

	host.update_handler(domain,password)
	host.close()
end

function Hosting.push_file(domain,password,dir,file)
	local my_dir = Io.uri("file:"..dir)
	my_dir.exists() or error("directory '"..dir.."' not found")
	my_dir.is_directory() or error("'"..dir.."' is not a directory")
	local my_file = Io.uri("file:"..file)
	my_file.exists() or error("file '"..file.."' not found")
	my_file.is_file() or error("'"..file.."' is not a file")
	local my_file_string = my_file.to_string()
	local my_dir_string = my_dir.to_string().."/"
	matches( my_file_string, [[^\Q]]..my_dir_string..[[\E]] ) or error "file must be in dir"
	my_file_string = substring(my_file_string,#my_dir_string+1)
	local path = {split( my_file_string, "/" )}
	path[#path] = nil
	local host = Rpc.remote(domain)
	local there = host.get(domain,password)
	there~=nil or error "site not created"
	for _, s in ipairs(path) do
		there = there.children[s] or error("'"..s.."' not found on remote")
	end
	host.copy_file(domain,password,there.path,my_file.name(),my_file.read_binary())
	host.update_handler(domain,password)
	host.close()
end

function Hosting.delete(domain,password)
	local host = Rpc.remote(domain)
	host.delete(domain,password)
	host.close()
end

function Hosting.exists(domain)
	local host = Rpc.remote(domain)
	local rtn = host.exists(domain)
	host.close()
	return rtn
end

function Hosting.change_domain(old_domain,new_domain,password)
	local host = Rpc.remote(new_domain)
	local rtn = host.change_domain(old_domain,new_domain,password)
	host.close()
	return rtn
end

function Hosting.change_password(domain,old_password,new_password)
	local host = Rpc.remote(domain)
	local rtn = host.change_password(domain,old_password,new_password)
	host.close()
	return rtn
end

function Hosting.caller(domain)
	local host = Rpc.remote(domain)
	local mt = {}
	function mt.__index(_,key)
		return function(...)
			if key == "close" then
				return host.close()
			end
			return host.call(domain,key,...)
		end
	end
	local t = {}
	set_metatable(t,mt)
	return t
end

return Hosting