/// <summary> /// This method will process downloading the documents and sanitise using the REST API /// </summary> /// <param name="context"></param> public void ProcessRequest(HttpContext context) { SPUser currentUser = SPContext.Current.Web.CurrentUser; string urlPath = context.Request.Url.ToString(); GWUtility.WriteLog("Processing ... " + urlPath); try { /// This is to handle the download.aspx which is triggered by Download Button if (!string.IsNullOrEmpty(context.Request.QueryString["SourceUrl"])) { urlPath = BuildDocumentUrl(context); } string fileName = Path.GetFileName(urlPath); GWUtility.WriteLog(" Filename: " + fileName); string base64 = GetFileContentAsBase64(urlPath); byte[] regeneratedFileBytes = new GlasswallRebuildClient().RebuildFile(base64); context.Response.Clear(); context.Response.ClearHeaders(); context.Response.Buffer = true; context.Response.ContentType = MediaTypeNames.Application.Octet; context.Response.AddHeader("content-disposition", "attachment;filename=" + fileName); context.Response.AddHeader("content-length", regeneratedFileBytes.Length.ToString()); context.Response.BinaryWrite(regeneratedFileBytes); context.Response.Flush(); context.Response.Close(); } catch (Exception ex) { GWUtility.WriteLog($@"Exception: {ex.Message}\n{ex.StackTrace}"); context.Response.ContentType = MediaTypeNames.Text.Html; context.Response.Write(PrepareErrorMarkup(urlPath, ex)); } }
private static string BuildDocumentUrl(HttpContext context) { string urlPath; var redirectURL = context.Request.QueryString["SourceUrl"]; GWUtility.WriteLog("SourceUrl ... " + redirectURL); var currentSiteUrl = new Uri(SPContext.Current.Site.Url, UriKind.Absolute); var spDomain = string.Format("{0}://{1}:{2}", currentSiteUrl.Scheme, currentSiteUrl.Host, currentSiteUrl.Port); urlPath = spDomain + redirectURL; GWUtility.WriteLog("DocumentUrl ... " + urlPath); return(urlPath); }
public byte[] RebuildFile(string base64) { var request = new JObject() { { "Base64", base64 } }; string payload = JsonConvert.SerializeObject(request); var requestMessage = new HttpRequestMessage(HttpMethod.Post, GWUtility.APIUrl); requestMessage.Headers.TryAddWithoutValidation("x-api-key", GWUtility.APIKey); requestMessage.Headers.TryAddWithoutValidation("accept", "*/*"); requestMessage.Headers.TryAddWithoutValidation("accept-encoding", "gzip, deflate, br"); requestMessage.Headers.TryAddWithoutValidation("accept-language", "en-US,en;q=0.9"); requestMessage.Headers.TryAddWithoutValidation("content-type", "application/json"); requestMessage.Headers.TryAddWithoutValidation("sec-fetch-dest", "empty"); requestMessage.Headers.TryAddWithoutValidation("sec-fetch-mode", "cors"); requestMessage.Headers.TryAddWithoutValidation("sec-fetch-site", "cross-site"); requestMessage.Content = new StringContent(payload, Encoding.UTF8, "application/json"); using (var client = new HttpClient()) { GWUtility.WriteLog($"GW rebuild api: {GWUtility.APIUrl}"); var responseMessage = client.SendAsync(requestMessage).GetAwaiter().GetResult(); GWUtility.WriteLog($"GW rebuild api response status: {responseMessage.StatusCode}"); if (responseMessage.IsSuccessStatusCode) { var responseContent = responseMessage.Content; var regeneratedFile = responseContent.ReadAsStringAsync().GetAwaiter().GetResult(); var regeneratedFileBytes = Convert.FromBase64String(regeneratedFile); return(regeneratedFileBytes); } else { GWUtility.WriteLog("Empty file return from REST API call"); return(new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }); // syntax for empty byte array } } }
public DownloadFileHandler() { GWUtility.LoadConfiguration(SPContext.Current.Site.Url); }