view core/src/luan/modules/host/Hosting.luan @ 716:28fedb32ab19 0.19

hosting changes
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 23 May 2016 19:16:21 -0600
parents ca169567ce07
children 4f8e30a3ffd0
line wrap: on
line source

-- Hosting

local Luan = require "luan:Luan.luan"
local error = Luan.error
local ipairs = Luan.ipairs or error()
local pairs = Luan.pairs 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 M = {}

M.port = 9101

function M.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 socket = "socket:" .. domain .. ":" .. M.port
	local host = Rpc.remote(socket)
	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.last_modified < here.last_modified() 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)==true then   -- remove ==true later
					print("deleted "..there_child.name)
				end
			end
		else
			error "not file or dir"
		end
	end

	process( nil, tree, my_dir )

	host.update_handler(domain,password)
end

function M.delete(domain,password)
	local socket = "socket:" .. domain .. ":" .. M.port
	local host = Rpc.remote(socket)
	host.delete(domain,password)
end

function M.exists(domain)
	local socket = "socket:" .. domain .. ":" .. M.port
	local host = Rpc.remote(socket)
	return host.exists(domain)
end

return M