1
0
mirror of https://github.com/cydrobolt/polr.git synced 2024-11-12 21:22:28 +01:00

#comment Add findByLongLink action

This commit is contained in:
Andrey 2019-03-04 15:35:14 +02:00
parent f9622d733b
commit 3cb23f838d
3 changed files with 29 additions and 1 deletions

View File

@ -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.

View File

@ -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);
}
}
}

View File

@ -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']);