diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsNetworking.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsNetworking.cs index bff556d40..845f90559 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsNetworking.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsNetworking.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Collections.Generic; using System.Net.Http; using System.Text; @@ -96,7 +97,7 @@ namespace Barotrauma HandleNetMessage(netMessage, name, client); } - public async void HttpRequest(string url, LuaCsAction callback, string data = null, string method = "POST", string contentType = "application/json", Dictionary headers = null) + public async void HttpRequest(string url, LuaCsAction callback, string data = null, string method = "POST", string contentType = "application/json", Dictionary headers = null, string savePath = null) { try { @@ -114,9 +115,22 @@ namespace Barotrauma { request.Content = new StringContent(data, Encoding.UTF8, contentType); } - + HttpResponseMessage response = await client.SendAsync(request); + if (savePath != null) + { + if (LuaCsFile.IsPathAllowedException(savePath)) + { + byte[] responseData = await response.Content.ReadAsByteArrayAsync(); + + using (var fileStream = new FileStream(savePath, FileMode.Create, FileAccess.Write)) + { + fileStream.Write(responseData, 0, responseData.Length); + } + } + } + string responseBody = await response.Content.ReadAsStringAsync(); GameMain.LuaCs.Timer.Wait((object[] par) => @@ -134,15 +148,15 @@ namespace Barotrauma } } - public void HttpPost(string url, LuaCsAction callback, string data, string contentType = "application/json", Dictionary headers = null) + public void HttpPost(string url, LuaCsAction callback, string data, string contentType = "application/json", Dictionary headers = null, string savePath = null) { - HttpRequest(url, callback, data, "POST", contentType, headers); + HttpRequest(url, callback, data, "POST", contentType, headers, savePath); } - public void HttpGet(string url, LuaCsAction callback, Dictionary headers = null) + public void HttpGet(string url, LuaCsAction callback, Dictionary headers = null, string savePath = null) { - HttpRequest(url, callback, null, "GET", null, headers); + HttpRequest(url, callback, null, "GET", null, headers, savePath); } public void CreateEntityEvent(INetSerializable entity, NetEntityEvent.IData extraData)