-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: implement caching Github avatar's [closes #39]
- Loading branch information
Showing
11 changed files
with
163 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,9 @@ | |
/www/assets/css/*.css | ||
/www/dist | ||
|
||
# Front-end | ||
/www/imgs | ||
|
||
# Composer | ||
/vendor/* | ||
/composer.lock | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace App\Model\Exceptions\Runtime; | ||
|
||
use App\Model\Exceptions\LogicException; | ||
|
||
class InvalidArgumentException extends LogicException | ||
{ | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace App\Model\WebImages; | ||
|
||
use App\Model\Exceptions\Runtime\InvalidArgumentException; | ||
use Nette\Utils\FileSystem; | ||
use Nette\Utils\Image; | ||
|
||
final class GithubImages implements ImageProvider | ||
{ | ||
|
||
// Github URL's | ||
const URL_AVATAR = 'https://github.com/%s'; | ||
|
||
/** @var string */ | ||
private $imageDir; | ||
|
||
/** | ||
* @param string $imageDir | ||
*/ | ||
public function __construct($imageDir) | ||
{ | ||
$this->imageDir = $imageDir; | ||
FileSystem::createDir($imageDir); | ||
} | ||
|
||
/** | ||
* FACTORIES *************************************************************** | ||
*/ | ||
|
||
/** | ||
* @param string $owner | ||
*/ | ||
protected function createAvatar($owner) | ||
{ | ||
$image = @file_get_contents(sprintf(self::URL_AVATAR, $owner)); | ||
if ($image) { | ||
// Save Github avatar to our FS | ||
$filename = $this->imageDir . '/avatar/' . $owner; | ||
FileSystem::write($filename, $image); | ||
|
||
// Send image for first request | ||
$image = Image::fromFile($filename); | ||
$image->send(Image::PNG); | ||
exit; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* API ********************************************************************* | ||
*/ | ||
|
||
/** | ||
* @param array $args | ||
*/ | ||
public function create(array $args) | ||
{ | ||
if (!isset($args['type'])) { | ||
throw new InvalidArgumentException('No type given'); | ||
} | ||
|
||
switch ($args['type']) { | ||
case 'avatar': | ||
$this->createAvatar($this->normalize($args['owner'])); | ||
break; | ||
|
||
default: | ||
throw new InvalidArgumentException('Unknown type "' . $args['type'] . '"given'); | ||
} | ||
} | ||
|
||
/** | ||
* HELPERS ***************************************************************** | ||
* ************************************************************************* | ||
*/ | ||
|
||
/** | ||
* @param string $image | ||
* @return string | ||
*/ | ||
private function normalize($image) | ||
{ | ||
return strtolower(htmlspecialchars($image)); | ||
} | ||
|
||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace App\Model\WebImages; | ||
|
||
interface ImageProvider | ||
{ | ||
|
||
/** | ||
* @param array $args | ||
*/ | ||
public function create(array $args); | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Modules\Front; | ||
|
||
use App\Model\WebImages\GithubImages; | ||
use Nette\Utils\Strings; | ||
|
||
final class WebImagePresenter extends BasePresenter | ||
{ | ||
|
||
/** @var GithubImages @inject */ | ||
public $githubImages; | ||
|
||
/** | ||
* Create and send user avatar from Github | ||
* | ||
* @param string $owner | ||
* @param string $ext | ||
*/ | ||
public function actionAvatar($owner, $ext) | ||
{ | ||
$this->githubImages->create([ | ||
'type' => 'avatar', | ||
'owner' => "$owner.$ext", | ||
]); | ||
} | ||
|
||
} |
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