changeset 6415:420aaf113272

File extension is optional on disk
author Stephen Paul Weber <singpolyma@singpolyma.net>
date Mon, 23 Feb 2026 15:06:37 -0500
parents 48fca0914fcf
children 2511bf07b8d2
files mod_muc_cache_media/mod_muc_cache_media.lua
diffstat 1 files changed, 29 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/mod_muc_cache_media/mod_muc_cache_media.lua	Mon Feb 23 12:41:36 2026 -0500
+++ b/mod_muc_cache_media/mod_muc_cache_media.lua	Mon Feb 23 15:06:37 2026 -0500
@@ -11,6 +11,7 @@
 
 local storage_path = module:get_option_string("muc_media_store_path", "/var/lib/prosody/muc-media")
 local public_base_url = module:get_option_string("muc_media_public_base", "https://cache.example.com")
+local files_have_extensions = module:get_option_boolean("muc_media_files_have_extensions", true)
 local max_size = module:get_option_number("muc_media_max_size", 10 * 1024 * 1024)
 
 lfs.mkdir(storage_path)
@@ -161,14 +162,18 @@
 			local name = filename_from_url(original_url)
 			if sha2 then
 				local ext = extension_from_content_type(content_type)
-				local final_name = sha2:gsub("+","-"):gsub("/","_"):gsub("=","")..ext
-				local final_path = storage_path.."/"..final_name
+				local file_name = sha2:gsub("+","-"):gsub("/","_"):gsub("=","")
+				local url_name = file_name..ext
+				if files_have_extensions then
+					file_name = file_name..ext
+				end
+				local final_path = storage_path.."/"..file_name
 				if not lfs.attributes(final_path) then
 					os.rename(tmp_path, final_path)
 				else
 					os.remove(tmp_path)
 				end
-				url_map[original_url] = { local_url = public_base_url.."/"..final_name, path=final_path, sha2=sha2, sha3=sha3, size=size, name=name, content_type=content_type }
+				url_map[original_url] = { local_url = public_base_url.."/"..url_name, path=final_path, sha2=sha2, sha3=sha3, size=size, name=name, content_type=content_type }
 			else
 				os.remove(tmp_path)
 			end
@@ -312,18 +317,29 @@
 							local sha2 = hash_tag:get_text()
 							if sha2 then
 								local prefix = sha2:gsub("+","-"):gsub("/","_"):gsub("=","")
-								-- Files are named: hash + extension
-								for file in lfs.dir(storage_path) do
-									if file:match("^" .. prefix) then
-										local path = storage_path .. "/" .. file
-										if lfs.attributes(path) then
-											os.remove(path)
-											module:log("debug",
-												"Deleted cached media %s for moderated message %s",
-												file, stanza_id
-											)
+								if files_have_extensions then
+									-- Files are named: hash + extension
+									for file in lfs.dir(storage_path) do
+										if file:match("^" .. prefix) then
+											local path = storage_path .. "/" .. file
+											if lfs.attributes(path) then
+												os.remove(path)
+												module:log("debug",
+													"Deleted cached media %s for moderated message %s",
+													file, stanza_id
+												)
+											end
 										end
 									end
+								else
+									local path = storage_path .. "/" .. prefix
+									if lfs.attributes(path) then
+										os.remove(path)
+										module:log("debug",
+											"Deleted cached media %s for moderated message %s",
+											file, stanza_id
+										)
+									end
 								end
 							end
 						end