Mercurial > prosody-hg
comparison plugins/mod_http_file_share.lua @ 13947:0f482af5b882
mod_http_file_share: Check for unexpected files in the upload directory
In case the metadata is lost or corrupted it can be handy to know what
files might have been left behind.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 04 Dec 2021 18:57:11 +0100 |
| parents | f5e8ab42c708 |
| children | fc489086e09a |
comparison
equal
deleted
inserted
replaced
| 13946:f5e8ab42c708 | 13947:0f482af5b882 |
|---|---|
| 16 local dataform = require "prosody.util.dataforms".new; | 16 local dataform = require "prosody.util.dataforms".new; |
| 17 local urlencode = require "prosody.util.http".urlencode; | 17 local urlencode = require "prosody.util.http".urlencode; |
| 18 local dt = require "prosody.util.datetime"; | 18 local dt = require "prosody.util.datetime"; |
| 19 local hi = require "prosody.util.human.units"; | 19 local hi = require "prosody.util.human.units"; |
| 20 local cache = require "prosody.util.cache"; | 20 local cache = require "prosody.util.cache"; |
| 21 local join_path = require "util.paths".join; | |
| 21 local lfs = require "lfs"; | 22 local lfs = require "lfs"; |
| 22 | 23 |
| 23 local unknown = math.abs(0/0); | 24 local unknown = math.abs(0/0); |
| 24 local unlimited = math.huge; | 25 local unlimited = math.huge; |
| 25 | 26 |
| 566 module:weekly("Calculate total storage usage", function() | 567 module:weekly("Calculate total storage usage", function() |
| 567 local summary_done = summary_start(); | 568 local summary_done = summary_start(); |
| 568 local iter = assert(uploads:find(nil)); | 569 local iter = assert(uploads:find(nil)); |
| 569 | 570 |
| 570 local count, sum = 0, 0; | 571 local count, sum = 0, 0; |
| 572 local expected_filenames = {}; | |
| 571 for slot_id, file in iter do | 573 for slot_id, file in iter do |
| 572 count = count + 1; | 574 count = count + 1; |
| 575 | |
| 573 local expected_size = tonumber(file.attr.size); | 576 local expected_size = tonumber(file.attr.size); |
| 574 local filename = get_filename(slot_id); | 577 local filename = get_filename(slot_id); |
| 575 local actual_size, err = lfs.attributes(filename, "size"); | 578 local actual_size, err = lfs.attributes(filename, "size"); |
| 579 expected_filenames[filename] = true; | |
| 576 if not actual_size then | 580 if not actual_size then |
| 577 module:log("warn", "Missing file for upload slot %s: %s", slot_id, err); | 581 module:log("warn", "Missing file for upload slot %s: %s", slot_id, err); |
| 578 count = count - 1; | 582 count = count - 1; |
| 579 elseif actual_size ~= expected_size then | 583 elseif actual_size ~= expected_size then |
| 580 module:log("warn", "Size mismatch for upload slot %s, expected %s, actual %s", slot_id, B(expected_size), B(actual_size)); | 584 module:log("warn", "Size mismatch for upload slot %s, expected %s, actual %s", slot_id, B(expected_size), B(actual_size)); |
| 581 -- TODO delete? | 585 -- TODO delete? |
| 582 sum = sum + actual_size; | 586 sum = sum + actual_size; |
| 583 else | 587 else |
| 584 sum = sum + expected_size; | 588 sum = sum + expected_size; |
| 589 end | |
| 590 end | |
| 591 | |
| 592 local upload_dir = dm.getpath("", module.host, module.name, ""):gsub("/[^/]*$", ""); | |
| 593 for file in lfs.dir(upload_dir) do | |
| 594 local file_path = join_path(upload_dir, file); | |
| 595 if file ~= "." and file ~= ".." and not expected_filenames[file_path] then | |
| 596 local mode, err = lfs.attributes(file_path, "mode"); | |
| 597 module:log("warn", "Unexpected %s in upload storage: %s", mode or "file", err or file); | |
| 585 end | 598 end |
| 586 end | 599 end |
| 587 | 600 |
| 588 module:log("info", "Uploaded files total: %s in %d files", B(sum), count); | 601 module:log("info", "Uploaded files total: %s in %d files", B(sum), count); |
| 589 if persist_stats:set(nil, "total", sum) then | 602 if persist_stats:set(nil, "total", sum) then |
