Skip to content

Commit

Permalink
Merge pull request #152 from amosproj/dev-simaProData
Browse files Browse the repository at this point in the history
Developement of Sprint 12
  • Loading branch information
Waldleufer authored Jul 7, 2021
2 parents 1fba93c + 4820c78 commit ff74c71
Show file tree
Hide file tree
Showing 60 changed files with 1,725 additions and 1,062 deletions.
3 changes: 3 additions & 0 deletions backend/Backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

secrets.json
Templates/template-docx
Templates/template-modified.docx
Templates/jpegsample.jpg

# User-specific files
*.rsuser
Expand Down
6 changes: 6 additions & 0 deletions backend/Backend/Backend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

<ItemGroup>
<PackageReference Include="AspNetCore.Proxy" Version="4.2.0" />
<PackageReference Include="CoreCompat.libgdiplus" Version="6.0.4-ci-84" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0-preview.5.21301.5" />
</ItemGroup>

<ItemGroup>
Expand All @@ -17,5 +19,9 @@
</Content>
</ItemGroup>

<ItemGroup>
<Folder Include="Templates\" />
</ItemGroup>

</Project>

61 changes: 61 additions & 0 deletions backend/Backend/Controllers/DocumentController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Backend.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Web;
namespace Backend.Controllers
{
[Route("[controller]")]
[ApiController]
public class DocumentController : ControllerBase
{
private readonly DocumentService _documentService;

public DocumentController(DocumentService documentService)
{
_documentService = documentService;
}

//[HttpPost("test")]
//public IActionResult test()
//{
// var files = DocumentService.GetFileModelsFromRequest(this.Request);

// return new OkResult();
//}

[HttpPost("CreateReport")]
public FileContentResult CreateReport()
{
string filepath = _documentService.createReport(this.Request);

string filename = "template-modified.docx";
byte[] filedata = System.IO.File.ReadAllBytes(filepath);
string contentType;
new FileExtensionContentTypeProvider().TryGetContentType(filepath, out contentType);

var cd = new System.Net.Mime.ContentDisposition {
FileName = filename,
Inline = false
};

System.IO.File.Delete(filepath);
Response.Headers.Add("Content-Disposition", cd.ToString());
Response.Headers.Add("Access-Control-Expose-Headers", "*");
//Response.Headers.Add("Content-Transfer-Encoding", "Binary");
//Response.Headers.Add("Content-Encoding", "gzip");
return File(filedata, contentType);
}

[HttpOptions("CreateReport")]
public IActionResult PreflightRoute()
{
return NoContent();
}

}
}
6 changes: 6 additions & 0 deletions backend/Backend/Controllers/SimaProController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ public Task ProxyCatchAll(string rest)
return this.HttpProxyAsync($"{rest}{queryString}", httpProxyOptions);
}

[HttpOptions]
public IActionResult PreflightRoute()
{
return NoContent();
}

}
}
56 changes: 56 additions & 0 deletions backend/Backend/Helper/XmlToDocxHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Xml.Linq;

namespace Backend.Helper
{
public static class XmlToDocxHelper
{

public static void ZipDOCX(this string unzipPath, string fileName)
{

if (File.Exists(fileName))
{
File.Delete(fileName);
}

ZipFile.CreateFromDirectory(unzipPath, fileName);

}

public static string UnzipDOCX(this string zipPath)
{
string[] subs = zipPath.Split('.', StringSplitOptions.RemoveEmptyEntries);

using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
if (Directory.Exists(subs[0]))
{
Directory.Delete(subs[0], true);
}

archive.ExtractToDirectory(subs[0]);

}

return subs[0];
}

//public static string XmlToString(this string xmlDirectory, string xmlPath)
//{
// XDocument document = XDocument.Load(xmlDirectory + xmlPath);
// string docString = document.ToString();

// return docString;
//}

//public static void StringToXml(this string xmlDirectory, string xmlPath, string documentString)
//{
// XDocument xDocument = XDocument.Parse(documentString, LoadOptions.PreserveWhitespace);
// xDocument.Save(xmlDirectory + xmlPath);
//}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
if (request.RequestUri.ToString().Contains("api/calculation"))
{
var requestResponse = await base.SendAsync(request, cancellationToken);
if (!requestResponse.IsSuccessStatusCode)
{
return requestResponse;
}
var contentString = await requestResponse.Content.ReadAsStringAsync();
//Retrieve the CalculationId from the post request
var responseContent = JObject.Parse(contentString);
Expand All @@ -43,7 +47,10 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
while (true)
{
calculationStateResponse = await base.SendAsync(calculationStateRequest, cancellationToken);

if (!calculationStateResponse.IsSuccessStatusCode)
{
return calculationStateResponse;
}
var calculationResponseContent = JObject.Parse(await calculationStateResponse.Content.ReadAsStringAsync());
var calculationResultStatus = calculationResponseContent["Result"].ToString();

Expand Down
116 changes: 116 additions & 0 deletions backend/Backend/Services/DocumentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Drawing;
using System.IO;
using System.ComponentModel;
using Backend.Helper;
using Microsoft.AspNetCore.Http;

namespace Backend.Services
{
public class DocumentService
{
public string createReport(HttpRequest request)
{
var imageNumbers = new List<int>() { 7, 13, 11 };

GetFileModelsFromRequest(request, imageNumbers);

var unzipPath = @"Templates\template-docx.docx".UnzipDOCX();
var docxImagePath = unzipPath + @"\word\media\";


foreach (var number in imageNumbers)
{
File.Delete(docxImagePath + "image" + number.ToString() + ".png");
var image = Image.FromFile(@"Templates\image" + number.ToString() + ".png");
image.Save(docxImagePath + "image" + number.ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);
image.Dispose();
File.Delete(@"Templates\image" + number.ToString() + ".png");
}

unzipPath.ZipDOCX(@"Templates\template-modified.docx");
Directory.Delete(@"Templates\template-docx",true);
return @"Templates\template-modified.docx";

//List<Image> images = new List<Image>() {
// Image.FromFile(@"Templates\image7.png"),
// Image.FromFile(@"Templates\image11.png"),
// Image.FromFile(@"Templates\image14.png")
//};

//File.Delete(imagePath + "image7.png");
//File.Delete(imagePath + "image11.png");
//File.Delete(imagePath + "image14.png");

//images[0].Save(imagePath + "image" + "7.png", System.Drawing.Imaging.ImageFormat.Png);
//images[1].Save(imagePath + "image" + "11.png", System.Drawing.Imaging.ImageFormat.Png);
//images[2].Save(imagePath + "image" + "14.png", System.Drawing.Imaging.ImageFormat.Png);


//File.Delete(@"Templates\image7.png");
//File.Delete(@"Templates\image11.png");
//File.Delete(@"Templates\image14.png");




}

public static void GetFileModelsFromRequest(HttpRequest request, List<int> imageNumbers)
{
foreach (var formField in request.Form)
{
// Form data
var stringValues = formField.Value.Select(x => x.ToString()).Select(y => y.Replace("data:image/jpeg;base64,",""));
var stringValueTuples = stringValues.Zip(imageNumbers);

foreach (var tuple in stringValueTuples)
{
//... process and add to the FileModel list
byte[] bytes = Convert.FromBase64String(tuple.First);

Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
image.Save(@"Templates\" + "image" + tuple.Second.ToString() +".png", System.Drawing.Imaging.ImageFormat.Png);
image.Dispose();
ms.Dispose();
}
}
}
}

//XDocument template = XDocument.Load(@"Templates\template-xml.xml");
//string document = template.ToString();

//var documentModified = ReplaceXmlInlineImageDate(document, @"Templates\jpegsample.jpg", "7");
//documentModified = ReplaceXmlInlineImageDate(documentModified, @"Templates\jpegsample.jpg", "11");
//documentModified = ReplaceXmlInlineImageDate(documentModified, @"Templates\jpegsample.jpg", "14");

//XDocument xDocument = XDocument.Parse(documentModified, LoadOptions.PreserveWhitespace);
//xDocument.Save(@"Templates\template-xml-edited.docx");

//private static string ReplaceXmlInlineImageDate(string document, string imagePath, string imageToBeReplacedNumber)
//{
// //load image from filesystem
// Image image = Image.FromFile(imagePath);
// Bitmap bitmap = new Bitmap(image);
// TypeConverter converter = TypeDescriptor.GetConverter(typeof(Bitmap));
// string imgContent = Convert.ToBase64String((byte[])converter.ConvertTo(bitmap, typeof(byte[])));

// //build document string
// string delimiterOpeningTag = "<pkg:part pkg:name=\"/word/media/image"+imageToBeReplacedNumber+".png\" pkg:contentType=\"image/png\" pkg:compression=\"store\">\r\n <pkg:binaryData>";
// string delimiterClosingTag = "</pkg:binaryData>\r\n </pkg:part>\r\n";
// var documentArray = document.Split(delimiterOpeningTag);
// var documentArray2 = documentArray[1].Split(delimiterClosingTag);
// var restString = String.Join(delimiterClosingTag, documentArray2, 1, documentArray2.Length - 1);
// var documentModified = documentArray[0] + delimiterOpeningTag + imgContent + delimiterClosingTag + restString;
// return documentModified;
//}
}
}
4 changes: 4 additions & 0 deletions backend/Backend/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AspNetCore.Proxy;
using Backend.Security;
using Backend.Middleware;
using Backend.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -49,6 +50,9 @@ public void ConfigureServices(IServiceCollection services)
});

services.AddProxies();

services.AddTransient<DocumentService>();

//Used to get the authenticate/process the Http requests.
services.AddTransient<SimaProLoginDelegatingHandler>();

Expand Down
Binary file added backend/Backend/Templates/jpegsample.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/Backend/Templates/template-docx.docx
Binary file not shown.
Loading

0 comments on commit ff74c71

Please sign in to comment.