generated from amosproj/amos202Xss0Y-projname
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from amosproj/dev-simaProData
Developement of Sprint 12
- Loading branch information
Showing
60 changed files
with
1,725 additions
and
1,062 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
//} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
//} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Oops, something went wrong.