MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / restore

Method restore

utils.py:244–280  ·  view source on GitHub ↗
(self, handler)

Source from the content-addressed store, hash-verified

242 handler.wfile.write(json.dumps({"status": "error", "message": str(e)}).encode('utf-8'))
243
244 def restore(self, handler):
245 try:
246 if cgi is None:
247 # CGI module not available, send error response
248 handler.send_response(500)
249 handler.send_header("Content-type", "application/json")
250 handler.end_headers()
251 handler.wfile.write(json.dumps({"status": "error", "message": "File upload not supported in this Python version"}).encode('utf-8'))
252 return
253
254 content_length = int(handler.headers['Content-Length'])
255 field_data = handler.rfile.read(content_length)
256 field_storage = cgi.FieldStorage(fp=io.BytesIO(field_data), headers=handler.headers, environ={'REQUEST_METHOD': 'POST'})
257
258 file_item = field_storage['file']
259 if file_item.filename:
260 backup_path = os.path.join(self.shared_data.upload_dir, file_item.filename)
261 with open(backup_path, 'wb') as output_file:
262 output_file.write(file_item.file.read())
263
264 with zipfile.ZipFile(backup_path, 'r') as backup_zip:
265 backup_zip.extractall(self.shared_data.currentdir)
266
267 handler.send_response(200)
268 handler.send_header("Content-type", "application/json")
269 handler.end_headers()
270 handler.wfile.write(json.dumps({"status": "success", "message": "Restore completed successfully"}).encode('utf-8'))
271 else:
272 handler.send_response(400)
273 handler.send_header("Content-type", "application/json")
274 handler.end_headers()
275 handler.wfile.write(json.dumps({"status": "error", "message": "No selected file"}).encode('utf-8'))
276 except Exception as e:
277 handler.send_response(500)
278 handler.send_header("Content-type", "application/json")
279 handler.end_headers()
280 handler.wfile.write(json.dumps({"status": "error", "message": str(e)}).encode('utf-8'))
281
282 def download_backup(self, handler):
283 query = unquote(handler.path.split('?filename=')[1])

Callers

nothing calls this directly

Calls 3

writeMethod · 0.45
encodeMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected