Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find by long link #508

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Factories/LinkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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.
Expand Down
32 changes: 32 additions & 0 deletions app/Http/Controllers/Api/ApiLinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,36 @@ public function lookupLink(Request $request) {
throw new ApiException('NOT_FOUND', 'Link not found.', 404, $response_type);
}
}

public function findByLongLink(Request $request)
{
$response_type = $request->input('response_type');
$user = $request->user;

$validator = \Validator::make(array_merge([
'url' => str_replace(' ', '%20', $request->input('url'))
], $request->except('url')), [
'url' => 'required|url'
]);

if ($validator->fails()) {
throw new ApiException('MISSING_PARAMETERS', 'Invalid or missing parameters.', 400, $response_type);
}

$short_url = LinkHelper::longLinkExists($request->input('url'), $user->username);
if (!empty($short_url)) {
$formatted_link = LinkFactory::formatLink($short_url);
} else {
$formatted_link = $this->getShortenedLink(
$request->input('url'),
($request->input('is_secret') == 'true' ? true : false),
$request->input('custom_ending'),
$request->ip(),
$user->username,
$response_type
);
}

return self::encodeResponse($formatted_link, 'find_by_long', $response_type);
}
}
3 changes: 3 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
$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']);
Expand Down