Skip to content

Commit

Permalink
Improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalii-bezuhlyi committed May 20, 2024
1 parent 3e1aeae commit eb6f130
Showing 1 changed file with 50 additions and 33 deletions.
83 changes: 50 additions & 33 deletions Utilities/Actions/Files.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,61 +85,78 @@ public async Task<int> GetWordCountInFile([ActionParameter] FileDto file)
var _file = await _fileManagementClient.DownloadAsync(file.File);

var extension = Path.GetExtension(file.File.Name).ToLower();

var filecontent = await ReadDocument(_file, extension);

return CountWords(filecontent);
}

[Action("Convert text to document", Description = "Convert text to txt, doc or docx document.")]
public async Task<ConvertTextToDocumentResponse> ConvertTextToDocument(
[ActionParameter] ConvertTextToDocumentRequest request)
{
var filename = request.Filename + request.FileExtension;
Stream fileStream;
var filename = $"{request.Filename}{request.FileExtension}";
ConvertTextToDocumentResponse response;

if (request.FileExtension == ".txt")
switch (request.FileExtension.ToLower())
{
var bytes = Encoding.UTF8.GetBytes(request.Text);
fileStream = new MemoryStream(bytes);
case ".txt":
response = await ConvertToTextFile(request.Text, filename);
break;
case ".doc":
case ".docx":
response = await ConvertToWordDocument(request.Text, filename);
break;
default:
throw new ArgumentException("Can convert to txt, doc, or docx file only.");
}
else if (request.FileExtension == ".doc" || request.FileExtension == ".docx")

return response;
}

private async Task<ConvertTextToDocumentResponse> ConvertToTextFile(string text, string filename)
{
var bytes = Encoding.UTF8.GetBytes(text);
var file = await _fileManagementClient.UploadAsync(new MemoryStream(bytes), MediaTypeNames.Application.Octet,
filename);

return new ConvertTextToDocumentResponse
{
File = file
};
}

private async Task<ConvertTextToDocumentResponse> ConvertToWordDocument(string text, string filename)
{
var stream = new MemoryStream();

using (var doc = WordprocessingDocument.Create(stream,
DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
{
fileStream = new MemoryStream();
using (var doc = WordprocessingDocument.Create(fileStream,
DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
var mainPart = doc.AddMainDocumentPart();
new Document(new Body()).Save(mainPart);
var body = mainPart.Document.Body!;

var paragraphs = text.Split(new[] { "\n\n" }, StringSplitOptions.None);

foreach (var para in paragraphs)
{
var body = doc.AddMainDocumentPart().Document.Body;
var paragraphs = request.Text.Split(new[] { "\n\n" }, StringSplitOptions.None)
.Select(para => new Paragraph(new Run(new Text(para))));

body.Append(paragraphs);
doc.Save();
var paragraph = new Paragraph(new Run(new Text(para)));
body.Append(paragraph);
}

fileStream.Seek(0, SeekOrigin.Begin);
}
else
{
throw new Exception("Can convert to txt, doc or docx file only.");
mainPart.Document.Save();
}

var file = await _fileManagementClient.UploadAsync(fileStream, GetMimeType(request.FileExtension), filename);
return new ConvertTextToDocumentResponse { File = file };
}
stream.Seek(0, SeekOrigin.Begin);
var file = await _fileManagementClient.UploadAsync(stream,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", filename);

private string GetMimeType(string fileExtension)
{
return fileExtension switch
return new ConvertTextToDocumentResponse
{
".txt" => MediaTypeNames.Application.Octet,
".doc" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
_ => throw new Exception("Unsupported file extension.")
File = file
};
}

private static async Task<string> ReadDocument(Stream file, string fileExtension)
public static async Task<string> ReadDocument(Stream file, string fileExtension)
{
string text;
if (fileExtension == ".txt")
Expand Down

0 comments on commit eb6f130

Please sign in to comment.