Comparison

plugins/mod_http_file_share.lua @ 11328:ceaa3cebf28b

mod_http_file_share: Add support for removing old files (default 2 weeks)
author Kim Alvefur <zash@zash.se>
date Wed, 27 Jan 2021 22:09:26 +0100
parent 11327:6f2b69469060
child 11329:2a431d3ad8f1
comparison
equal deleted inserted replaced
11327:6f2b69469060 11328:ceaa3cebf28b
30 30
31 local secret = module:get_option_string(module.name.."_secret", require"util.id".long()); 31 local secret = module:get_option_string(module.name.."_secret", require"util.id".long());
32 local external_base_url = module:get_option_string(module.name .. "_base_url"); 32 local external_base_url = module:get_option_string(module.name .. "_base_url");
33 local file_size_limit = module:get_option_number(module.name .. "_size_limit", 10 * 1024 * 1024); -- 10 MB 33 local file_size_limit = module:get_option_number(module.name .. "_size_limit", 10 * 1024 * 1024); -- 10 MB
34 local file_types = module:get_option_set(module.name .. "_allowed_file_types", {}); 34 local file_types = module:get_option_set(module.name .. "_allowed_file_types", {});
35 local expiry = module:get_option_number(module.name .. "_expires_after", 7 * 86400);
35 36
36 local access = module:get_option_set(module.name .. "_access", {}); 37 local access = module:get_option_set(module.name .. "_access", {});
37 38
38 if not external_base_url then 39 if not external_base_url then
39 module:depends("http"); 40 module:depends("http");
254 response.headers.x_xss_protection = "1; mode=block"; 255 response.headers.x_xss_protection = "1; mode=block";
255 256
256 return response:send_file(handle); 257 return response:send_file(handle);
257 end 258 end
258 259
259 -- TODO periodic cleanup job 260 if expiry >= 0 and not external_base_url then
261 -- TODO HTTP DELETE to the external endpoint?
262 local array = require "util.array";
263 local async = require "util.async";
264 local ENOENT = require "util.pposix".ENOENT;
265
266 local reaper_task = async.runner(function(boundary_time)
267 local iter, total = assert(uploads:find(nil, {["end"] = boundary_time; total = true}));
268
269 if total == 0 then
270 module:log("info", "No expired to prune");
271 return;
272 end
273
274 module:log("info", "Pruning expired files uploaded earlier than %s", dt.datetime(boundary_time));
275
276 local obsolete_files = array();
277 local i = 0;
278 for slot_id in iter do
279 i = i + 1;
280 obsolete_files:push(get_filename(slot_id));
281 end
282
283 local n = 0;
284 obsolete_files:filter(function(filename)
285 n = n + 1;
286 local deleted, err, errno = os.remove(filename);
287 if deleted or errno == ENOENT then
288 return false;
289 else
290 module:log("error", "Could not delete file %q: %s", filename, err);
291 return true;
292 end
293 end);
294
295 local deletion_query = {["end"] = boundary_time};
296 if #obsolete_files == 0 then
297 module:log("info", "All %d expired files deleted", n);
298 else
299 module:log("warn", "%d out of %d expired files could not be deleted", #obsolete_files, n);
300 deletion_query = {ids = obsolete_files};
301 end
302
303 local removed, err = uploads:delete(nil, deletion_query);
304
305 if removed == true or removed == n or removed == #obsolete_files then
306 module:log("debug", "Removed all metadata for expired uploaded files");
307 else
308 module:log("error", "Problem removing metadata for deleted files: %s", err);
309 end
310
311 end);
312
313 module:add_timer(1, function ()
314 reaper_task:run(os.time()-expiry);
315 return 60*60;
316 end);
317 end
260 318
261 module:hook("iq-get/host/urn:xmpp:http:upload:0:request", handle_slot_request); 319 module:hook("iq-get/host/urn:xmpp:http:upload:0:request", handle_slot_request);
262 320
263 if not external_base_url then 321 if not external_base_url then
264 module:provides("http", { 322 module:provides("http", {