Skip to content

Commit

Permalink
(feat) impl: CloudflareImagesFs::fileExists
Browse files Browse the repository at this point in the history
  • Loading branch information
nitriques committed Jun 6, 2024
1 parent 7414d2a commit 257fa1c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/fs/CloudflareImagesFs.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,30 @@ public function saveAsset(Asset $asset): void
public function fileExists(string $path): bool
{
// There are no way for us to know if an image exists without its id
if (!$path) {
return false;
}

// Check if the file exists in the recent files
if (isset($this->recentFiles[$path])) {
return true;
}

// Check if the file exists in the cloudflare images
try {
$imageId = Filename::toId(\basename($path));
if (!$imageId) {
throw new \Exception('Failed to parse filename');
}
$image = $this->client->getImage($imageId);
if (isset($image['id'])) {
return true;
}

} catch (\Exception $e) {
// ignore, must not exist
$imageId = null;
}
return false;
}

Expand Down

0 comments on commit 257fa1c

Please sign in to comment.