Skip to content

Commit

Permalink
Use HEAD request instead of md5_file to get etag when missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Randy Čupić committed Oct 24, 2024
1 parent dca4631 commit 67e7eab
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
8 changes: 7 additions & 1 deletion bundle/Resources/config/services/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,19 @@ services:
class: Cloudinary
factory: ['@netgen_remote_media.provider.cloudinary.factory.cloudinary_instance', 'create']

netgen_remote_media.provider.cloudinary.factory.md5_file_hash:
class: Netgen\RemoteMedia\Core\Provider\Cloudinary\Factory\Md5FileHash
public: false
arguments:
- '@netgen_remote_media.factory.md5_file_hash'

netgen_remote_media.provider.cloudinary.factory.remote_resource:
class: Netgen\RemoteMedia\Core\Provider\Cloudinary\Factory\RemoteResource
public: false
arguments:
- '@netgen_remote_media.provider.cloudinary.converter.resource_type'
- '@netgen_remote_media.provider.cloudinary.converter.visibility_type'
- '@netgen_remote_media.factory.md5_file_hash'
- '@netgen_remote_media.provider.cloudinary.factory.md5_file_hash'

netgen_remote_media.provider.cloudinary.factory.search_result:
class: Netgen\RemoteMedia\Core\Provider\Cloudinary\Factory\SearchResult
Expand Down
67 changes: 67 additions & 0 deletions lib/Core/Provider/Cloudinary/Factory/Md5FileHash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Netgen\RemoteMedia\Core\Provider\Cloudinary\Factory;

use Netgen\RemoteMedia\API\Factory\FileHash as FileHashFactoryInterface;
use Symfony\Component\HttpFoundation\Response;

use function count;
use function curl_close;
use function curl_exec;
use function curl_getinfo;
use function curl_init;
use function curl_setopt;
use function explode;
use function trim;

use const CURLINFO_HTTP_CODE;
use const CURLOPT_HEADERFUNCTION;
use const CURLOPT_NOBODY;

final class Md5FileHash implements FileHashFactoryInterface
{
public function __construct(
private readonly FileHashFactoryInterface $fileHashFactory,
) {}

public function createHash(string $path): string
{
return $this->getUsingHead($path) ?? $this->fileHashFactory->createHash($path);
}

private function getUsingHead(string $path): ?string
{
$ch = curl_init($path);
$headers = [];

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt(
$ch,
CURLOPT_HEADERFUNCTION,
static function ($curl, $header) use (&$headers) {
$len = mb_strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) {
return $len;
}

$headers[mb_strtolower(trim($header[0]))][] = trim($header[1]);

return $len;
},
);

$status = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

if ($status !== true || $code !== Response::HTTP_OK) {
return null;
}

return ($headers['etag'][0] ?? null) ? trim($headers['etag'][0], ' "\'\t\n\r\0\v') : null;
}
}

0 comments on commit 67e7eab

Please sign in to comment.