Comparison

plugins/mod_http_file_share.lua @ 11310:d1a0f2e918c0

mod_http_file_share: Add support for external file upload service PUT /upload/:slot/:filename Authorization: Bearer JWT{ uploader, filename, filesize, filetype, exp }
author Kim Alvefur <zash@zash.se>
date Tue, 26 Jan 2021 14:27:51 +0100
parent 11309:b59aed75dc5e
child 11311:9edda2026e57
comparison
equal deleted inserted replaced
11309:b59aed75dc5e 11310:d1a0f2e918c0
15 local jwt = require "util.jwt"; 15 local jwt = require "util.jwt";
16 local errors = require "util.error"; 16 local errors = require "util.error";
17 17
18 local namespace = "urn:xmpp:http:upload:0"; 18 local namespace = "urn:xmpp:http:upload:0";
19 19
20 module:depends("http");
21 module:depends("disco"); 20 module:depends("disco");
22 21
23 module:add_identity("store", "file", module:get_option_string("name", "HTTP File Upload")); 22 module:add_identity("store", "file", module:get_option_string("name", "HTTP File Upload"));
24 module:add_feature(namespace); 23 module:add_feature(namespace);
25 24
26 local uploads = module:open_store("uploads", "archive"); 25 local uploads = module:open_store("uploads", "archive");
27 -- id, <request>, time, owner 26 -- id, <request>, time, owner
28 27
29 local secret = module:get_option_string(module.name.."_secret", require"util.id".long()); 28 local secret = module:get_option_string(module.name.."_secret", require"util.id".long());
29 local external_base_url = module:get_option_string(module.name .. "_base_url");
30
31 if not external_base_url then
32 module:depends("http");
33 end
30 34
31 function may_upload(uploader, filename, filesize, filetype) -- > boolean, error 35 function may_upload(uploader, filename, filesize, filetype) -- > boolean, error
32 -- TODO authz 36 -- TODO authz
33 return true; 37 return true;
34 end 38 end
43 exp = os.time()+300; 47 exp = os.time()+300;
44 }); 48 });
45 end 49 end
46 50
47 function get_url(slot, filename) 51 function get_url(slot, filename)
48 local base_url = module:http_url(); 52 local base_url = external_base_url or module:http_url();
49 local slot_url = url.parse(base_url); 53 local slot_url = url.parse(base_url);
50 slot_url.path = url.parse_path(slot_url.path or "/"); 54 slot_url.path = url.parse_path(slot_url.path or "/");
51 t_insert(slot_url.path, slot); 55 t_insert(slot_url.path, slot);
52 if filename then 56 if filename then
53 t_insert(slot_url.path, filename); 57 t_insert(slot_url.path, filename);
180 184
181 -- TODO periodic cleanup job 185 -- TODO periodic cleanup job
182 186
183 module:hook("iq-get/host/urn:xmpp:http:upload:0:request", handle_slot_request); 187 module:hook("iq-get/host/urn:xmpp:http:upload:0:request", handle_slot_request);
184 188
189 if not external_base_url then
185 module:provides("http", { 190 module:provides("http", {
186 streaming_uploads = true; 191 streaming_uploads = true;
187 route = { 192 route = {
188 ["PUT /*"] = handle_upload; 193 ["PUT /*"] = handle_upload;
189 ["GET /*"] = handle_download; 194 ["GET /*"] = handle_download;
190 } 195 }
191 }); 196 });
197 end