-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #435 from hydephp/improve-rss-feed-image-handling
Improve RSS image handling and feed and sitemap generation processes
- Loading branch information
Showing
13 changed files
with
443 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Actions; | ||
|
||
use Hyde\Framework\Contracts\ActionContract; | ||
use Hyde\Framework\Models\Image; | ||
use Illuminate\Support\Facades\Http; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @see \Tests\Feature\FindsContentLengthForImageObjectTest | ||
*/ | ||
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 | ||
{ | ||
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->write('<fg=gray> ></> <fg=gray>Fetching remote image information for '.basename($this->image->getSource()).'...</>'); | ||
|
||
$response = Http::withHeaders([ | ||
'User-Agent' => config('hyde.http_user_agent', 'RSS Request Client'), | ||
])->head($this->image->getSource()); | ||
|
||
$headers = $response->headers(); | ||
|
||
if (array_key_exists('Content-Length', $headers)) { | ||
return (int) key(array_flip($headers['Content-Length'])); | ||
} | ||
|
||
$this->write(' > <comment>Warning:</comment> Could not find content length in headers for '.basename($this->image->getSource().'!')); | ||
$this->write(' <fg=gray> Using default content length of 0. '.'</>'); | ||
$this->write(' <fg=gray> Is the image path valid? '.($this->image->getSource()).'</>'); | ||
|
||
return 0; | ||
} | ||
|
||
protected function fetchLocalImageInformation(): int | ||
{ | ||
if (! file_exists($this->image->getSource())) { | ||
$this->write(' > <comment>Warning:</comment> Could not find image file at '.$this->image->getSource().'!'); | ||
$this->write(' <fg=gray> Using default content length of 0. '.'</>'); | ||
|
||
return 0; | ||
} | ||
|
||
return filesize($this->image->getSource()); | ||
} | ||
|
||
protected function write(string $string): void | ||
{ | ||
$this->output->writeln($string); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Commands; | ||
|
||
use Hyde\Framework\Hyde; | ||
use Hyde\Framework\Services\RssFeedService; | ||
use LaravelZero\Framework\Commands\Command; | ||
|
||
/** | ||
* Hyde Command to run the Build Process for the RSS Feed. | ||
* | ||
* @see \Tests\Feature\Commands\HydeBuildRssFeedCommandTest | ||
*/ | ||
class HydeBuildRssFeedCommand extends Command | ||
{ | ||
/** | ||
* The signature of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'build:rss {--no-api : (Not yet supported)}'; | ||
|
||
/** | ||
* The description of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Generate the RSS feed'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle(): int | ||
{ | ||
$actionTime = microtime(true); | ||
|
||
if (! $this->runPreflightCheck()) { | ||
return 1; | ||
} | ||
|
||
$this->comment('Generating RSS feed...'); | ||
file_put_contents(Hyde::getSiteOutputPath(RssFeedService::getDefaultOutputFilename()), RssFeedService::generateFeed()); | ||
$this->line(' > Created <info>'.RssFeedService::getDefaultOutputFilename().'</> in '.$this->getExecutionTimeInMs($actionTime)."ms\n"); | ||
|
||
return 0; | ||
} | ||
|
||
protected function runPreflightCheck(): bool | ||
{ | ||
if (! RssFeedService::canGenerateFeed()) { | ||
$this->error('Cannot generate an RSS feed, please check your configuration.'); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected function getExecutionTimeInMs(float $timeStart): float | ||
{ | ||
return number_format(((microtime(true) - $timeStart) * 1000), 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Commands; | ||
|
||
use Hyde\Framework\Hyde; | ||
use Hyde\Framework\Services\SitemapService; | ||
use LaravelZero\Framework\Commands\Command; | ||
|
||
/** | ||
* Hyde Command to run the Build Process for the Sitemap. | ||
* | ||
* @see \Tests\Feature\Commands\HydeBuildSitemapCommandTest | ||
*/ | ||
class HydeBuildSitemapCommand extends Command | ||
{ | ||
/** | ||
* The signature of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'build:sitemap'; | ||
|
||
/** | ||
* The description of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Generate the sitemap.xml'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle(): int | ||
{ | ||
$actionTime = microtime(true); | ||
|
||
if (! $this->runPreflightCheck()) { | ||
return 1; | ||
} | ||
|
||
$this->comment('Generating sitemap...'); | ||
file_put_contents(Hyde::getSiteOutputPath('sitemap.xml'), SitemapService::generateSitemap()); | ||
$this->line(' > Created <info>sitemap.xml</> in '.$this->getExecutionTimeInMs($actionTime)."ms\n"); | ||
|
||
return 0; | ||
} | ||
|
||
protected function runPreflightCheck(): bool | ||
{ | ||
if (! SitemapService::canGenerateSitemap()) { | ||
$this->error('Cannot generate sitemap.xml, please check your configuration.'); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected function getExecutionTimeInMs(float $timeStart): float | ||
{ | ||
return number_format(((microtime(true) - $timeStart) * 1000), 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.