Comparison

mod_http_upload/mod_http_upload.lua @ 1772:45f7e3c2557f

mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
author Kim Alvefur <zash@zash.se>
date Sat, 18 Jul 2015 23:05:09 +0200
child 1805:25c28644fae8
comparison
equal deleted inserted replaced
1771:398a007da84e 1772:45f7e3c2557f
1 -- mod_http_upload
2 --
3 -- Copyright (C) 2015 Kim Alvefur
4 --
5 -- This file is MIT/X11 licensed.
6 --
7 -- Implementation of HTTP Upload file transfer mechanism used by Conversations
8 --
9
10 -- imports
11 local st = require"util.stanza";
12 local lfs = require"lfs";
13 local join_path = require"util.paths".join;
14 local uuid = require"util.uuid".generate;
15
16 -- depends
17 module:depends("http");
18
19 -- namespace
20 local xmlns_http_upload = "eu:siacs:conversations:http:upload";
21
22 module:add_feature(xmlns_http_upload);
23
24 -- state
25 local pending_slots = module:shared("upload_slots");
26
27 local storage_path = join_path(prosody.paths.data, module.name);
28 lfs.mkdir(storage_path);
29
30 -- hooks
31 module:hook("iq/host/"..xmlns_http_upload..":request", function (event)
32 local stanza, origin = event.stanza, event.origin;
33 -- local clients only
34 if origin.type ~= "c2s" then
35 origin.send(st.error_reply(stanza, "cancel", "not-authorized"));
36 return true;
37 end
38 -- validate
39 local filename = stanza.tags[1]:get_child_text("filename");
40 if not filename or filename:find("/") then
41 origin.send(st.error_reply(stanza, "modify", "bad-request"));
42 return true;
43 end
44 local reply = st.reply(stanza);
45 reply:tag("slot", { xmlns = xmlns_http_upload });
46 local random = uuid();
47 pending_slots[random.."/"..filename] = origin.full_jid;
48 local url = module:http_url() .. "/" .. random .. "/" .. filename;
49 reply:tag("get"):text(url):up();
50 reply:tag("put"):text(url):up();
51 origin.send(reply);
52 return true;
53 end);
54
55 -- http service
56 local function upload_data(event, path)
57 if not pending_slots[path] then
58 return 401;
59 end
60 local random, filename = path:match("^([^/]+)/([^/]+)$");
61 if not random then
62 return 400;
63 end
64 local dirname = join_path(storage_path, random);
65 if not lfs.mkdir(dirname) then
66 module:log("error", "Could not create directory %s for upload", dirname);
67 return 500;
68 end
69 local full_filename = join_path(dirname, filename);
70 local fh, ferr = io.open(full_filename, "w");
71 if not fh then
72 module:log("error", "Could not open file %s for upload: %s", full_filename, ferr);
73 return 500;
74 end
75 local ok, err = fh:write(event.request.body);
76 if not ok then
77 module:log("error", "Could not write to file %s for upload: %s", full_filename, err);
78 os.remove(full_filename);
79 return 500;
80 end
81 ok, err = fh:close();
82 if not ok then
83 module:log("error", "Could not write to file %s for upload: %s", full_filename, err);
84 os.remove(full_filename);
85 return 500;
86 end
87 module:log("info", "File uploaded by %s to slot %s", pending_slots[path], random);
88 pending_slots[path] = nil;
89 return 200;
90 end
91
92 local serve_uploaded_files = module:depends("http_files").serve(storage_path);
93
94 local function size_only(request, data)
95 request.headers.content_size = #data;
96 return 200;
97 end
98
99 local function serve_head(event, path)
100 event.send = size_only;
101 return serve_uploaded_files(event, path);
102 end
103
104 module:provides("http", {
105 route = {
106 ["GET /*"] = serve_uploaded_files;
107 ["HEAD /*"] = serve_head;
108 ["PUT /*"] = upload_data;
109 };
110 });