Various updates

This commit is contained in:
2023-06-08 20:49:27 -04:00
parent 3ed5acf75e
commit 9307ba330c
45 changed files with 864 additions and 188 deletions

View File

@ -1,15 +1,15 @@
<?php
namespace ProVM\Common\Service;
use ProVM\Common\Exception\Database\BlankResult;
use Psr\Log\LoggerInterface;
use Ddeboer\Imap\Message\AttachmentInterface;
use Ddeboer\Imap\MessageInterface;
use Nyholm\Psr7\Stream;
use Safe\Exceptions\FilesystemException;
use ProVM\Common\Exception\Message\NoAttachments;
use ProVM\Emails\Model\Message;
use ProVM\Emails\Repository\Attachment;
use Psr\Http\Message\StreamInterface;
use Psr\Log\LoggerInterface;
use Safe\Exceptions\FilesystemException;
use function Safe\{file_get_contents,file_put_contents};
class Attachments extends Base
{
@ -100,13 +100,33 @@ class Attachments extends Base
$attachment->getFullFilename()
]);
}
return \Safe\file_get_contents($filename);
return file_get_contents($filename);
}
public function getAll(): array
{
return $this->getRepository()->fetchAll();
}
public function getDownloadedFiles(): array
{
$downloaded = [];
$folder = $this->getFolder();
$files = new \FilesystemIterator($folder);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$name = $file->getBasename(".{$file->getExtension()}");
list($subject, $date, $filename) = explode(' - ', $name);
try {
$message = $this->getMessages()->find($subject, $date)[0];
$filename = "{$filename}.{$file->getExtension()}";
$downloaded []= compact('message', 'filename');
} catch (BlankResult $e) {
}
}
return $downloaded;
}
public function create(int $message_id): array
{
$message = $this->getMessages()->getRepository()->fetchById($message_id);
@ -183,7 +203,7 @@ class Attachments extends Base
$attachment->getFullFilename()
]);
try {
\Safe\file_put_contents($destination, $remote_attachment->getDecodedContent());
file_put_contents($destination, $remote_attachment->getDecodedContent());
return true;
} catch (FilesystemException $e) {
$this->getLogger()->error($e);
@ -236,6 +256,9 @@ class Attachments extends Base
if (!$message->hasValidAttachments()) {
return false;
}
if ($message->hasDownloadedAttachments()) {
return true;
}
foreach ($remote_message->getAttachments() as $attachment) {
if (!str_contains($attachment->getFilename(), '.pdf')) {
continue;
@ -247,4 +270,59 @@ class Attachments extends Base
}
return true;
}
}
public function find(Message $message, string $filename): \ProVM\Emails\Model\Attachment
{
return $this->getRepository()->fetchByMessageAndFilename($message->getId(), $filename);
}
public function exists(Message $message, string $filename): bool
{
try {
$this->find($message, $filename);
return true;
} catch (BlankResult $e) {
return false;
}
}
public function add(Message $message, string $filename): bool
{
$data = [
'message_id' => $message->getId(),
'filename' => $filename
];
try {
$attachment = $this->getRepository()->create($data);
$attachment->itIsDownloaded();
$this->getRepository()->save($attachment);
$message->doesHaveDownloadedAttachments();
$this->getMessages()->getRepository()->save($message);
return true;
} catch (PDOException $e) {
$this->getLogger()->error($e);
return false;
}
}
public function checkDownloaded(): void
{
$data = $this->getDownloadedFiles();
foreach ($data as $info) {
if (!$this->exists($info['message'], $info['filename'])) {
$this->logger->info("Updating attachment {$info['filename']} for message {$info['message']->getSubject()}");
$this->add($info['message'], $info['filename']);
}
}
}
public function getDownloaded(): array
{
return $this->getRepository()->fetchDownloaded();
}
public function checkEncryption(): void
{
$attachments = $this->getDownloaded();
foreach ($attachments as $attachment) {
if ($attachment->isEncrypted() and !$attachment->isDecrypted()) {
$this->logger->notice("Schedule decrypt for {$attachment->getFullFilename()}");
$this->decrypt($attachment->getMessage(), $attachment->getFilename());
}
}
}
}