-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract URL shortening, create API endpoints & logic
- Loading branch information
Showing
15 changed files
with
219 additions
and
73 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ env.*.php | |
env | ||
.env.php | ||
.env | ||
.env.bak | ||
.env.example | ||
vendor/ | ||
composer.phar |
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,100 @@ | ||
<?php | ||
namespace App\Factories; | ||
|
||
use App\Models\Link; | ||
use App\Helpers\CryptoHelper; | ||
use App\Helpers\LinkHelper; | ||
|
||
|
||
class LinkFactory { | ||
private 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. | ||
* @param string $link_ending | ||
* @param boolean $secret_ending | ||
* @return string | ||
*/ | ||
$short_url = env('APP_PROTOCOL') . env('APP_ADDRESS') . '/' . $link_ending; | ||
|
||
if ($secret_ending) { | ||
$short_url .= '/' . $secret_ending; | ||
} | ||
|
||
return $short_url; | ||
} | ||
|
||
public static function createLink($long_url, $is_secret=false, $custom_ending=null, $link_ip='127.0.0.1', $creator=false) { | ||
/** | ||
* Given parameters needed to create a link, generate appropriate ending and | ||
* return formatted link. | ||
* | ||
* @param string $custom_ending | ||
* @param boolean (optional) $is_secret | ||
* @param string (optional) $custom_ending | ||
* @param string $link_ip | ||
* @param string $creator | ||
* @return string $formatted_link | ||
*/ | ||
|
||
$is_already_short = LinkHelper::checkIfAlreadyShortened($long_url); | ||
|
||
if ($is_already_short) { | ||
throw new \Exception('Sorry, but your link already | ||
looks like a shortened URL.'); | ||
} | ||
|
||
if (!$is_secret && !$custom_ending && $existing_link = LinkHelper::longLinkExists($long_url)) { | ||
// if link is not specified as secret, is non-custom, and | ||
// already exists in Polr, lookup the value and return | ||
return self::formatLink($existing_link); | ||
} | ||
|
||
if ($custom_ending) { | ||
// has custom ending | ||
$ending_conforms = LinkHelper::validateEnding($custom_ending); | ||
if (!$ending_conforms) { | ||
throw new \Exception('Sorry, but custom endings | ||
can only contain alphanumeric characters'); | ||
} | ||
|
||
$ending_in_use = LinkHelper::linkExists($custom_ending); | ||
if ($ending_in_use) { | ||
throw new \Exception('Sorry, but this URL ending is already in use.'); | ||
} | ||
|
||
$link_ending = $custom_ending; | ||
} | ||
else { | ||
// no custom ending | ||
$link_ending = LinkHelper::findSuitableEnding(); | ||
} | ||
|
||
$link = new Link; | ||
$link->short_url = $link_ending; | ||
$link->long_url = $long_url; | ||
$link->ip = $link_ip; | ||
$link->is_custom = $custom_ending != null; | ||
|
||
if ($creator) { | ||
// if user is logged in, save user as creator | ||
$link->creator = $creator; | ||
} | ||
|
||
if ($is_secret) { | ||
$rand_bytes_num = intval(env('POLR_SECRET_BYTES')); | ||
$secret_key = CryptoHelper::generateRandomHex($rand_bytes_num); | ||
$link->secret_key = $secret_key; | ||
} | ||
else { | ||
$secret_key = false; | ||
} | ||
|
||
$link->save(); | ||
|
||
$formatted_link = self::formatLink($link_ending, $secret_key); | ||
|
||
return $formatted_link; | ||
} | ||
|
||
} |
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,12 @@ | ||
<?php | ||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Models\User; | ||
use App\Helpers\ApiHelper; | ||
|
||
class ApiHelper { | ||
public static function checkUserApiQuota($username) { | ||
// pass | ||
return true; | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Models\User; | ||
use App\Helpers\ApiHelper; | ||
|
||
class ApiController extends ApiController { | ||
protected static function getApiUserInfo(Request $request) { | ||
$api_key = $request->input('api_key'); | ||
$user = User::where('active', 1) | ||
->where('api_key', $api_key) | ||
->where('api_active', true) | ||
->first(); | ||
|
||
$api_limited_reached = ApiHelper::checkUserApiQuota($user->username); | ||
} | ||
|
||
protected static function encodeResponse($result, $action, $response_type='json') { | ||
$response = { | ||
"action" => $action, | ||
"result" => $result | ||
} | ||
|
||
if ($response_type == 'json') { | ||
return json_encode($response); | ||
} | ||
else if ($response_type == 'plain_text') { | ||
return $result; | ||
} | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Factories\LinkFactory; | ||
use App\Helpers\LinkHelper; | ||
|
||
class ApiLinkController extends ApiController { | ||
public static function shortenLink(Request $request) { | ||
$response_type = $request->input('response_type'); | ||
$ard = self::getApiUserInfo($request); | ||
|
||
/* */ | ||
$long_url = $request->input('url'); | ||
$is_secret = $request->input('is_secret'); | ||
$custom_ending = $request->input('custom_ending'); | ||
|
||
$formatted_link = LinkFactory::createLink(); | ||
|
||
return self::encodeResponse($formatted_link, 'shorten', $response_type); | ||
} | ||
|
||
public static function lookupLink(Request $request) { | ||
$response_type = $request->input('response_type'); | ||
$ard = self::getApiUserInfo($request); | ||
|
||
/* */ | ||
$url_ending = $request->input('url_ending'); | ||
$link_or_false = LinkHelper::linkExists($url_ending); | ||
|
||
if ($link_or_false) { | ||
return $link_or_false; | ||
} | ||
else { | ||
abort(404, "Link not found."); | ||
} | ||
|
||
} | ||
} |
Empty file.
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
Oops, something went wrong.