Skip to content

Commit

Permalink
Merge branch 'pdf_checker'
Browse files Browse the repository at this point in the history
  • Loading branch information
decimo3 committed Dec 19, 2023
2 parents 3382720 + 2cda45e commit 91b8d82
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
13 changes: 8 additions & 5 deletions Handles/HandleInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,14 @@ async public Task SendDocument()
{
foreach (string fatura in respostas)
{
if (fatura == "None" || fatura == null || fatura == "")
{
continue;
}
await using Stream stream = System.IO.File.OpenRead(@$"{cfg.CURRENT_PATH}\tmp\{fatura}");
if (fatura == "None" || fatura == null || fatura == "") continue;
if(!PdfChecker.PdfCheck($"./tmp/{fatura}", request.informacao!))
throw new InvalidOperationException("ERRO: A fatura recuperada não corresponde com a solicitada!");
}
foreach (string fatura in respostas)
{
if (fatura == "None" || fatura == null || fatura == "") continue;
await using Stream stream = System.IO.File.OpenRead($"./tmp/{fatura}");
await bot.SendDocumentAsyncWraper(user.id, stream, fatura);
stream.Dispose();
await bot.sendTextMesssageWraper(user.id, fatura, false);
Expand Down
27 changes: 27 additions & 0 deletions Helpers/PdfChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace telbot;
public static class PdfChecker
{
public static bool PdfCheck(String filepath, String instalacao)
{
try
{
if(!System.IO.File.Exists(filepath)) return false;
var reader = new iTextSharp.text.pdf.PdfReader(filepath);
var text = new System.Text.StringBuilder();
for (int page = 1; page <= reader.NumberOfPages; page++) {
var strategy = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();
string currentText = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, page, strategy);
currentText = System.Text.Encoding.UTF8.GetString(System.Text.ASCIIEncoding.Convert(
System.Text.Encoding.Default, System.Text.Encoding.UTF8, System.Text.Encoding.Default.GetBytes(currentText)));
text.Append(currentText);
}
reader.Close();
String result = text.ToString();
return result.Contains(instalacao);
}
catch
{
return false;
}
}
}
1 change: 1 addition & 0 deletions telbot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<PackageReference Include="dotenv.net" Version="3.1.2" />
<PackageReference Include="iTextSharp" Version="5.5.13.3" />
<PackageReference Include="JWT" Version="10.0.0" />
<PackageReference Include="System.Data.SQLite" Version="1.0.117" />
<PackageReference Include="System.Management" Version="7.0.2" />
Expand Down

0 comments on commit 91b8d82

Please sign in to comment.