Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggressively simplify and inline action FindsContentLengthForRemoteImageObject #616

Merged
merged 38 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
305fd3b
Move class namespace
caendesilva Oct 28, 2022
ae91036
Fix use function statement
caendesilva Oct 28, 2022
2e673de
Merge multiple method calls into concatenation
caendesilva Oct 28, 2022
47a5de0
First output message needs newline
caendesilva Oct 28, 2022
939e045
Merge branch 'experimental' into refactor-FindsContentLengthForImageO…
caendesilva Oct 28, 2022
7ae0733
Retrieve output from session
caendesilva Oct 28, 2022
aa9cccc
Set default value to false
caendesilva Oct 28, 2022
952e78c
Merge branch 'experimental' into refactor-FindsContentLengthForImageO…
caendesilva Oct 28, 2022
587757a
Set the console output in session so it can be used by actions
caendesilva Oct 28, 2022
4087642
Revert "Set the console output in session so it can be used by actions"
caendesilva Oct 28, 2022
d02d1e5
Put warnings in session
caendesilva Oct 28, 2022
c8aa6f7
Aggressively simplify action
caendesilva Oct 28, 2022
a875b94
Aggressively simplify action even more
caendesilva Oct 28, 2022
55529e7
Inline action FindsContentLengthForRemoteImageObject
caendesilva Oct 28, 2022
07e8aa5
Method getPath must not return basename
caendesilva Oct 28, 2022
b40c2f7
Simplify if with ??
caendesilva Oct 28, 2022
f091053
Change private to protected
caendesilva Oct 28, 2022
6efef4c
Remove unused import
caendesilva Oct 28, 2022
9e0432e
Delete FindsContentLengthForImageObjectTest.php
caendesilva Oct 28, 2022
ff1d89b
Apply fixes from StyleCI
StyleCIBot Oct 28, 2022
d5edccc
Force internal image path property to always begin with _media/
caendesilva Oct 28, 2022
582f8d9
Apply fixes from StyleCI
StyleCIBot Oct 28, 2022
41397e0
Test normalizing
caendesilva Oct 28, 2022
5755159
Assert same
caendesilva Oct 28, 2022
e94af16
Expect normalized path
caendesilva Oct 28, 2022
6b93514
Support media/file as input
caendesilva Oct 28, 2022
c2e488e
Move down method to group constructors
caendesilva Oct 28, 2022
d4f544f
Expect normalized path
caendesilva Oct 28, 2022
866cf16
Revert "Expect normalized path"
caendesilva Oct 28, 2022
e009081
Revert "Expect normalized path"
caendesilva Oct 28, 2022
98c67d5
Fix https://github.com/hydephp/develop/pull/617#issuecomment-1295441165
caendesilva Oct 28, 2022
9fe9a2c
Make helper public
caendesilva Oct 28, 2022
34e4b19
Move up public methods
caendesilva Oct 28, 2022
1eb8944
Apply fixes from StyleCI
StyleCIBot Oct 28, 2022
6de4f9e
Needs the real path
caendesilva Oct 28, 2022
df2a5a7
Check file path is set
caendesilva Oct 29, 2022
f253358
Update FeaturedImageModelTest.php
caendesilva Oct 29, 2022
4a8365a
Merge pull request #617 from hydephp/Force-internal-image-path-proper…
caendesilva Oct 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

namespace Hyde\Framework\Features\Blogging\Models;

use function array_flip;
use function array_key_exists;
use BadMethodCallException;
use function basename;
use function config;
use function e;
use Hyde\Framework\Actions\Constructors\FindsContentLengthForImageObject;
use function file_exists;
use Hyde\Hyde;
use Hyde\Markdown\Contracts\FrontMatter\SubSchemas\FeaturedImageSchema;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use function implode;
use function key;
use Stringable;

/**
Expand All @@ -36,8 +41,13 @@ class FeaturedImage implements FeaturedImageSchema, Stringable
{
/**
* The image's path if it's stored locally.
* The image must be stored in the _media directory.
*
* It no longer matters if you add the _media/ prefix or not,
* as it will be normalized to have the prefix stripped.
*
* @example image.jpg.
* @example _media/image.jpg. (will be normalized to image.jpg)
*/
public ?string $path;

Expand Down Expand Up @@ -102,23 +112,25 @@ class FeaturedImage implements FeaturedImageSchema, Stringable
*/
public ?string $attributionUrl = null;

/**
* @var string The path to the image in the _media directory, relative to the root of the site.
*
* @example "_media/image.jpg".
*/
protected string $sourcePath;

public function __construct(array $data = [])
{
foreach ($data as $key => $value) {
$this->{$key} = $value;
}

if (isset($this->path)) {
$this->path = basename($this->path);
$this->sourcePath = static::normalizeSourcePath($this->path);
$this->path = Str::after($this->sourcePath, '_media/');
}
}

/** @inheritDoc */
public function __toString()
{
return $this->getLink();
}

/** Dynamically create an image based on string or front matter array */
public static function make(string|array $data): static
{
Expand All @@ -136,6 +148,12 @@ public static function fromSource(string $image): static
: new static(['path' => $image]);
}

/** @inheritDoc */
public function __toString()
{
return $this->getLink();
}

public function getSource(): string
{
return $this->url ?? $this->getPath() ?? throw new BadMethodCallException('Attempting to get source from Image that has no source.');
Expand All @@ -146,9 +164,21 @@ public function getLink(): string
return Hyde::image($this->getSource());
}

public function getPath(): ?string
{
return $this->path ?? null;
}

public function getSourcePath(): ?string
{
return $this->sourcePath ?? null;
}

public function getContentLength(): int
{
return (new FindsContentLengthForImageObject($this))->execute();
return (str_starts_with($this->getSource(), 'http')
? $this->getContentLengthFromRemote()
: $this->getLocalContentLength()) ?? 0;
}

public function getFluentAttribution(): HtmlString
Expand Down Expand Up @@ -244,12 +274,40 @@ protected function getAuthorElement(): string
: $this->getAuthorSpan();
}

protected function getPath(): ?string
protected function getContentLengthFromRemote(): ?int
{
if (isset($this->path)) {
return basename($this->path);
$headers = Http::withHeaders([
'User-Agent' => config('hyde.http_user_agent', 'RSS Request Client'),
])->head($this->getSource())->headers();

if (array_key_exists('Content-Length', $headers)) {
return (int) key(array_flip($headers['Content-Length']));
}

return null;
}

protected function getLocalContentLength(): ?int
{
if (isset($this->sourcePath) && file_exists(Hyde::path($this->getSourcePath()))) {
return filesize(Hyde::path($this->getSourcePath()));
}

return null;
}

protected static function normalizeSourcePath(string $path): string
{
$path = Hyde::pathToRelative($path);

if (str_starts_with($path, '_media/')) {
return $path;
}

if (str_starts_with($path, 'media/')) {
return '_'.$path;
}

return '_media/'.$path;
}
}
79 changes: 79 additions & 0 deletions packages/framework/tests/Feature/FeaturedImageModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace Hyde\Framework\Testing\Feature;

use BadMethodCallException;
use function file_put_contents;
use Hyde\Framework\Features\Blogging\Models\FeaturedImage;
use Hyde\Pages\MarkdownPost;
use Hyde\Testing\TestCase;
use Illuminate\Support\Facades\Http;
use function strip_tags;
use function unlink;

/**
* @covers \Hyde\Framework\Features\Blogging\Models\FeaturedImage
Expand Down Expand Up @@ -39,6 +42,30 @@ public function test_make_can_create_an_image_based_on_array()
$this->assertEquals('bar', $image->title);
}

public function test_image_path_is_normalized_to_never_begin_with_media_prefix()
{
$image = FeaturedImage::make('foo');
$this->assertSame('foo', $image->path);

$image = FeaturedImage::make('_media/foo');
$this->assertSame('foo', $image->path);

$image = FeaturedImage::make('_media/foo');
$this->assertSame('foo', $image->path);
}

public function test_image_source_path_is_normalized_to_always_begin_with_media_prefix()
{
$image = FeaturedImage::make('foo');
$this->assertSame('_media/foo', $image->getSourcePath());

$image = FeaturedImage::make('_media/foo');
$this->assertSame('_media/foo', $image->getSourcePath());

$image = FeaturedImage::make('_media/foo');
$this->assertSame('_media/foo', $image->getSourcePath());
}

public function test_from_source_automatically_assigns_proper_property_depending_on_if_the_string_is_remote()
{
$image = FeaturedImage::fromSource('https://example.com/image.jpg');
Expand Down Expand Up @@ -390,6 +417,58 @@ public function test_the_view()
$this->assertEquals('Image by John Doe. License Creative Commons.', $this->stripHtml($component));
}

public function test_it_can_find_the_content_length_for_a_local_image_stored_in_the_media_directory()
{
$image = new FeaturedImage(['path' => 'image.jpg']);
file_put_contents($image->getSourcePath(), '16bytelongstring');

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

unlink($image->getSourcePath());
}

public function test_it_can_find_the_content_length_for_a_remote_image()
{
Http::fake(function () {
return Http::response(null, 200, [
'Content-Length' => 16,
]);
});

$image = new FeaturedImage();
$image->url = 'https://hyde.test/static/image.png';

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

public function test_it_returns_0_if_local_image_is_missing()
{
$image = new FeaturedImage();
$image->path = '_media/image.jpg';

$this->assertEquals(
0, $image->getContentLength()
);
}

public function test_it_returns_0_if_remote_image_is_missing()
{
Http::fake(function () {
return Http::response(null, 404);
});

$image = new FeaturedImage();
$image->url = 'https://hyde.test/static/image.png';

$this->assertEquals(
0, $image->getContentLength()
);
}

protected function stripHtml(string $string): string
{
return trim(strip_newlines(strip_tags($string)), "\t ");
Expand Down
Loading