Mercurial > prosody-modules
comparison mod_http_upload/mod_http_upload.lua @ 2678:2dec7cad9218
mod_http_upload: Implement quota support (closes #823)
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 13 Apr 2017 16:35:10 +0200 |
| parents | 6daaa1ad2559 |
| children | 96bf67f1f960 |
comparison
equal
deleted
inserted
replaced
| 2677:6daaa1ad2559 | 2678:2dec7cad9218 |
|---|---|
| 27 return table.concat({ ... }, package.config:sub(1,1)); | 27 return table.concat({ ... }, package.config:sub(1,1)); |
| 28 end | 28 end |
| 29 | 29 |
| 30 -- config | 30 -- config |
| 31 local file_size_limit = module:get_option_number(module.name .. "_file_size_limit", 1024 * 1024); -- 1 MB | 31 local file_size_limit = module:get_option_number(module.name .. "_file_size_limit", 1024 * 1024); -- 1 MB |
| 32 local quota = module:get_option_number(module.name .. "_quota"); | |
| 32 local max_age = module:get_option_number(module.name .. "_expire_after"); | 33 local max_age = module:get_option_number(module.name .. "_expire_after"); |
| 33 | 34 |
| 34 --- sanity | 35 --- sanity |
| 35 local parser_body_limit = module:context("*"):get_option_number("http_max_content_size", 10*1024*1024); | 36 local parser_body_limit = module:context("*"):get_option_number("http_max_content_size", 10*1024*1024); |
| 36 if file_size_limit > parser_body_limit then | 37 if file_size_limit > parser_body_limit then |
| 86 return true; | 87 return true; |
| 87 end); | 88 end); |
| 88 return datamanager.list_store(username, host, module.name, uploads); | 89 return datamanager.list_store(username, host, module.name, uploads); |
| 89 end | 90 end |
| 90 | 91 |
| 92 local function check_quota(username, host, does_it_fit) | |
| 93 if not quota then return true; end | |
| 94 local uploads, err = datamanager.list_load(username, host, module.name); | |
| 95 if not uploads then return true; end | |
| 96 local sum = does_it_fit or 0; | |
| 97 for _, item in ipairs(uploads) do | |
| 98 sum = sum + item.size; | |
| 99 end | |
| 100 return sum < quota; | |
| 101 end | |
| 102 | |
| 91 local function handle_request(origin, stanza, xmlns, filename, filesize) | 103 local function handle_request(origin, stanza, xmlns, filename, filesize) |
| 92 -- local clients only | 104 -- local clients only |
| 93 if origin.type ~= "c2s" then | 105 if origin.type ~= "c2s" then |
| 94 module:log("debug", "Request for upload slot from a %s", origin.type); | 106 module:log("debug", "Request for upload slot from a %s", origin.type); |
| 95 origin.send(st.error_reply(stanza, "cancel", "not-authorized")); | 107 origin.send(st.error_reply(stanza, "cancel", "not-authorized")); |
| 109 elseif filesize > file_size_limit then | 121 elseif filesize > file_size_limit then |
| 110 module:log("debug", "File too large (%d > %d)", filesize, file_size_limit); | 122 module:log("debug", "File too large (%d > %d)", filesize, file_size_limit); |
| 111 origin.send(st.error_reply(stanza, "modify", "not-acceptable", "File too large") | 123 origin.send(st.error_reply(stanza, "modify", "not-acceptable", "File too large") |
| 112 :tag("file-too-large", {xmlns=xmlns}) | 124 :tag("file-too-large", {xmlns=xmlns}) |
| 113 :tag("max-file-size"):text(tostring(file_size_limit))); | 125 :tag("max-file-size"):text(tostring(file_size_limit))); |
| 126 return true; | |
| 127 elseif not check_quota(origin.username, origin.host, filesize) then | |
| 128 module:log("debug", "Upload of %dB by %s would exceed quota", filesize, origin.full_jid); | |
| 129 origin.send(st.error_reply(stanza, "wait", "resource-constraint", "Quota reached")); | |
| 114 return true; | 130 return true; |
| 115 end | 131 end |
| 116 local reply = st.reply(stanza); | 132 local reply = st.reply(stanza); |
| 117 reply:tag("slot", { xmlns = xmlns }); | 133 reply:tag("slot", { xmlns = xmlns }); |
| 118 | 134 |
