Comparison

mod_http_upload/mod_http_upload.lua @ 2467:290fef768a81

mod_http_upload: Forget upload slot under some error conditions
author Kim Alvefur <zash@zash.se>
date Sun, 29 Jan 2017 17:22:37 +0100
parent 2445:e822900c87d4
child 2468:3bff2848af12
comparison
equal deleted inserted replaced
2466:349008f9452d 2467:290fef768a81
101 return true; 101 return true;
102 end); 102 end);
103 103
104 -- http service 104 -- http service
105 local function upload_data(event, path) 105 local function upload_data(event, path)
106 if not pending_slots[path] then 106 local uploader = pending_slots[path];
107 if not uploader then
107 module:log("warn", "Attempt to upload to unknown slot %q", path); 108 module:log("warn", "Attempt to upload to unknown slot %q", path);
108 return; -- 404 109 return; -- 404
109 end 110 end
110 local random, filename = path:match("^([^/]+)/([^/]+)$"); 111 local random, filename = path:match("^([^/]+)/([^/]+)$");
111 if not random then 112 if not random then
119 local dirname = join_path(storage_path, random); 120 local dirname = join_path(storage_path, random);
120 if not lfs.mkdir(dirname) then 121 if not lfs.mkdir(dirname) then
121 module:log("warn", "Could not create directory %s for upload", dirname); 122 module:log("warn", "Could not create directory %s for upload", dirname);
122 return 500; 123 return 500;
123 end 124 end
125 pending_slots[path] = nil;
124 local full_filename = join_path(dirname, filename); 126 local full_filename = join_path(dirname, filename);
125 local fh, ferr = io.open(full_filename, "w"); 127 local fh, ferr = io.open(full_filename, "w");
126 if not fh then 128 if not fh then
127 module:log("error", "Could not open file %s for upload: %s", full_filename, ferr); 129 module:log("error", "Could not open file %s for upload: %s", full_filename, ferr);
128 return 500; 130 return 500;
137 if not ok then 139 if not ok then
138 module:log("error", "Could not write to file %s for upload: %s", full_filename, err); 140 module:log("error", "Could not write to file %s for upload: %s", full_filename, err);
139 os.remove(full_filename); 141 os.remove(full_filename);
140 return 500; 142 return 500;
141 end 143 end
142 module:log("info", "File uploaded by %s to slot %s", pending_slots[path], random); 144 module:log("info", "File uploaded by %s to slot %s", uploader, random);
143 pending_slots[path] = nil;
144 return 200; 145 return 200;
145 end 146 end
146 147
147 -- FIXME Duplicated from net.http.server 148 -- FIXME Duplicated from net.http.server
148 149