From 87ef0c3124e9cc1713720e2174f77bd964698e95 Mon Sep 17 00:00:00 2001 From: Claudia Murialdo Date: Sat, 7 Dec 2024 11:48:40 +0000 Subject: [PATCH] Cherry pick branch 'genexuslabs:rest-service-as-controller' into beta --- .../Middleware/GXRestServices.cs | 16 +++++++--- .../GxClasses/Helpers/HttpHelper.cs | 1 + .../GxClasses/Middleware/GXHttpServices.cs | 30 ++++++++++++------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRestServices.cs b/dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRestServices.cs index dd532140a..4682997eb 100644 --- a/dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRestServices.cs +++ b/dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRestServices.cs @@ -1,5 +1,6 @@ using System; using System.Globalization; +using System.IO; using System.Linq; using System.Net; using System.Net.Http; @@ -98,12 +99,19 @@ protected bool RestParameter(string parameterName, string parameterValue) return false; } } - /*public void UploadImpl(Stream stream) + protected ActionResult UploadImpl() { GXObjectUploadServices gxobject = new GXObjectUploadServices(context); - IncomingWebRequestContext request = WebOperationContext.Current.IncomingRequest; - gxobject.WcfExecute(stream, request.ContentType, request.ContentLength, request.Headers[HttpHeader.XGXFILENAME]); - }*/ + string fileGuid; + string fileToken; + using (Stream stream = Request.Body) + { + fileToken = gxobject.ReadFileFromStream(stream, Request.ContentType, Request.ContentLength.GetValueOrDefault(), Request.Headers[HttpHeader.XGXFILENAME], out fileGuid); + } + Response.Headers.Append(HttpHeader.GX_OBJECT_ID, fileGuid); + SetStatusCode(HttpStatusCode.Created); + return GetResponse(new {object_id = fileToken}); + } protected void ErrorCheck(IGxSilentTrn trn) { if (trn.Errors() == 1) diff --git a/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs b/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs index 10d1ce2b1..8cfdc2bef 100644 --- a/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs +++ b/dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs @@ -48,6 +48,7 @@ public class HttpHeader public static string LAST_MODIFIED = "Last-Modified"; public static string EXPIRES = "Expires"; public static string XGXFILENAME = "x-gx-filename"; + public static string GX_OBJECT_ID = "GeneXus-Object-Id"; internal static string ACCEPT = "Accept"; internal static string TRANSFER_ENCODING = "Transfer-Encoding"; internal static string X_CSRF_TOKEN_HEADER = "X-XSRF-TOKEN"; diff --git a/dotnet/src/dotnetframework/GxClasses/Middleware/GXHttpServices.cs b/dotnet/src/dotnetframework/GxClasses/Middleware/GXHttpServices.cs index b9b993bc1..1c7ea22cb 100644 --- a/dotnet/src/dotnetframework/GxClasses/Middleware/GXHttpServices.cs +++ b/dotnet/src/dotnetframework/GxClasses/Middleware/GXHttpServices.cs @@ -355,7 +355,20 @@ public override void webExecute() internal void WcfExecute(Stream istream, string contentType, long streamLength, string gxFileName) { - string ext=null, fName=null; + + string fileToken = ReadFileFromStream(istream, contentType, streamLength, gxFileName, out string fileGuid); + JObject obj = new JObject(); + obj.Put("object_id", fileToken); + + localHttpContext.Response.AddHeader(HttpHeader.GX_OBJECT_ID, fileGuid); + localHttpContext.Response.ContentType = MediaTypesNames.ApplicationJson; + HttpHelper.SetResponseStatus(localHttpContext, HttpStatusCode.Created, string.Empty); + localHttpContext.Response.Write(obj.ToString()); + } + + internal string ReadFileFromStream(Stream istream, string contentType, long streamLength, string gxFileName, out string fileGuid) + { + string ext = null, fName = null; if (!string.IsNullOrEmpty(gxFileName)) { ext = Path.GetExtension(gxFileName); @@ -373,23 +386,18 @@ internal void WcfExecute(Stream istream, string contentType, long streamLength, { fName = string.Empty; } - string tempDir = Preferences.getTMP_MEDIA_PATH(); - GxFile file = new GxFile(tempDir, FileUtil.getTempFileName(tempDir, fName), GxFileType.PrivateAttribute); + string tempDir = Preferences.getTMP_MEDIA_PATH(); + GxFile file = new GxFile(tempDir, FileUtil.getTempFileName(tempDir, fName), GxFileType.PrivateAttribute); file.Create(new NetworkInputStream(istream, streamLength)); - JObject obj = new JObject(); fName = file.GetURI(); - string fileGuid = GxUploadHelper.GetUploadFileGuid(); + fileGuid = GxUploadHelper.GetUploadFileGuid(); string fileToken = GxUploadHelper.GetUploadFileId(fileGuid); - obj.Put("object_id", fileToken); - localHttpContext.Response.AddHeader("GeneXus-Object-Id", fileGuid); - localHttpContext.Response.ContentType = MediaTypesNames.ApplicationJson; - HttpHelper.SetResponseStatus(localHttpContext, HttpStatusCode.Created, string.Empty); - localHttpContext.Response.Write(obj.ToString()); - GxUploadHelper.CacheUploadFile(fileGuid, $"{Path.GetFileNameWithoutExtension(fName)}.{ext}", ext, file, context); + return fileToken; } + protected override bool IntegratedSecurityEnabled { get