Inject always local file device
This commit is contained in:
parent
35441dc542
commit
2667d1fb07
2 changed files with 29 additions and 19 deletions
|
@ -218,6 +218,10 @@ Server::setResource('cacheDevice', function (Document $project) {
|
||||||
return getDevice(APP_STORAGE_CACHE . '/app-' . $project->getId());
|
return getDevice(APP_STORAGE_CACHE . '/app-' . $project->getId());
|
||||||
}, ['project']);
|
}, ['project']);
|
||||||
|
|
||||||
|
Server::setResource('filesLocalDevice', function (Document $project) {
|
||||||
|
return new Local(APP_STORAGE_UPLOADS . '/app-' . $project->getId());
|
||||||
|
}, ['project']);
|
||||||
|
|
||||||
$pools = $register->get('pools');
|
$pools = $register->get('pools');
|
||||||
$platform = new Appwrite();
|
$platform = new Appwrite();
|
||||||
$args = $_SERVER['argv'];
|
$args = $_SERVER['argv'];
|
||||||
|
|
|
@ -59,9 +59,10 @@ class Messaging extends Action
|
||||||
->inject('message')
|
->inject('message')
|
||||||
->inject('log')
|
->inject('log')
|
||||||
->inject('dbForProject')
|
->inject('dbForProject')
|
||||||
->inject('getLocalCache')
|
->inject('filesDevice')
|
||||||
|
->inject('filesLocalDevice')
|
||||||
->inject('queueForUsage')
|
->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,
|
Message $message,
|
||||||
Log $log,
|
Log $log,
|
||||||
Database $dbForProject,
|
Database $dbForProject,
|
||||||
callable $getLocalCache,
|
Device $filesDevice,
|
||||||
|
Device $filesLocalDevice,
|
||||||
Usage $queueForUsage
|
Usage $queueForUsage
|
||||||
): void {
|
): void {
|
||||||
$payload = $message->getPayload() ?? [];
|
$payload = $message->getPayload() ?? [];
|
||||||
|
@ -99,15 +101,19 @@ class Messaging extends Action
|
||||||
case MESSAGE_SEND_TYPE_EXTERNAL:
|
case MESSAGE_SEND_TYPE_EXTERNAL:
|
||||||
$message = $dbForProject->getDocument('messages', $payload['messageId']);
|
$message = $dbForProject->getDocument('messages', $payload['messageId']);
|
||||||
|
|
||||||
$this->sendExternalMessage($dbForProject, $message);
|
$this->sendExternalMessage($dbForProject, $message, $filesDevice, $filesLocalDevice,);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Exception('Unknown message type: ' . $type);
|
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', []);
|
$topicIds = $message->getAttribute('topics', []);
|
||||||
$targetIds = $message->getAttribute('targets', []);
|
$targetIds = $message->getAttribute('targets', []);
|
||||||
$userIds = $message->getAttribute('users', []);
|
$userIds = $message->getAttribute('users', []);
|
||||||
|
@ -211,8 +217,8 @@ class Messaging extends Action
|
||||||
/**
|
/**
|
||||||
* @var array<array> $results
|
* @var array<array> $results
|
||||||
*/
|
*/
|
||||||
$results = batch(\array_map(function ($providerId) use ($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, $localCache, $deviceFiles) {
|
return function () use ($providerId, $identifiers, $providers, $fallback, $message, $dbForProject, $filesDevice, $filesLocalDevice) {
|
||||||
if (\array_key_exists($providerId, $providers)) {
|
if (\array_key_exists($providerId, $providers)) {
|
||||||
$provider = $providers[$providerId];
|
$provider = $providers[$providerId];
|
||||||
} else {
|
} else {
|
||||||
|
@ -238,8 +244,8 @@ class Messaging extends Action
|
||||||
$batches = \array_chunk($identifiers, $maxBatchSize);
|
$batches = \array_chunk($identifiers, $maxBatchSize);
|
||||||
$batchIndex = 0;
|
$batchIndex = 0;
|
||||||
|
|
||||||
return batch(\array_map(function ($batch) use ($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, $localCache, $deviceFiles) {
|
return function () use ($batch, $message, $provider, $adapter, &$batchIndex, $dbForProject, $filesDevice, $filesLocalDevice) {
|
||||||
$deliveredTotal = 0;
|
$deliveredTotal = 0;
|
||||||
$deliveryErrors = [];
|
$deliveryErrors = [];
|
||||||
$messageData = clone $message;
|
$messageData = clone $message;
|
||||||
|
@ -248,7 +254,7 @@ class Messaging extends Action
|
||||||
$data = match ($provider->getAttribute('type')) {
|
$data = match ($provider->getAttribute('type')) {
|
||||||
MESSAGE_TYPE_SMS => $this->buildSmsMessage($messageData, $provider),
|
MESSAGE_TYPE_SMS => $this->buildSmsMessage($messageData, $provider),
|
||||||
MESSAGE_TYPE_PUSH => $this->buildPushMessage($messageData),
|
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)
|
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
|
// Delete any attachments that were downloaded to the local cache
|
||||||
if ($provider->getAttribute('type') === MESSAGE_TYPE_EMAIL) {
|
if ($provider->getAttribute('type') === MESSAGE_TYPE_EMAIL) {
|
||||||
if ($deviceFiles->getType() === Storage::DEVICE_LOCAL) {
|
if ($filesDevice->getType() === Storage::DEVICE_LOCAL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -348,8 +354,8 @@ class Messaging extends Action
|
||||||
|
|
||||||
$path = $file->getAttribute('path', '');
|
$path = $file->getAttribute('path', '');
|
||||||
|
|
||||||
if ($localCache->exists($path)) {
|
if ($filesLocalDevice->exists($path)) {
|
||||||
$localCache->delete($path);
|
$filesLocalDevice->delete($path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -506,8 +512,8 @@ class Messaging extends Action
|
||||||
Database $dbForProject,
|
Database $dbForProject,
|
||||||
Document $message,
|
Document $message,
|
||||||
Document $provider,
|
Document $provider,
|
||||||
Device $deviceFiles,
|
Device $filesDevice,
|
||||||
Device $localCache,
|
Device $filesLocalDevice,
|
||||||
): Email {
|
): Email {
|
||||||
$fromName = $provider['options']['fromName'] ?? null;
|
$fromName = $provider['options']['fromName'] ?? null;
|
||||||
$fromEmail = $provider['options']['fromEmail'] ?? null;
|
$fromEmail = $provider['options']['fromEmail'] ?? null;
|
||||||
|
@ -558,7 +564,7 @@ class Messaging extends Action
|
||||||
$mimes = Config::getParam('storage-mimes');
|
$mimes = Config::getParam('storage-mimes');
|
||||||
$path = $file->getAttribute('path', '');
|
$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);
|
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');
|
$contentType = $file->getAttribute('mimeType');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($deviceFiles->getType() !== Storage::DEVICE_LOCAL) {
|
if ($filesDevice->getType() !== Storage::DEVICE_LOCAL) {
|
||||||
$deviceFiles->transfer($path, $path, $localCache);
|
$filesDevice->transfer($path, $path, $filesLocalDevice);
|
||||||
}
|
}
|
||||||
|
|
||||||
$attachment = new Attachment(
|
$attachment = new Attachment(
|
||||||
|
|
Loading…
Reference in a new issue