changeset 3625:a578b4977bb0

mod_http_upload: Duplicate mime types handling from mod_http_files (fixes #1374) When using net.http.files this isn't automatically provided via mod_http_files so we need to do it ourselves.
author Kim Alvefur <zash@zash.se>
date Sat, 29 Jun 2019 19:26:08 +0200
parents 3109a65ab7f4
children c84bbf36c878
files mod_http_upload/mod_http_upload.lua
diffstat 1 files changed, 30 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mod_http_upload/mod_http_upload.lua	Wed Jun 26 21:45:50 2019 +0200
+++ b/mod_http_upload/mod_http_upload.lua	Sat Jun 29 19:26:08 2019 +0200
@@ -52,6 +52,35 @@
 	http_files = module:depends"http_files";
 end
 
+local mime_map = module:shared("/*/http_files/mime").types;
+if not mime_map then
+	mime_map = {
+		html = "text/html", htm = "text/html",
+		xml = "application/xml",
+		txt = "text/plain",
+		css = "text/css",
+		js = "application/javascript",
+		png = "image/png",
+		gif = "image/gif",
+		jpeg = "image/jpeg", jpg = "image/jpeg",
+		svg = "image/svg+xml",
+	};
+	module:shared("/*/http_files/mime").types = mime_map;
+
+	local mime_types, err = io.open(module:get_option_path("mime_types_file", "/etc/mime.types", "config"), "r");
+	if mime_types then
+		local mime_data = mime_types:read("*a");
+		mime_types:close();
+		setmetatable(mime_map, {
+			__index = function(t, ext)
+				local typ = mime_data:match("\n(%S+)[^\n]*%s"..(ext:lower()).."%s") or "application/octet-stream";
+				t[ext] = typ;
+				return typ;
+			end
+		});
+	end
+end
+
 -- namespaces
 local namespace = "urn:xmpp:http:upload:0";
 local legacy_namespace = "urn:xmpp:http:upload";
@@ -342,7 +371,7 @@
 	end
 end
 
-local serve_uploaded_files = http_files.serve(storage_path);
+local serve_uploaded_files = http_files.serve({ path = storage_path, mime_map = mime_map });
 
 local function serve_head(event, path)
 	set_cross_domain_headers(event.response);