Various updates
This commit is contained in:
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
40
api/common/Service/Install.php
Normal file
40
api/common/Service/Install.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Service;
|
||||
|
||||
use ProVM\Common\Factory\Model;
|
||||
|
||||
class Install
|
||||
{
|
||||
public function __construct(protected Model $factory, protected array $model_list) {}
|
||||
|
||||
public function check(): bool
|
||||
{
|
||||
foreach ($this->model_list as $model_class) {
|
||||
$repository = $this->factory->find($model_class);
|
||||
if (!$repository->isInstalled()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public function install(): void
|
||||
{
|
||||
$check = true;
|
||||
$repository = null;
|
||||
foreach ($this->model_list as $model_class) {
|
||||
$repository = $this->factory->find($model_class);
|
||||
if ($check) {
|
||||
$query = "SET FOREIGN_KEY_CHECKS = 0";
|
||||
$repository->getConnection()->query($query);
|
||||
$check = false;
|
||||
}
|
||||
if (!$repository->isInstalled()) {
|
||||
$repository->install();
|
||||
}
|
||||
}
|
||||
if (!$check) {
|
||||
$query = "SET FOREIGN_KEY_CHECKS = 1";
|
||||
$repository->getConnection()->query($query);
|
||||
}
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ use ProVM\Emails\Repository\State;
|
||||
|
||||
class Mailboxes extends Base
|
||||
{
|
||||
public function __construct(Mailbox $repository, Remote\Mailboxes $remoteService, State\Mailbox $states, LoggerInterface $logger)
|
||||
public function __construct(Mailbox $repository, Remote\Mailboxes $remoteService, State\Mailbox $states, LoggerInterface $logger, protected int $max_update_days)
|
||||
{
|
||||
$this->setRepository($repository)
|
||||
->setRemoteService($remoteService)
|
||||
@ -35,7 +35,6 @@ class Mailboxes extends Base
|
||||
{
|
||||
return $this->statesRepository;
|
||||
}
|
||||
|
||||
public function setRepository(Mailbox $repository): Mailboxes
|
||||
{
|
||||
$this->repository = $repository;
|
||||
@ -110,6 +109,7 @@ class Mailboxes extends Base
|
||||
$this->getStatesRepository()->save($state);
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
$this->getLogger()->error($e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -141,4 +141,13 @@ class Mailboxes extends Base
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public function isUpdated(\ProVM\Emails\Model\Mailbox $mailbox): bool
|
||||
{
|
||||
$states = $mailbox->getStates();
|
||||
if (count($states) === 0) {
|
||||
return false;
|
||||
}
|
||||
$last = $states[count($states) - 1];
|
||||
return abs((int) $last->getDateTime()->diff(new \DateTimeImmutable())->format('%r%a')) < $this->max_update_days;
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ namespace ProVM\Common\Service;
|
||||
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
|
||||
use Ddeboer\Imap\MailboxInterface;
|
||||
use PDOException;
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use ProVM\Common\Exception\Mailbox\Stateless;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Ddeboer\Imap\MessageInterface;
|
||||
@ -14,17 +15,19 @@ use Safe\DateTimeImmutable;
|
||||
|
||||
class Messages extends Base
|
||||
{
|
||||
public function __construct(Mailboxes $mailboxes, Message $repository, Remote\Messages $remoteService, LoggerInterface $logger)
|
||||
public function __construct(Mailboxes $mailboxes, Message $repository, Remote\Messages $remoteService, Jobs $jobsService, LoggerInterface $logger)
|
||||
{
|
||||
$this->setMailboxes($mailboxes)
|
||||
->setRepository($repository)
|
||||
->setRemoteService($remoteService)
|
||||
->setJobsService($jobsService)
|
||||
->setLogger($logger);
|
||||
}
|
||||
|
||||
protected Mailboxes $mailboxes;
|
||||
protected Message $repository;
|
||||
protected Remote\Messages $remoteService;
|
||||
protected Jobs $jobsService;
|
||||
|
||||
public function getMailboxes(): Mailboxes
|
||||
{
|
||||
@ -38,6 +41,10 @@ class Messages extends Base
|
||||
{
|
||||
return $this->remoteService;
|
||||
}
|
||||
public function getJobsService(): Jobs
|
||||
{
|
||||
return $this->jobsService;
|
||||
}
|
||||
|
||||
public function setMailboxes(Mailboxes $mailboxes): Messages
|
||||
{
|
||||
@ -54,6 +61,11 @@ class Messages extends Base
|
||||
$this->remoteService = $service;
|
||||
return $this;
|
||||
}
|
||||
public function setJobsService(Jobs $service): Messages
|
||||
{
|
||||
$this->jobsService = $service;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLocalMessage(string $message_uid): \ProVM\Emails\Model\Message
|
||||
{
|
||||
@ -142,6 +154,7 @@ class Messages extends Base
|
||||
$message->doesHaveValidAttachments();
|
||||
}
|
||||
}
|
||||
error_log(json_encode(compact('message')).PHP_EOL,3,'/logs/debug');
|
||||
$this->getRepository()->save($message);
|
||||
return true;
|
||||
} catch (PDOException $e) {
|
||||
@ -174,4 +187,30 @@ class Messages extends Base
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public function find(string $subject, string $date): array
|
||||
{
|
||||
return $this->repository->fetchAllBySubjectAndDate($subject, new DateTimeImmutable($date));
|
||||
}
|
||||
public function checkUpdate(): void
|
||||
{
|
||||
$registered = $this->getMailboxes()->getRegistered();
|
||||
foreach ($registered as $mailbox) {
|
||||
if (!$this->getMailboxes()->isUpdated($mailbox)) {
|
||||
$this->logger->info("Updating messages from {$mailbox->getName()}");
|
||||
$this->grab($mailbox->getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
public function checkSchedule(): void
|
||||
{
|
||||
$messages = $this->getRepository()->fetchAll();
|
||||
foreach ($messages as $message) {
|
||||
if ($message->hasAttachments() and $message->hasValidAttachments() and !$message->hasDownloadedAttachments() and !$message->hasScheduledDownloads()) {
|
||||
if ($this->getJobsService()->schedule($message->getId())) {
|
||||
$message->doesHaveDownloadedAttachments();
|
||||
$this->getRepository()->save($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user