Annotate

mod_file_management/mod_file_management.lua @ 5551:8bfcedd93a72

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