comparison mod_http_upload/mod_http_upload.lua @ 2468:3bff2848af12

mod_http_upload: Create random directory name when assigning slot
author Kim Alvefur <zash@zash.se>
date Sun, 29 Jan 2017 17:30:25 +0100
parents 290fef768a81
children 43f7637f0143
comparison
equal deleted inserted replaced
2467:290fef768a81 2468:3bff2848af12
82 :tag("max-file-size"):text(tostring(file_size_limit))); 82 :tag("max-file-size"):text(tostring(file_size_limit)));
83 return true; 83 return true;
84 end 84 end
85 local reply = st.reply(stanza); 85 local reply = st.reply(stanza);
86 reply:tag("slot", { xmlns = xmlns_http_upload }); 86 reply:tag("slot", { xmlns = xmlns_http_upload });
87 local random = uuid(); 87
88 local random;
89 repeat random = uuid();
90 until lfs.mkdir(join_path(storage_path, random))
91
88 pending_slots[random.."/"..filename] = origin.full_jid; 92 pending_slots[random.."/"..filename] = origin.full_jid;
89 local base_url = module:http_url(); 93 local base_url = module:http_url();
90 local slot_url = url.parse(base_url); 94 local slot_url = url.parse(base_url);
91 slot_url.path = url.parse_path(slot_url.path or "/"); 95 slot_url.path = url.parse_path(slot_url.path or "/");
92 t_insert(slot_url.path, random); 96 t_insert(slot_url.path, random);
115 end 119 end
116 if #event.request.body > file_size_limit then 120 if #event.request.body > file_size_limit then
117 module:log("warn", "Uploaded file too large %d bytes", #event.request.body); 121 module:log("warn", "Uploaded file too large %d bytes", #event.request.body);
118 return 400; 122 return 400;
119 end 123 end
120 local dirname = join_path(storage_path, random);
121 if not lfs.mkdir(dirname) then
122 module:log("warn", "Could not create directory %s for upload", dirname);
123 return 500;
124 end
125 pending_slots[path] = nil; 124 pending_slots[path] = nil;
126 local full_filename = join_path(dirname, filename); 125 local full_filename = join_path(dirname, filename);
127 local fh, ferr = io.open(full_filename, "w"); 126 local fh, ferr = io.open(full_filename, "w");
128 if not fh then 127 if not fh then
129 module:log("error", "Could not open file %s for upload: %s", full_filename, ferr); 128 module:log("error", "Could not open file %s for upload: %s", full_filename, ferr);