mod_http_upload: Guess mime type from file extension if not provided by client (mime-type is OPTIONAL)

Sun, 16 Apr 2017 00:58:02 +0200

author
Kim Alvefur <zash@zash.se>
date
Sun, 16 Apr 2017 00:58:02 +0200
changeset 2692
785465f8af3d
parent 2691
92ddfc548ce8
child 2693
04ae5b45e6c7

mod_http_upload: Guess mime type from file extension if not provided by client (mime-type is OPTIONAL)

mod_http_upload/mod_http_upload.lua file | annotate | diff | comparison | revisions
--- a/mod_http_upload/mod_http_upload.lua	Sat Apr 15 15:55:39 2017 +0200
+++ b/mod_http_upload/mod_http_upload.lua	Sun Apr 16 00:58:02 2017 +0200
@@ -143,9 +143,16 @@
 
 	if mime_map then
 		local file_ext = filename:match("%.([^.]+)$");
-		if (not file_ext and mimetype ~= "application/octet-stream") or (file_ext and mime_map[file_ext] ~= mimetype) then
-			origin.send(st.error_reply(stanza, "modify", "bad-request", "MIME type does not match file extension"));
-			return true;
+		if not mimetype then
+			mimetype = "application/octet-stream";
+			if file_ext then
+				mimetype = mime_map[file_ext] or mimetype;
+			end
+		else
+			if (not file_ext and mimetype ~= "application/octet-stream") or (file_ext and mime_map[file_ext] ~= mimetype) then
+				origin.send(st.error_reply(stanza, "modify", "bad-request", "MIME type does not match file extension"));
+				return true;
+			end
 		end
 	end
 

mercurial