Mercurial > prosody-modules
comparison mod_muc_cache_media/mod_muc_cache_media.lua @ 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 | dcc8d4af8d4f |
comparison
equal
deleted
inserted
replaced
| 6414:48fca0914fcf | 6415:420aaf113272 |
|---|---|
| 9 local hashes = require "util.hashes" | 9 local hashes = require "util.hashes" |
| 10 local jid = require "util.jid" | 10 local jid = require "util.jid" |
| 11 | 11 |
| 12 local storage_path = module:get_option_string("muc_media_store_path", "/var/lib/prosody/muc-media") | 12 local storage_path = module:get_option_string("muc_media_store_path", "/var/lib/prosody/muc-media") |
| 13 local public_base_url = module:get_option_string("muc_media_public_base", "https://cache.example.com") | 13 local public_base_url = module:get_option_string("muc_media_public_base", "https://cache.example.com") |
| 14 local files_have_extensions = module:get_option_boolean("muc_media_files_have_extensions", true) | |
| 14 local max_size = module:get_option_number("muc_media_max_size", 10 * 1024 * 1024) | 15 local max_size = module:get_option_number("muc_media_max_size", 10 * 1024 * 1024) |
| 15 | 16 |
| 16 lfs.mkdir(storage_path) | 17 lfs.mkdir(storage_path) |
| 17 | 18 |
| 18 local mime_extensions = { | 19 local mime_extensions = { |
| 159 local sha3 = sha3_256_file(tmp_path) | 160 local sha3 = sha3_256_file(tmp_path) |
| 160 local size = file_size(tmp_path) | 161 local size = file_size(tmp_path) |
| 161 local name = filename_from_url(original_url) | 162 local name = filename_from_url(original_url) |
| 162 if sha2 then | 163 if sha2 then |
| 163 local ext = extension_from_content_type(content_type) | 164 local ext = extension_from_content_type(content_type) |
| 164 local final_name = sha2:gsub("+","-"):gsub("/","_"):gsub("=","")..ext | 165 local file_name = sha2:gsub("+","-"):gsub("/","_"):gsub("=","") |
| 165 local final_path = storage_path.."/"..final_name | 166 local url_name = file_name..ext |
| 167 if files_have_extensions then | |
| 168 file_name = file_name..ext | |
| 169 end | |
| 170 local final_path = storage_path.."/"..file_name | |
| 166 if not lfs.attributes(final_path) then | 171 if not lfs.attributes(final_path) then |
| 167 os.rename(tmp_path, final_path) | 172 os.rename(tmp_path, final_path) |
| 168 else | 173 else |
| 169 os.remove(tmp_path) | 174 os.remove(tmp_path) |
| 170 end | 175 end |
| 171 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 } | 176 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 } |
| 172 else | 177 else |
| 173 os.remove(tmp_path) | 178 os.remove(tmp_path) |
| 174 end | 179 end |
| 175 end | 180 end |
| 176 end | 181 end |
| 310 for hash_tag in file_tag:childtags("hash", "urn:xmpp:hashes:2") do | 315 for hash_tag in file_tag:childtags("hash", "urn:xmpp:hashes:2") do |
| 311 if hash_tag.attr.algo == "sha2-256" then | 316 if hash_tag.attr.algo == "sha2-256" then |
| 312 local sha2 = hash_tag:get_text() | 317 local sha2 = hash_tag:get_text() |
| 313 if sha2 then | 318 if sha2 then |
| 314 local prefix = sha2:gsub("+","-"):gsub("/","_"):gsub("=","") | 319 local prefix = sha2:gsub("+","-"):gsub("/","_"):gsub("=","") |
| 315 -- Files are named: hash + extension | 320 if files_have_extensions then |
| 316 for file in lfs.dir(storage_path) do | 321 -- Files are named: hash + extension |
| 317 if file:match("^" .. prefix) then | 322 for file in lfs.dir(storage_path) do |
| 318 local path = storage_path .. "/" .. file | 323 if file:match("^" .. prefix) then |
| 319 if lfs.attributes(path) then | 324 local path = storage_path .. "/" .. file |
| 320 os.remove(path) | 325 if lfs.attributes(path) then |
| 321 module:log("debug", | 326 os.remove(path) |
| 322 "Deleted cached media %s for moderated message %s", | 327 module:log("debug", |
| 323 file, stanza_id | 328 "Deleted cached media %s for moderated message %s", |
| 324 ) | 329 file, stanza_id |
| 330 ) | |
| 331 end | |
| 325 end | 332 end |
| 333 end | |
| 334 else | |
| 335 local path = storage_path .. "/" .. prefix | |
| 336 if lfs.attributes(path) then | |
| 337 os.remove(path) | |
| 338 module:log("debug", | |
| 339 "Deleted cached media %s for moderated message %s", | |
| 340 file, stanza_id | |
| 341 ) | |
| 326 end | 342 end |
| 327 end | 343 end |
| 328 end | 344 end |
| 329 end | 345 end |
| 330 end | 346 end |
