Skip to content

Commit

Permalink
Fetch information for local images
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed May 20, 2022
1 parent 911a0da commit a10c1c3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/Actions/FindsContentLengthForImageObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Hyde\Framework\Contracts\ActionContract;
use Hyde\Framework\Models\Image;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @see \Tests\Feature\FindsContentLengthForImageObjectTest
Expand All @@ -12,13 +13,63 @@ class FindsContentLengthForImageObject implements ActionContract
{
protected Image $image;

/**
* Testing adding console debug output
*/
protected OutputInterface $output;

public function __construct(Image $image)
{
$this->image = $image;

$this->output = new \Symfony\Component\Console\Output\ConsoleOutput();
}

public function execute(): int
{
$this->output->writeln('Attempting to find content length for image object...');

if ($this->isImageStoredRemotely()) {
return $this->fetchRemoteImageInformation();
}

return $this->fetchLocalImageInformation();
}

protected function isImageStoredRemotely(): bool
{
return str_starts_with($this->image->getSource(), 'http');
}

protected function fetchRemoteImageInformation(): int
{
$this->output->writeln('Fetching remote image information...');

$headers = get_headers($this->image->getSource(), 1);

if (array_key_exists('Content-Length', $headers)) {
$this->output->writeln('Found content length in headers.');

return (int) $headers['Content-Length'];
}

$this->output->writeln('Could not find content length in headers.');

return 0;
}

protected function fetchLocalImageInformation(): int
{
$this->output->writeln('Fetching local image information...');

if (! file_exists($this->image->getSource())) {
$this->output->writeln('Could not find image file.');

return 0;
}

$this->output->writeln('Found image file.');

return filesize($this->image->getSource());
}
}
15 changes: 14 additions & 1 deletion tests/Feature/FindsContentLengthForImageObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
class FindsContentLengthForImageObjectTest extends TestCase
{
// Test Image helper shorthand returns content length
/**
* Unit test for the shorthand. Logic is tested in the rest of the case.
* @covers \Hyde\Framework\Models\Image::getContentLength
Expand All @@ -22,4 +21,18 @@ public function test_image_helper_shorthand_returns_content_length()
(new Image())->getContentLength()
);
}

// Test it can find the content length for a local image stored in the _media directory.
public function test_it_can_find_the_content_length_for_a_local_image_stored_in_the_media_directory()
{
$image = new Image();
$image->path = '_media/image.jpg';
file_put_contents($image->path, '16bytelongstring');

$this->assertEquals(
16, $image->getContentLength()
);

unlink($image->path);
}
}

0 comments on commit a10c1c3

Please sign in to comment.