Software /
code /
prosody-modules
Comparison
mod_http_upload/mod_http_upload.lua @ 2649:abea818eed7b
mod_http_upload: Rename variable to avoid name clash
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 26 Mar 2017 23:07:16 +0200 |
parent | 2648:4c92e2e897c8 |
child | 2650:fef067b57305 |
comparison
equal
deleted
inserted
replaced
2648:4c92e2e897c8 | 2649:abea818eed7b |
---|---|
95 return true; | 95 return true; |
96 end | 96 end |
97 local reply = st.reply(stanza); | 97 local reply = st.reply(stanza); |
98 reply:tag("slot", { xmlns = xmlns }); | 98 reply:tag("slot", { xmlns = xmlns }); |
99 | 99 |
100 local random; | 100 local random_dir; |
101 repeat random = uuid(); | 101 repeat random_dir = uuid(); |
102 until lfs.mkdir(join_path(storage_path, random)) | 102 until lfs.mkdir(join_path(storage_path, random_dir)) |
103 or not lfs.attributes(join_path(storage_path, random, filenams)) | 103 or not lfs.attributes(join_path(storage_path, random_dir, filenams)) |
104 | 104 |
105 datamanager.list_append(origin.username, origin.host, module.name, { | 105 datamanager.list_append(origin.username, origin.host, module.name, { |
106 filename = join_path(storage_path, random, filename), size = filesize, time = os.time() }); | 106 filename = join_path(storage_path, random_dir, filename), size = filesize, time = os.time() }); |
107 local slot = random.."/"..filename; | 107 local slot = random_dir.."/"..filename; |
108 pending_slots[slot] = origin.full_jid; | 108 pending_slots[slot] = origin.full_jid; |
109 local base_url = module:http_url(); | 109 local base_url = module:http_url(); |
110 local slot_url = url.parse(base_url); | 110 local slot_url = url.parse(base_url); |
111 slot_url.path = url.parse_path(slot_url.path or "/"); | 111 slot_url.path = url.parse_path(slot_url.path or "/"); |
112 t_insert(slot_url.path, random); | 112 t_insert(slot_url.path, random_dir); |
113 t_insert(slot_url.path, filename); | 113 t_insert(slot_url.path, filename); |
114 slot_url.path.is_directory = false; | 114 slot_url.path.is_directory = false; |
115 slot_url.path = url.build_path(slot_url.path); | 115 slot_url.path = url.build_path(slot_url.path); |
116 slot_url = url.build(slot_url); | 116 slot_url = url.build(slot_url); |
117 reply:tag("get"):text(slot_url):up(); | 117 reply:tag("get"):text(slot_url):up(); |
143 local uploader = pending_slots[path]; | 143 local uploader = pending_slots[path]; |
144 if not uploader then | 144 if not uploader then |
145 module:log("warn", "Attempt to upload to unknown slot %q", path); | 145 module:log("warn", "Attempt to upload to unknown slot %q", path); |
146 return; -- 404 | 146 return; -- 404 |
147 end | 147 end |
148 local random, filename = path:match("^([^/]+)/([^/]+)$"); | 148 local random_dir, filename = path:match("^([^/]+)/([^/]+)$"); |
149 if not random then | 149 if not random_dir then |
150 module:log("warn", "Invalid file path %q", path); | 150 module:log("warn", "Invalid file path %q", path); |
151 return 400; | 151 return 400; |
152 end | 152 end |
153 if #event.request.body > file_size_limit then | 153 if #event.request.body > file_size_limit then |
154 module:log("warn", "Uploaded file too large %d bytes", #event.request.body); | 154 module:log("warn", "Uploaded file too large %d bytes", #event.request.body); |
155 return 400; | 155 return 400; |
156 end | 156 end |
157 pending_slots[path] = nil; | 157 pending_slots[path] = nil; |
158 local full_filename = join_path(storage_path, random, filename); | 158 local full_filename = join_path(storage_path, random_dir, filename); |
159 if lfs.attributes(full_filename) then | 159 if lfs.attributes(full_filename) then |
160 module:log("warn", "File %s exists already, not replacing it", full_filename); | 160 module:log("warn", "File %s exists already, not replacing it", full_filename); |
161 return 409; | 161 return 409; |
162 end | 162 end |
163 local fh, ferr = io.open(full_filename, "w"); | 163 local fh, ferr = io.open(full_filename, "w"); |
175 if not ok then | 175 if not ok then |
176 module:log("error", "Could not write to file %s for upload: %s", full_filename, err); | 176 module:log("error", "Could not write to file %s for upload: %s", full_filename, err); |
177 os.remove(full_filename); | 177 os.remove(full_filename); |
178 return 500; | 178 return 500; |
179 end | 179 end |
180 module:log("info", "File uploaded by %s to slot %s", uploader, random); | 180 module:log("info", "File uploaded by %s to slot %s", uploader, random_dir); |
181 return 201; | 181 return 201; |
182 end | 182 end |
183 | 183 |
184 -- FIXME Duplicated from net.http.server | 184 -- FIXME Duplicated from net.http.server |
185 | 185 |