Mercurial > prosody-hg
comparison plugins/mod_http_file_share.lua @ 11325:76fc73d39092
mod_http_file_share: Factor out function for generating full filename
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Wed, 27 Jan 2021 18:13:15 +0100 |
| parents | 494761f5d7da |
| children | 1ecda954fe97 |
comparison
equal
deleted
inserted
replaced
| 11324:494761f5d7da | 11325:76fc73d39092 |
|---|---|
| 55 }); | 55 }); |
| 56 | 56 |
| 57 -- Convenience wrapper for logging file sizes | 57 -- Convenience wrapper for logging file sizes |
| 58 local function B(bytes) return hi.format(bytes, "B", "b"); end | 58 local function B(bytes) return hi.format(bytes, "B", "b"); end |
| 59 | 59 |
| 60 local function get_filename(slot, create) | |
| 61 return dm.getpath(slot, module.host, module.name, "bin", create) | |
| 62 end | |
| 63 | |
| 60 function may_upload(uploader, filename, filesize, filetype) -- > boolean, error | 64 function may_upload(uploader, filename, filesize, filetype) -- > boolean, error |
| 61 local uploader_host = jid.host(uploader); | 65 local uploader_host = jid.host(uploader); |
| 62 if not ((access:empty() and prosody.hosts[uploader_host]) or access:contains(uploader) or access:contains(uploader_host)) then | 66 if not ((access:empty() and prosody.hosts[uploader_host]) or access:contains(uploader) or access:contains(uploader_host)) then |
| 63 return false, upload_errors.new("access"); | 67 return false, upload_errors.new("access"); |
| 64 end | 68 end |
| 169 return 413; | 173 return 413; |
| 170 -- Note: We don't know the size if the upload is streamed in chunked encoding, | 174 -- Note: We don't know the size if the upload is streamed in chunked encoding, |
| 171 -- so we also check the final file size on completion. | 175 -- so we also check the final file size on completion. |
| 172 end | 176 end |
| 173 | 177 |
| 174 local filename = dm.getpath(upload_info.slot, module.host, module.name, "bin", true); | 178 local filename = get_filename(upload_info.slot, true); |
| 175 | 179 |
| 176 | 180 |
| 177 if not request.body_sink then | 181 if not request.body_sink then |
| 178 module:log("debug", "Preparing to receive upload into %q, expecting %s", filename, B(upload_info.filesize)); | 182 module:log("debug", "Preparing to receive upload into %q, expecting %s", filename, B(upload_info.filesize)); |
| 179 local fh, err = errors.coerce(io.open(filename.."~", "w")); | 183 local fh, err = errors.coerce(io.open(filename.."~", "w")); |
| 227 module:log("debug", "uploads:get(%q) --> %s, %d", slot_id, slot, when); | 231 module:log("debug", "uploads:get(%q) --> %s, %d", slot_id, slot, when); |
| 228 local last_modified = os.date('!%a, %d %b %Y %H:%M:%S GMT', when); | 232 local last_modified = os.date('!%a, %d %b %Y %H:%M:%S GMT', when); |
| 229 if request.headers.if_modified_since == last_modified then | 233 if request.headers.if_modified_since == last_modified then |
| 230 return 304; | 234 return 304; |
| 231 end | 235 end |
| 232 local filename = dm.getpath(slot_id, module.host, module.name, "bin"); | 236 local filename = get_filename(slot_id); |
| 233 local handle, ferr = errors.coerce(io.open(filename)); | 237 local handle, ferr = errors.coerce(io.open(filename)); |
| 234 if not handle then | 238 if not handle then |
| 235 return ferr or 410; | 239 return ferr or 410; |
| 236 end | 240 end |
| 237 response.headers.last_modified = last_modified; | 241 response.headers.last_modified = last_modified; |
