Comparison

mod_http_upload/mod_http_upload.lua @ 3366:6d1c5ecf72c1

mod_http_upload: Report statistics on upload sizes (0.10+)
author Kim Alvefur <zash@zash.se>
date Mon, 29 Oct 2018 01:21:29 +0100
parent 3341:1e1dbd7e5b6c
child 3369:fca95f1b8870
comparison
equal deleted inserted replaced
3365:a5a5f85d7ca1 3366:6d1c5ecf72c1
107 sum = sum + item.size; 107 sum = sum + item.size;
108 end 108 end
109 return sum < quota; 109 return sum < quota;
110 end 110 end
111 111
112 local measure_slot = function () end
113 if module.measure then
114 -- COMPAT 0.9
115 -- module:measure was added in 0.10
116 measure_slot = module:measure("sizes", "slot");
117 end
118
112 local function handle_request(origin, stanza, xmlns, filename, filesize) 119 local function handle_request(origin, stanza, xmlns, filename, filesize)
113 local username, host = origin.username, origin.host; 120 local username, host = origin.username, origin.host;
114 -- local clients only 121 -- local clients only
115 if origin.type ~= "c2s" then 122 if origin.type ~= "c2s" then
116 module:log("debug", "Request for upload slot from a %s", origin.type); 123 module:log("debug", "Request for upload slot from a %s", origin.type);
155 162
156 module:add_timer(900, function() 163 module:add_timer(900, function()
157 pending_slots[slot] = nil; 164 pending_slots[slot] = nil;
158 end); 165 end);
159 166
167 measure_slot(filesize);
168
160 origin.log("debug", "Given upload slot %q", slot); 169 origin.log("debug", "Given upload slot %q", slot);
161 170
162 local base_url = module:http_url(); 171 local base_url = module:http_url();
163 local slot_url = url.parse(base_url); 172 local slot_url = url.parse(base_url);
164 slot_url.path = url.parse_path(slot_url.path or "/"); 173 slot_url.path = url.parse_path(slot_url.path or "/");
211 :up(); 220 :up();
212 origin.send(reply); 221 origin.send(reply);
213 return true; 222 return true;
214 end); 223 end);
215 224
225 local measure_upload = function () end
226 if module.measure then
227 -- COMPAT 0.9
228 -- module:measure was added in 0.10
229 measure_upload = module:measure("sizes", "upload");
230 end
231
216 -- http service 232 -- http service
217 local function upload_data(event, path) 233 local function upload_data(event, path)
218 local uploader = pending_slots[path]; 234 local uploader = pending_slots[path];
219 if not uploader then 235 if not uploader then
220 module:log("warn", "Attempt to upload to unknown slot %q", path); 236 module:log("warn", "Attempt to upload to unknown slot %q", path);
250 if not ok then 266 if not ok then
251 module:log("error", "Could not write to file %s for upload: %s", full_filename, err); 267 module:log("error", "Could not write to file %s for upload: %s", full_filename, err);
252 os.remove(full_filename); 268 os.remove(full_filename);
253 return 500; 269 return 500;
254 end 270 end
271 measure_upload(#event.request.body);
255 module:log("info", "File uploaded by %s to slot %s", uploader, random_dir); 272 module:log("info", "File uploaded by %s to slot %s", uploader, random_dir);
256 return 201; 273 return 201;
257 end 274 end
258 275
259 -- FIXME Duplicated from net.http.server 276 -- FIXME Duplicated from net.http.server