Skip to content

Commit

Permalink
cache remote
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardokum committed Feb 22, 2024
1 parent 3bd08cb commit e41a4b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/mail-auto-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@
'curl' => [
'connect_timeout' => 5, // Seconds
'timeout' => 10, // Seconds
'cache' => false,
'cache_ttl' => 3600,
]
];
18 changes: 18 additions & 0 deletions src/Embedder/AttachmentEmbedder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Eduardokum\LaravelMailAutoEmbed\Embedder;

use Exception;
use Illuminate\Support\Facades\Cache;
use Swift_Message;
use Swift_EmbeddedFile;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -102,6 +103,15 @@ public function fromEntity(EmbeddableEntity $entity)
public function fromRemoteUrl($url)
{
if (filter_var($url, FILTER_VALIDATE_URL)) {
$hashName = implode('_', [
'laravel-mail-auto-embed',
hash('sha256', $url)
]);

if (config('mail-auto-embed.curl.cache', false) && $file = Cache::get($hashName)) {
return $this->embed($file['content'], $file['name'], $file['type']);
}

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Expand All @@ -119,6 +129,14 @@ public function fromRemoteUrl($url)
parse_str($queryStr, $queryParams);
$basename = $queryParams['basename'] ?? $pathInfo['basename'];

if (config('mail-auto-embed.curl.cache', false)) {
Cache::put($hashName, [
'content' => $raw,
'name' => $basename,
'type' => $contentType
], config('mail-auto-embed.curl.cache_ttl', 3600));
}

return $this->embed($raw, $basename, $contentType);
}
}
Expand Down

0 comments on commit e41a4b6

Please sign in to comment.