← All changes
|
app/src/Controllers/Ajax/MediaAssetsAPIAjaxController.php
+60
-31
1.3.8
→
1.9.2
View file →
| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | namespace Depicter\Controllers\Ajax; |
| 3 | 3 | |
| 4 | 4 | use Depicter; |
| 5 | 5 | use Depicter\GuzzleHttp\Exception\ConnectException; |
| 6 | +use Depicter\GuzzleHttp\Exception\GuzzleException; | |
| 6 | 7 | use Depicter\Services\AssetsAPIService; |
| 7 | 8 | use Depicter\Utility\Sanitize; |
| 8 | 9 | use Psr\Http\Message\RequestInterface; |
| 9 | 10 | use Psr\Http\Message\ResponseInterface; |
| @@ -19,15 +20,15 @@ | ||
| 19 | 20 | class MediaAssetsAPIAjaxController |
| 20 | 21 | { |
| 21 | 22 | |
| 22 | 23 | /** |
| 23 | - * search Photos | |
| 24 | + * Search Photos | |
| 24 | 25 | * |
| 25 | 26 | * @param RequestInterface $request |
| 26 | 27 | * @param string $view |
| 27 | 28 | * |
| 28 | 29 | * @return ResponseInterface |
| 29 | - * @throws \Depicter\GuzzleHttp\Exception\GuzzleException | |
| 30 | + * @throws GuzzleException | |
| 30 | 31 | */ |
| 31 | 32 | public function searchImages( RequestInterface $request, $view ) |
| 32 | 33 | { |
| 33 | 34 | // available values are => "landscape", "portrait", "squarish" |
| @@ -54,8 +55,9 @@ | ||
| 54 | 55 | 'orderBy' => $orderBy |
| 55 | 56 | ]; |
| 56 | 57 | try { |
| 57 | 58 | $result = AssetsAPIService::searchAssets( 'photos', $options ); |
| 59 | + | |
| 58 | 60 | return \Depicter::json( $result ); |
| 59 | 61 | |
| 60 | 62 | } catch ( ConnectException $exception ) { |
| 61 | 63 | return \Depicter::json([ |
| @@ -61,13 +63,64 @@ | ||
| 61 | 63 | return \Depicter::json([ |
| 62 | 64 | 'errors' => [ 'Connection error ..', $exception->getMessage() ] |
| 63 | 65 | ])->withStatus(503); |
| 64 | 66 | } catch ( \Exception $exception ) { |
| 67 | + // set status code to 404 if 404 Not found was in response of unsplash API | |
| 68 | + $statusCode = false !== strpos( $exception->getMessage(), '404' ) ? 404 : 200; | |
| 65 | 69 | return \Depicter::json([ |
| 66 | 70 | 'errors' => [ $exception->getMessage() ] |
| 71 | + ])->withStatus( $statusCode ); | |
| 72 | + } | |
| 73 | + | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Hotlinks to a media src | |
| 78 | + * | |
| 79 | + * @param Request $request | |
| 80 | + * @param string $view | |
| 81 | + * | |
| 82 | + * @return ResponseInterface | |
| 83 | + * @throws \Exception | |
| 84 | + */ | |
| 85 | + public function getMedia( Request $request, $view ) { | |
| 86 | + $id = ! empty( $request->query('id' ) ) ? Sanitize::textfield( $request->query('id' ) ) : ''; | |
| 87 | + $size = ! empty( $request->query('size') ) ? Sanitize::textfield( $request->query('size') ) : ''; | |
| 88 | + | |
| 89 | + $mediaHotlinkUrl = \Depicter::media()->getSourceUrl( $id ); | |
| 90 | + $clientKey = \Depicter::auth()->getClientKey(); | |
| 91 | + | |
| 92 | + return Depicter::redirect()->to( $mediaHotlinkUrl ) | |
| 93 | + ->withHeader( 'Access-Control-Allow-Origin', '*' ) | |
| 94 | + ->withHeader( 'X-DEPICTER-CKEY', $clientKey ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * Retrieves url of a media by size | |
| 99 | + * | |
| 100 | + * @param Request $request | |
| 101 | + * @param string $view | |
| 102 | + * | |
| 103 | + * @return ResponseInterface | |
| 104 | + * @throws \Exception | |
| 105 | + */ | |
| 106 | + public function getMediaUrl( Request $request, $view ) { | |
| 107 | + $id = ! empty( $request->query('id' ) ) ? Sanitize::textfield( $request->query('id' ) ) : ''; | |
| 108 | + $size = ! empty( $request->query('size') ) ? Sanitize::textfield( $request->query('size') ) : 'large'; | |
| 109 | + | |
| 110 | + try { | |
| 111 | + $mediaHotlinkUrl = \Depicter::media()->getSourceUrl( $id, $size ); | |
| 112 | + return \Depicter::json([ 'url'=> $mediaHotlinkUrl ]); | |
| 113 | + | |
| 114 | + } catch ( GuzzleException $exception ) { | |
| 115 | + return \Depicter::json([ | |
| 116 | + 'errors' => [ 'Connection error ..', $exception->getMessage() ] | |
| 117 | + ])->withStatus(503); | |
| 118 | + } catch ( \Exception $exception ) { | |
| 119 | + return \Depicter::json([ | |
| 120 | + 'errors' => [ $exception->getMessage() ] | |
| 67 | 121 | ]); |
| 68 | 122 | } |
| 69 | - | |
| 70 | 123 | } |
| 71 | 124 | |
| 72 | 125 | /** |
| 73 | 126 | * Search pixabay Videos |
| @@ -75,9 +128,9 @@ | ||
| 75 | 128 | * @param RequestInterface $request |
| 76 | 129 | * @param $view |
| 77 | 130 | * |
| 78 | 131 | * @return ResponseInterface |
| 79 | - * @throws \Depicter\GuzzleHttp\Exception\GuzzleException | |
| 132 | + * @throws GuzzleException | |
| 80 | 133 | */ |
| 81 | 134 | public function searchVideos( RequestInterface $request, $view ) { |
| 82 | 135 | $options = $this->getPixabayQueryParams( $request ); |
| 83 | 136 | try { |
| @@ -92,19 +145,18 @@ | ||
| 92 | 145 | return \Depicter::json([ |
| 93 | 146 | 'errors' => [ $exception->getMessage() ] |
| 94 | 147 | ]); |
| 95 | 148 | } |
| 96 | - | |
| 97 | 149 | } |
| 98 | 150 | |
| 99 | 151 | /** |
| 100 | - * Query pixabay vectors | |
| 152 | + * Query Pixabay vectors | |
| 101 | 153 | * |
| 102 | 154 | * @param Request $request |
| 103 | 155 | * @param string $view |
| 104 | 156 | * |
| 105 | - * @return \Psr\Http\Message\ResponseInterface | |
| 106 | - * @throws \Depicter\GuzzleHttp\Exception\GuzzleException | |
| 157 | + * @return ResponseInterface | |
| 158 | + * @throws GuzzleException | |
| 107 | 159 | */ |
| 108 | 160 | public function searchVectors( Request $request, $view ) { |
| 109 | 161 | |
| 110 | 162 | $options = $this->getPixabayQueryParams( $request ); |
| @@ -122,31 +174,8 @@ | ||
| 122 | 174 | ]); |
| 123 | 175 | } |
| 124 | 176 | |
| 125 | 177 | } |
| 126 | - | |
| 127 | - /** | |
| 128 | - * Return Pixabay vector hot links | |
| 129 | - * | |
| 130 | - * @param Request $request | |
| 131 | - * @param string $view | |
| 132 | - * | |
| 133 | - * @return ResponseInterface | |
| 134 | - * @throws \Exception | |
| 135 | - */ | |
| 136 | - public function getMedia( Request $request, $view ) { | |
| 137 | - $id = ! empty( $request->query('id' ) ) ? Sanitize::textfield( $request->query('id' ) ) : ''; | |
| 138 | - $size = ! empty( $request->query('size') ) ? Sanitize::textfield( $request->query('size') ) : ''; | |
| 139 | - $forcePreview = ! empty( $request->query('forcePreview') ) ? Sanitize::textfield( $request->query('forcePreview') ) : 'false'; | |
| 140 | - | |
| 141 | - $mediaHotlinkUrl = \Depicter::media()->getSourceUrl( $id, $size, $forcePreview ); | |
| 142 | - $clientKey = \Depicter::options()->get( 'client_key' ); | |
| 143 | - | |
| 144 | - return Depicter::redirect()->to( $mediaHotlinkUrl ) | |
| 145 | - ->withHeader( 'Access-Control-Allow-Origin', '*' ) | |
| 146 | - ->withHeader( 'X-DEPICTER-CKEY', $clientKey ); | |
| 147 | - } | |
| 148 | - | |
| 149 | 178 | |
| 150 | 179 | /** |
| 151 | 180 | * Get sanitized query params |
| 152 | 181 | * |