Diff

mod_http_upload_external/mod_http_upload_external.lua @ 2978:ac99a04231b1

mod_http_upload_external: Add newer 'v2' protocol (and share_v2.php) which supports content-type preservation
author Matthew Wild <mwild1@gmail.com>
date Mon, 02 Apr 2018 10:57:17 +0100
parent 2939:280305c043b0
child 3165:7af4776a5dea
line wrap: on
line diff
--- a/mod_http_upload_external/mod_http_upload_external.lua	Mon Apr 02 10:52:32 2018 +0100
+++ b/mod_http_upload_external/mod_http_upload_external.lua	Mon Apr 02 10:57:17 2018 +0100
@@ -14,8 +14,12 @@
 
 -- config
 local file_size_limit = module:get_option_number(module.name .. "_file_size_limit", 100 * 1024 * 1024); -- 100 MB
-local base_url = assert(module:get_option_string(module.name .. "_base_url"), module.name .. "_base_url is a required option");
-local secret = assert(module:get_option_string(module.name .. "_secret"), module.name .. "_secret is a required option");
+local base_url = assert(module:get_option_string(module.name .. "_base_url"),
+	module.name .. "_base_url is a required option");
+local secret = assert(module:get_option_string(module.name .. "_secret"),
+	module.name .. "_secret is a required option");
+
+local token_protocol = module:get_option_string(module.name .. "_protocol", "v1");
 
 -- depends
 module:depends("disco");
@@ -39,14 +43,19 @@
 	{ name = "max-file-size", type = "text-single" },
 }:form({ ["max-file-size"] = tostring(file_size_limit) }, "result"));
 
-local function magic_crypto_dust(random, filename, filesize)
-	local message = string.format("%s/%s %d", random, filename, filesize);
+local function magic_crypto_dust(random, filename, filesize, filetype)
+	local param, message;
+	if token_protocol == "v1" then
+		param, message = "v", string.format("%s/%s %d", random, filename, filesize);
+	else
+		param, message = "v2", string.format("%s/%s\0%d\0%s", random, filename, filesize, filetype);
+	end
 	local digest = HMAC(secret, message, true);
 	random, filename = http.urlencode(random), http.urlencode(filename);
-	return base_url .. random .. "/" .. filename, "?v=" .. digest;
+	return base_url .. random .. "/" .. filename, "?"..param.."=" .. digest;
 end
 
-local function handle_request(origin, stanza, xmlns, filename, filesize)
+local function handle_request(origin, stanza, xmlns, filename, filesize, filetype)
 	-- local clients only
 	if origin.type ~= "c2s" then
 		module:log("debug", "Request for upload slot from a %s", origin.type);
@@ -71,7 +80,7 @@
 		return nil, nil;
 	end
 	local random = uuid();
-	local get_url, verify = magic_crypto_dust(random, filename, filesize);
+	local get_url, verify = magic_crypto_dust(random, filename, filesize, filetype);
 	local put_url = get_url .. verify;
 
 	module:log("info", "Handing out upload slot %s to %s@%s", get_url, origin.username, origin.host);
@@ -85,9 +94,10 @@
 	local request = stanza.tags[1];
 	local filename = request:get_child_text("filename");
 	local filesize = tonumber(request:get_child_text("size"));
+	local filetype = request.attr["content-type"] or "application/octet-stream";
 
 	local get_url, put_url = handle_request(
-		origin, stanza, legacy_namespace, filename, filesize);
+		origin, stanza, legacy_namespace, filename, filesize, filetype);
 
 	if not get_url then
 		-- error was already sent
@@ -108,8 +118,10 @@
 	local request = stanza.tags[1];
 	local filename = request.attr.filename;
 	local filesize = tonumber(request.attr.size);
+	local filetype = request.attr["content-type"] or "application/octet-stream";
+
 	local get_url, put_url = handle_request(
-		origin, stanza, namespace, filename, filesize);
+		origin, stanza, namespace, filename, filesize, filetype);
 
 	if not get_url then
 		-- error was already sent