diff src/luan/modules/host/Hosting.luan @ 1596:a9ff30fb5d89

add Hosting.push_file
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 26 Mar 2021 20:10:44 -0600
parents fe5bcd8133e0
children cd2a0c41b23f
line wrap: on
line diff
--- a/src/luan/modules/host/Hosting.luan	Sun Mar 21 16:13:05 2021 -0600
+++ b/src/luan/modules/host/Hosting.luan	Fri Mar 26 20:10:44 2021 -0600
@@ -3,11 +3,14 @@
 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"
 
@@ -61,6 +64,28 @@
 	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())
+end
+
 function Hosting.delete(domain,password)
 	local host = Rpc.remote(domain)
 	host.delete(domain,password)