last ...
This commit is contained in:
@@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Read a IFormFile Content as byte array ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static async Task<byte[]> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read a File Content by it's Path as byte array ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filePath"></param>
|
||||||
|
/// <param name="bufferSize"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="Exception"></exception>
|
||||||
|
public static async Task<byte[]> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read Text Content of Document ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static async Task<string> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-1
@@ -27,7 +27,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<!-- <PackageReference Include="xDashboard.xExceptions" Version="1.0.0" /> -->
|
<!-- <PackageReference Include="xDashboard.xExceptions" Version="1.0.0" /> -->
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- Local Dependencies -->
|
<!-- Local Dependencies -->
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="../xExceptions/xExceptions.csproj" />
|
<ProjectReference Include="../xExceptions/xExceptions.csproj" />
|
||||||
@@ -40,6 +40,8 @@
|
|||||||
<PackageReference Include="System.Reactive" Version="5.0.0" />
|
<PackageReference Include="System.Reactive" Version="5.0.0" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
<PackageReference Include="libphonenumber-csharp" Version="8.12.17" />
|
<PackageReference Include="libphonenumber-csharp" Version="8.12.17" />
|
||||||
|
<PackageReference Include="UglyToad.PdfPig" Version="1.7.0-custom-5" />
|
||||||
|
<PackageReference Include="Spire.PDFfor.NETStandard" Version="12.5.8" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
|
||||||
|
|||||||
Reference in New Issue
Block a user