Skip to content

Commit 8911e3f

Browse files
committed
Removed http fetching from image base64 generation
1 parent 7d38c96 commit 8911e3f

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

app/Uploads/ImageService.php

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -418,36 +418,25 @@ public function deleteUnusedImages($checkRevisions = true, $dryRun = true, $type
418418

419419
/**
420420
* Convert a image URI to a Base64 encoded string.
421-
* Attempts to find locally via set storage method first.
421+
* Attempts to convert the URL to a system storage url then
422+
* fetch the data from the disk or storage location.
423+
* Returns null if the image data cannot be fetched from storage.
422424
* @throws FileNotFoundException
423425
*/
424426
public function imageUriToBase64(string $uri): ?string
425427
{
426-
$isLocal = strpos(trim($uri), 'http') !== 0;
427-
428-
// Attempt to find local files even if url not absolute
429-
$base = url('/');
430-
if (!$isLocal && strpos($uri, $base) === 0) {
431-
$isLocal = true;
432-
$uri = str_replace($base, '', $uri);
428+
$storagePath = $this->imageUrlToStoragePath($uri);
429+
if (empty($uri) || is_null($storagePath)) {
430+
return null;
433431
}
434432

433+
$storage = $this->getStorage();
435434
$imageData = null;
436-
437-
if ($isLocal) {
438-
$uri = trim($uri, '/');
439-
$storage = $this->getStorage();
440-
if ($storage->exists($uri)) {
441-
$imageData = $storage->get($uri);
442-
}
443-
} else {
444-
try {
445-
$imageData = $this->http->fetch($uri);
446-
} catch (Exception $e) {
447-
}
435+
if ($storage->exists($storagePath)) {
436+
$imageData = $storage->get($storagePath);
448437
}
449438

450-
if ($imageData === null) {
439+
if (is_null($imageData)) {
451440
return null;
452441
}
453442

@@ -459,6 +448,36 @@ public function imageUriToBase64(string $uri): ?string
459448
return 'data:image/' . $extension . ';base64,' . base64_encode($imageData);
460449
}
461450

451+
/**
452+
* Get a storage path for the given image URL.
453+
* Returns null if the url cannot be resolved to a local URL.
454+
*/
455+
private function imageUrlToStoragePath(string $url): ?string
456+
{
457+
$url = trim($url);
458+
459+
// Handle potential relative paths
460+
$isRelative = strpos($url, 'http') !== 0;
461+
if ($isRelative) {
462+
return trim($url, '/');
463+
}
464+
465+
// Handle local images based on paths on the same domain
466+
$potentialHostPaths = [
467+
url('/'),
468+
$this->getPublicUrl('/'),
469+
];
470+
471+
foreach ($potentialHostPaths as $potentialBasePath) {
472+
$potentialBasePath = strtolower($potentialBasePath);
473+
if (strpos(strtolower($url), $potentialBasePath) === 0) {
474+
return trim(substr($url, strlen($potentialBasePath)), '/');
475+
}
476+
}
477+
478+
return null;
479+
}
480+
462481
/**
463482
* Gets a public facing url for an image by checking relevant environment variables.
464483
* If s3-style store is in use it will default to guessing a public bucket URL.

0 commit comments

Comments
 (0)