Comparison

mod_file_management/mod_file_management.lua @ 3663:d43623bdf91b

mod_upload_file_management: Add this new module, for now only listing files uploaded by a user as an admin.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 01 Sep 2019 01:13:12 +0200
comparison
equal deleted inserted replaced
3662:a6c51f380777 3663:d43623bdf91b
1 -- mod_file_management
2 --
3 -- Copyright (C) 2019 Emmanuel Gil Peyrot
4 --
5 -- This file is MIT/X11 licensed.
6 --
7
8 module:depends("http_upload");
9 local dataform_new = require "util.dataforms".new;
10 local adhoc_new = module:require "adhoc".new;
11 local adhoc_simple_form = require "util.adhoc".new_simple_form;
12 local adhoc_initial_data_form = require "util.adhoc".new_initial_data_form;
13 local url = require "socket.url";
14 local lfs = require "lfs";
15 local datamanager = require "util.datamanager";
16 local jid_prepped_split = require "util.jid".prepped_split;
17 local join_path = require "util.paths".join;
18 local t_concat = table.concat;
19 local t_insert = table.insert;
20
21 local storage_path = module:get_option_string("http_upload_path", join_path(prosody.paths.data, "http_upload"));
22
23 local function get_url(dir, filename)
24 local slot_url = url.parse(module:http_url("upload"));
25 slot_url.path = url.parse_path(slot_url.path or "/");
26 t_insert(slot_url.path, dir);
27 t_insert(slot_url.path, filename);
28 slot_url.path.is_directory = false;
29 slot_url.path = url.build_path(slot_url.path);
30 return url.build(slot_url);
31 end
32
33 local list_form = dataform_new {
34 title = "List files for user";
35 instructions = "Select the JID of a user to list the files they have uploaded.";
36 {
37 type = "hidden";
38 name = "FORM_TYPE";
39 value = "http://prosody.im/protocol/file_management#list";
40 };
41 {
42 type = "jid-single";
43 name = "accountjid";
44 required = true;
45 label = "JID";
46 };
47 };
48
49 module:provides("adhoc", adhoc_new("File Management", "http://prosody.im/protocol/file_management#list", adhoc_simple_form(list_form, function (data, errors)
50 if errors then
51 local errmsg = {};
52 for name, text in pairs(errors) do
53 errmsg[#errmsg + 1] = name .. ": " .. text;
54 end
55 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
56 end
57
58 local jid = data.accountjid;
59 local user, host = jid_prepped_split(jid);
60
61 local uploads, err = datamanager.list_load(user, host, "http_upload");
62 if err then
63 return { status = "completed", error = "File upload data not found for user "..jid.."." };
64 end
65
66 local result = {};
67 for i, upload in ipairs(uploads) do
68 module:log("debug", "http_upload_management#list %d %q", i, upload);
69 if upload.dir ~= nil then
70 t_insert(result, get_url(upload.dir, upload.filename));
71 else
72 -- upload.filename was pointing to a path on the file system…
73 -- TODO: Try to guess the URL from that.
74 end
75 end
76
77 return { status = "completed", info = t_concat(result, "\n") };
78 end), "admin"));