From f9c6671dd7f683b2f59d5d9d76c864ebc2db86a2 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sat, 1 Aug 2026 10:53:09 +0330 Subject: [PATCH] last ... --- Extensions/IFormFileExtensions.cs | 149 ++++++++++++++++++++++++++++++ xCommons.csproj | 4 +- 2 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 Extensions/IFormFileExtensions.cs diff --git a/Extensions/IFormFileExtensions.cs b/Extensions/IFormFileExtensions.cs new file mode 100644 index 0000000..f3a7081 --- /dev/null +++ b/Extensions/IFormFileExtensions.cs @@ -0,0 +1,149 @@ +using System; +using System.IO; +using System.Linq; +using System.Text; +using UglyToad.PdfPig; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using xCommons.Extensions; + +namespace xAiModels.Extensions +{ + public static class IFormFileExtensions + { + /// + /// Read a IFormFile Content as byte array ... + /// + /// + /// + /// + public static async Task ReadAsBytes( + this IFormFile source, + CancellationToken cancellationToken = default + ) + { + // + // Create and use a Memory Stream ... + using var stream = new MemoryStream(); + + // + // Copy File Content to Stream ... + await source.CopyToAsync( + target: stream, + cancellationToken: cancellationToken + ); + + // + // Read Stream Content as byte array ... + var result = stream.ToArray(); + + // + return result; + } + + /// + /// Read a File Content by it's Path as byte array ... + /// + /// + /// + /// + /// + /// + public static async Task ReadAsBytes( + this string filePath, + int bufferSize = 81920, + CancellationToken cancellationToken = default + ) + { + // + if (string.IsNullOrWhiteSpace(filePath)) + { + throw new Exception("Invalid Args ..."); + } + + // + // Create and use a Memory Stream ... + using var memStream = new MemoryStream(); + + // + using var stream = new FileStream( + path: filePath, + mode: FileMode.Open, + access: FileAccess.Read, + share: FileShare.ReadWrite + ); + + // + // Copy File Content to Stream ... + await stream.CopyToAsync( + destination: memStream, + bufferSize: bufferSize, + cancellationToken: cancellationToken + ); + + // + // Read Stream Content as byte array ... + var result = memStream.ToArray(); + + // + return result; + } + + /// + /// Read Text Content of Document ... + /// + /// + /// + /// + public static async Task ReadContent( + this IFormFile source, + CancellationToken cancellationToken = default + ) + { + // + // Check if File PDF or not ... + var isPDF = source.ContentType + .ToNormalString() + .Contains("pdf" + .ToNormalString()); + + // + var result = string.Empty; + + // + var isValidFileType = isPDF; + if (!isValidFileType) + { + return result; + } + + // + if (isPDF) + { + // + // Reading File Content as Byte array ... + var fileBytes = await source.ReadAsBytes(cancellationToken); + + // + // Create a PDF Document ... + using var stream = new MemoryStream(fileBytes); + using var document = PdfDocument.Open(stream); + + // + // Extract PDF Text ... + var textBuilder = new StringBuilder(); + foreach (var page in document.GetPages()) + { + textBuilder.Append(page.Text); + } + + // + result = textBuilder.ToString(); + } + + // + return result; + } + } +} \ No newline at end of file diff --git a/xCommons.csproj b/xCommons.csproj index 96e6ca3..5629189 100644 --- a/xCommons.csproj +++ b/xCommons.csproj @@ -27,7 +27,7 @@ - + @@ -40,6 +40,8 @@ + +