/** * Shorten a new URL * * @return Response */ public function postShorten() { // No big url if (!\Input::has('bigurl')) { return \Response::json(array('error' => array('code' => 'MISSING-PARAMETERS', 'http_code' => '400', 'message' => 'Bad Request')), 400); } $bigURL = \Input::get('bigurl'); $user = $this->apiKey->user; // No user linked to API key - SHOULD NEVER HAPPEN if (!isset($user)) { return \Response::json(array('error' => array('code' => 'NOT-AUTH', 'http_code' => '403', 'message' => 'Forbidden: SHOULD NEVER HAPPEN!')), 403); } // User has gone over quota so cant shorten if ($user->quota_max != 0 && $user->quota_used + 1 > $user->quota_max) { return \Response::json(array('error' => array('code' => 'QUOTA-USED', 'http_code' => '400', 'message' => 'Bad Request')), 403); } if (filter_var($bigURL, FILTER_VALIDATE_URL) === false) { return \Response::json(array('error' => array('code' => 'URL-INVALID', 'http_code' => '400', 'message' => 'Bad Request')), 400); } $dbLink = \Link::where('destination', '=', $bigURL)->first(); if (!isset($dbLink)) { $dbLink = new \Link(); $dbLink->user_id = $user->id; $dbLink->code = $dbLink->generateCode(); $dbLink->destination = $bigURL; $dbLink->clicks = "0"; $dbLink->save(); $user->quota_used += 1; $user->save(); } $linkCode = $dbLink->code; $linkURL = \Request::root() . '/' . $linkCode; return \Response::json(array('ok' => array('code' => 'LINK-SHORTENED', 'http_code' => '200', 'message' => 'OK', 'data' => array('url' => $linkURL, 'url_code' => $linkCode))), 200); }
/** * Shorten a new URL * * @return Response */ public function postShorten() { if (!\Auth::check()) { return \Response::json(array('error' => array('code' => 'NOT-AUTH', 'http_code' => '403', 'message' => 'Forbidden')), 403); } $shortenValidator = \Validator::make(\Input::all(), array('url' => 'required|url')); if ($shortenValidator->fails()) { return \Response::json(array('error' => array('code' => 'SHORTEN-VALIDATOR-FAILED', 'http_code' => '400', 'message' => 'Bad Request', 'data' => array('validator_messages' => $shortenValidator->messages()))), 400); } $bigURL = \Input::get('url'); // User has gone over quota so cant shorten if (\Auth::user()->quota_max != 0 && \Auth::user()->quota_used + 1 > \Auth::user()->quota_max) { return \Response::json(array('error' => array('code' => 'QUOTA-USED', 'http_code' => '400', 'message' => 'Bad Request')), 403); } $dbLink = \Link::where('destination', '=', $bigURL)->first(); if (!isset($dbLink)) { $dbLink = new \Link(); $dbLink->user_id = \Auth::user()->id; $dbLink->code = $dbLink->generateCode(); $dbLink->destination = $bigURL; $dbLink->clicks = "0"; $dbLink->save(); \Auth::user()->quota_used += 1; \Auth::user()->save(); } $linkCode = $dbLink->code; $linkURL = \Request::root() . '/' . $linkCode; return \Response::json(array('ok' => array('code' => 'LINK-SHORTENED', 'http_code' => '200', 'message' => 'OK', 'data' => array('url' => $linkURL, 'url_code' => $linkCode))), 200); }