diff --git a/app/Factories/LinkFactory.php b/app/Factories/LinkFactory.php index 7ff2bda..9f3e504 100644 --- a/app/Factories/LinkFactory.php +++ b/app/Factories/LinkFactory.php @@ -9,7 +9,7 @@ use App\Helpers\LinkHelper; class LinkFactory { const MAXIMUM_LINK_LENGTH = 65535; - private static function formatLink($link_ending, $secret_ending=false) { + public static function formatLink($link_ending, $secret_ending=false) { /** * Given a link ending and a boolean indicating whether a secret ending is needed, * return a link formatted with app protocol, app address, and link ending. diff --git a/app/Http/Controllers/Api/ApiLinkController.php b/app/Http/Controllers/Api/ApiLinkController.php index b1f377c..1169c17 100644 --- a/app/Http/Controllers/Api/ApiLinkController.php +++ b/app/Http/Controllers/Api/ApiLinkController.php @@ -137,4 +137,29 @@ class ApiLinkController extends ApiController { throw new ApiException('NOT_FOUND', 'Link not found.', 404, $response_type); } } + + public function findByLongLink(Request $request) { + + $response_type = $request->input('response_type', 'json'); + if ($response_type != 'json') { + throw new ApiException('JSON_ONLY', 'Only JSON-encoded responses are available for this endpoint.', 401, $response_type); + } + + $long_url = urldecode(trim($request->input('long_url'))); + if (empty($long_url)) { + throw new ApiException('MISSING_PARAMETERS', 'Invalid or missing parameters.', 400, $response_type); + } + + $user = $request->user; + + $short_url = LinkHelper::longLinkExists($long_url, $user->username); + if ($short_url) { + return self::encodeResponse([ + 'short_url' => LinkFactory::formatLink($short_url), + 'long_url' => $long_url + ], 'find_by_long', 'json'); + } else { + throw new ApiException('NOT_FOUND', 'Link not found.', 404, $response_type); + } + } } diff --git a/app/Http/routes.php b/app/Http/routes.php index e936cb1..8598a74 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -72,6 +72,9 @@ $app->group(['prefix' => '/api/v2', 'namespace' => 'App\Http\Controllers\Api', ' $app->post('action/lookup', ['as' => 'api_lookup_url', 'uses' => 'ApiLinkController@lookupLink']); $app->get('action/lookup', ['as' => 'api_lookup_url', 'uses' => 'ApiLinkController@lookupLink']); + /* API find long link endpoints */ + $app->get('action/find_by_long', ['as' => 'api_find_by_long_url', 'uses' => 'ApiLinkController@findByLongLink']); + /* API data endpoints */ $app->get('data/link', ['as' => 'api_link_analytics', 'uses' => 'ApiAnalyticsController@lookupLinkStats']); $app->post('data/link', ['as' => 'api_link_analytics', 'uses' => 'ApiAnalyticsController@lookupLinkStats']);