Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/cydrobolt/polr.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/Http/Controllers/Api/ApiLinkController.php')
-rw-r--r--app/Http/Controllers/Api/ApiLinkController.php79
1 files changed, 69 insertions, 10 deletions
diff --git a/app/Http/Controllers/Api/ApiLinkController.php b/app/Http/Controllers/Api/ApiLinkController.php
index e05c25e..b1f377c 100644
--- a/app/Http/Controllers/Api/ApiLinkController.php
+++ b/app/Http/Controllers/Api/ApiLinkController.php
@@ -7,12 +7,22 @@ use App\Helpers\LinkHelper;
use App\Exceptions\Api\ApiException;
class ApiLinkController extends ApiController {
+ protected function getShortenedLink($long_url, $is_secret, $custom_ending, $link_ip, $username, $response_type) {
+ try {
+ $formatted_link = LinkFactory::createLink(
+ $long_url, $is_secret, $custom_ending, $link_ip, $username, false, true);
+ }
+ catch (\Exception $e) {
+ throw new ApiException('CREATION_ERROR', $e->getMessage(), 400, $response_type);
+ }
+
+ return $formatted_link;
+ }
+
public function shortenLink(Request $request) {
$response_type = $request->input('response_type');
$user = $request->user;
- // Validate parameters
- // Encode spaces as %20 to avoid validator conflicts
$validator = \Validator::make(array_merge([
'url' => str_replace(' ', '%20', $request->input('url'))
], $request->except('url')), [
@@ -23,20 +33,69 @@ class ApiLinkController extends ApiController {
throw new ApiException('MISSING_PARAMETERS', 'Invalid or missing parameters.', 400, $response_type);
}
- $long_url = $request->input('url'); // * required
- $is_secret = ($request->input('is_secret') == 'true' ? true : false);
+ $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, 'shorten', $response_type);
+ }
+
+ public function shortenLinksBulk(Request $request) {
+ $response_type = $request->input('response_type', 'json');
+ $request_data = $request->input('data');
+ $user = $request->user;
$link_ip = $request->ip();
- $custom_ending = $request->input('custom_ending');
+ $username = $user->username;
- try {
- $formatted_link = LinkFactory::createLink($long_url, $is_secret, $custom_ending, $link_ip, $user->username, false, true);
+ if ($response_type != 'json') {
+ throw new ApiException('JSON_ONLY', 'Only JSON-encoded responses are available for this endpoint.', 401, $response_type);
}
- catch (\Exception $e) {
- throw new ApiException('CREATION_ERROR', $e->getMessage(), 400, $response_type);
+
+ $links_array_raw_json = json_decode($request_data, true);
+
+ if ($links_array_raw_json === null) {
+ throw new ApiException('INVALID_PARAMETERS', 'Invalid JSON.', 400, $response_type);
}
- return self::encodeResponse($formatted_link, 'shorten', $response_type);
+ $links_array = $links_array_raw_json['links'];
+
+ foreach ($links_array as $link) {
+ $validator = \Validator::make($link, [
+ 'url' => 'required|url'
+ ]);
+
+ if ($validator->fails()) {
+ throw new ApiException('MISSING_PARAMETERS', 'Invalid or missing parameters.', 400, $response_type);
+ }
+ }
+
+ $formatted_links = [];
+
+ foreach ($links_array as $link) {
+ $formatted_link = $this->getShortenedLink(
+ $link['url'],
+ (array_get($link, 'is_secret') == 'true' ? true : false),
+ array_get($link, 'custom_ending'),
+ $link_ip,
+ $username,
+ $response_type
+ );
+
+ $formatted_links[] = [
+ 'long_url' => $link['url'],
+ 'short_url' => $formatted_link
+ ];
+ }
+
+ return self::encodeResponse([
+ 'shortened_links' => $formatted_links
+ ], 'shorten_bulk', 'json');
}
public function lookupLink(Request $request) {