1
0
Fork 0
mirror of synced 2024-06-26 10:10:57 +12:00

Inject always local file device

This commit is contained in:
Jake Barnby 2024-02-21 02:50:44 +13:00
parent 35441dc542
commit 2667d1fb07
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C
2 changed files with 29 additions and 19 deletions

View file

@ -218,6 +218,10 @@ Server::setResource('cacheDevice', function (Document $project) {
return getDevice(APP_STORAGE_CACHE . '/app-' . $project->getId());
}, ['project']);
Server::setResource('filesLocalDevice', function (Document $project) {
return new Local(APP_STORAGE_UPLOADS . '/app-' . $project->getId());
}, ['project']);
$pools = $register->get('pools');
$platform = new Appwrite();
$args = $_SERVER['argv'];

View file

@ -59,9 +59,10 @@ class Messaging extends Action
->inject('message')
->inject('log')
->inject('dbForProject')
->inject('getLocalCache')
->inject('filesDevice')
->inject('filesLocalDevice')
->inject('queueForUsage')
->callback(fn(Message $message, Log $log, Database $dbForProject, callable $getLocalCache, Usage $queueForUsage) => $this->action($message, $log, $dbForProject, $getLocalCache, $queueForUsage));
->callback(fn(Message $message, Log $log, Database $dbForProject, Device $filesDevice, Device $filesLocalDevice, Usage $queueForUsage) => $this->action($message, $log, $dbForProject, $filesDevice, $filesLocalDevice, $queueForUsage));
}
/**
@ -77,7 +78,8 @@ class Messaging extends Action
Message $message,
Log $log,
Database $dbForProject,
callable $getLocalCache,
Device $filesDevice,
Device $filesLocalDevice,
Usage $queueForUsage
): void {
$payload = $message->getPayload() ?? [];
@ -99,15 +101,19 @@ class Messaging extends Action
case MESSAGE_SEND_TYPE_EXTERNAL:
$message = $dbForProject->getDocument('messages', $payload['messageId']);
$this->sendExternalMessage($dbForProject, $message);
$this->sendExternalMessage($dbForProject, $message, $filesDevice, $filesLocalDevice,);
break;
default:
throw new Exception('Unknown message type: ' . $type);
}
}
private function sendExternalMessage(Database $dbForProject, Document $message): void
{
private function sendExternalMessage(
Database $dbForProject,
Document $message,
Device $filesDevice,
Device $filesLocalDevice,
): void {
$topicIds = $message->getAttribute('topics', []);
$targetIds = $message->getAttribute('targets', []);
$userIds = $message->getAttribute('users', []);
@ -211,8 +217,8 @@ class Messaging extends Action
/**
* @var array<array> $results
*/
$results = batch(\array_map(function ($providerId) use ($identifiers, $providers, $fallback, $message, $dbForProject, $localCache, $deviceFiles) {
return function () use ($providerId, $identifiers, $providers, $fallback, $message, $dbForProject, $localCache, $deviceFiles) {
$results = batch(\array_map(function ($providerId) use ($identifiers, $providers, $fallback, $message, $dbForProject, $filesDevice, $filesLocalDevice) {
return function () use ($providerId, $identifiers, $providers, $fallback, $message, $dbForProject, $filesDevice, $filesLocalDevice) {
if (\array_key_exists($providerId, $providers)) {
$provider = $providers[$providerId];
} else {
@ -238,8 +244,8 @@ class Messaging extends Action
$batches = \array_chunk($identifiers, $maxBatchSize);
$batchIndex = 0;
return batch(\array_map(function ($batch) use ($message, $provider, $adapter, &$batchIndex, $dbForProject, $localCache, $deviceFiles) {
return function () use ($batch, $message, $provider, $adapter, &$batchIndex, $dbForProject, $localCache, $deviceFiles) {
return batch(\array_map(function ($batch) use ($message, $provider, $adapter, &$batchIndex, $dbForProject, $filesDevice, $filesLocalDevice) {
return function () use ($batch, $message, $provider, $adapter, &$batchIndex, $dbForProject, $filesDevice, $filesLocalDevice) {
$deliveredTotal = 0;
$deliveryErrors = [];
$messageData = clone $message;
@ -248,7 +254,7 @@ class Messaging extends Action
$data = match ($provider->getAttribute('type')) {
MESSAGE_TYPE_SMS => $this->buildSmsMessage($messageData, $provider),
MESSAGE_TYPE_PUSH => $this->buildPushMessage($messageData),
MESSAGE_TYPE_EMAIL => $this->buildEmailMessage($dbForProject, $messageData, $provider, $deviceFiles, $localCache),
MESSAGE_TYPE_EMAIL => $this->buildEmailMessage($dbForProject, $messageData, $provider, $filesDevice, $filesLocalDevice),
default => throw new Exception(Exception::PROVIDER_INCORRECT_TYPE)
};
@ -325,7 +331,7 @@ class Messaging extends Action
// Delete any attachments that were downloaded to the local cache
if ($provider->getAttribute('type') === MESSAGE_TYPE_EMAIL) {
if ($deviceFiles->getType() === Storage::DEVICE_LOCAL) {
if ($filesDevice->getType() === Storage::DEVICE_LOCAL) {
return;
}
@ -348,8 +354,8 @@ class Messaging extends Action
$path = $file->getAttribute('path', '');
if ($localCache->exists($path)) {
$localCache->delete($path);
if ($filesLocalDevice->exists($path)) {
$filesLocalDevice->delete($path);
}
}
}
@ -506,8 +512,8 @@ class Messaging extends Action
Database $dbForProject,
Document $message,
Document $provider,
Device $deviceFiles,
Device $localCache,
Device $filesDevice,
Device $filesLocalDevice,
): Email {
$fromName = $provider['options']['fromName'] ?? null;
$fromEmail = $provider['options']['fromEmail'] ?? null;
@ -558,7 +564,7 @@ class Messaging extends Action
$mimes = Config::getParam('storage-mimes');
$path = $file->getAttribute('path', '');
if (!$deviceFiles->exists($path)) {
if (!$filesDevice->exists($path)) {
throw new Exception(Exception::STORAGE_FILE_NOT_FOUND, 'File not found in ' . $path);
}
@ -568,8 +574,8 @@ class Messaging extends Action
$contentType = $file->getAttribute('mimeType');
}
if ($deviceFiles->getType() !== Storage::DEVICE_LOCAL) {
$deviceFiles->transfer($path, $path, $localCache);
if ($filesDevice->getType() !== Storage::DEVICE_LOCAL) {
$filesDevice->transfer($path, $path, $filesLocalDevice);
}
$attachment = new Attachment(