From 680ae31549139d2b32d8f58ba09b7ef914b6d0be Mon Sep 17 00:00:00 2001 From: Nadil Karunarathna Date: Tue, 29 Oct 2024 20:58:47 +0530 Subject: [PATCH 1/2] wip --- .gitignore | 3 +- app/.env.example | 59 +---------------------------- app/config/app.php | 2 +- app/tests/Feature/IframeApiTest.php | 30 +++++++++++++++ 4 files changed, 34 insertions(+), 60 deletions(-) create mode 100644 app/tests/Feature/IframeApiTest.php diff --git a/.gitignore b/.gitignore index bf5b0b5..ccef056 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ /vendor/ -.idea -app/ \ No newline at end of file +.idea \ No newline at end of file diff --git a/app/.env.example b/app/.env.example index a1b3de4..83fffe7 100644 --- a/app/.env.example +++ b/app/.env.example @@ -1,66 +1,11 @@ -APP_NAME=Laravel APP_ENV=local -APP_KEY= APP_DEBUG=true -APP_TIMEZONE=UTC -APP_URL=http://localhost - -APP_LOCALE=en -APP_FALLBACK_LOCALE=en -APP_FAKER_LOCALE=en_US - -APP_MAINTENANCE_DRIVER=file -# APP_MAINTENANCE_STORE=database - -PHP_CLI_SERVER_WORKERS=4 - -BCRYPT_ROUNDS=12 - -LOG_CHANNEL=stack -LOG_STACK=single -LOG_DEPRECATIONS_CHANNEL=null -LOG_LEVEL=debug - -DB_CONNECTION=sqlite -# DB_HOST=127.0.0.1 -# DB_PORT=3306 -# DB_DATABASE=laravel -# DB_USERNAME=root -# DB_PASSWORD= - -SESSION_DRIVER=database -SESSION_LIFETIME=120 -SESSION_ENCRYPT=false -SESSION_PATH=/ -SESSION_DOMAIN=null - -BROADCAST_CONNECTION=log -FILESYSTEM_DISK=local -QUEUE_CONNECTION=database +APP_URL=http://127.0.0.1:7272 CACHE_STORE=database CACHE_PREFIX= -MEMCACHED_HOST=127.0.0.1 - REDIS_CLIENT=phpredis REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null -REDIS_PORT=6379 - -MAIL_MAILER=log -MAIL_HOST=127.0.0.1 -MAIL_PORT=2525 -MAIL_USERNAME=null -MAIL_PASSWORD=null -MAIL_ENCRYPTION=null -MAIL_FROM_ADDRESS="hello@example.com" -MAIL_FROM_NAME="${APP_NAME}" - -AWS_ACCESS_KEY_ID= -AWS_SECRET_ACCESS_KEY= -AWS_DEFAULT_REGION=us-east-1 -AWS_BUCKET= -AWS_USE_PATH_STYLE_ENDPOINT=false - -VITE_APP_NAME="${APP_NAME}" +REDIS_PORT=6379 \ No newline at end of file diff --git a/app/config/app.php b/app/config/app.php index f467267..4bcf9c0 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -13,7 +13,7 @@ | */ - 'name' => env('APP_NAME', 'Laravel'), + 'name' => env('APP_NAME', 'Unfold'), /* |-------------------------------------------------------------------------- diff --git a/app/tests/Feature/IframeApiTest.php b/app/tests/Feature/IframeApiTest.php new file mode 100644 index 0000000..ee8ada4 --- /dev/null +++ b/app/tests/Feature/IframeApiTest.php @@ -0,0 +1,30 @@ +getJson('/iframe?'.http_build_query([ + 'url' => 'https://www.youtube.com/watch?v=9bZkp7q19f0', + ])); + + $response->assertStatus(200); + $html = $response->getContent(); + + expect($html)->toContain('') + ->and($html)->toContain('') + ->and($html)->toContain('
and($html)->toContain('src="https://www.youtube.com/embed/9bZkp7q19f0"') + ->and($html)->toContain('style="position: absolute;top:0;left:0;width:100%;height:100%;border:0;"') + ->and($html)->toContain('allow="fullscreen;accelerometer;clipboard-write;encrypted-media;gyroscope;picture-in-picture;web-share;">') + ->and($html)->toContain('const height = document.documentElement.scrollHeight;') + ->and($html)->toContain('function sendDelayedHeight() {') + ->and($html)->toContain('const mutation = new window.MutationObserver(sendDelayedHeight);') + ->and($html)->toContain(`document.addEventListener('resize', sendDelayedHeight);`); +}); + +it('returns error for invalid url', function () { + $response = $this->getJson('/iframe?'.http_build_query([ + 'url' => 'https://invalid.url', + ])); + $response->assertStatus(200); + $response->assertSee('This URL cannot be embedded.'); +}); \ No newline at end of file From 73d997d1ade78f370f63713a0c50c2e1d2ae3feb Mon Sep 17 00:00:00 2001 From: Nadil Karunarathna Date: Tue, 29 Oct 2024 21:52:14 +0530 Subject: [PATCH 2/2] fix --- app/app/Http/Controllers/Controller.php | 32 ++++++++++++++----------- app/tests/Feature/UnfoldApiTest.php | 15 ------------ 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/app/app/Http/Controllers/Controller.php b/app/app/Http/Controllers/Controller.php index 92e7ae8..6e98090 100644 --- a/app/app/Http/Controllers/Controller.php +++ b/app/app/Http/Controllers/Controller.php @@ -16,24 +16,28 @@ public function init(Request $request): JsonResponse { $request->validate([ 'url' => 'required|url', - 'method' => 'nullable|string|in:link,embed,link_embed', + 'method' => 'nullable|string|in:embed,link', 'embedMetaFallback' => 'nullable|boolean', ]); $url = (string) $request->string('url'); - $method = $request->string('method', 'link'); - $method = UnfoldMethod::from($method); - $embedMetaFallback = $request->boolean('embedMetaFallback'); - + $method = (string) $request->string('method', 'link'); try { - $response = Unfold::unfold( - $url, - $method, - new UnfoldConfig( - $embedMetaFallback, - app()->bound('httpClient') ? app('httpClient') : null - ) - ); + if ($method === 'embed') { + $response = Unfold::embed( + $url, + new UnfoldConfig( + app()->bound('httpClient') ? app('httpClient') : null + ) + ); + } else { + $response = Unfold::link( + $url, + new UnfoldConfig( + app()->bound('httpClient') ? app('httpClient') : null + ) + ); + } } catch (UnfoldException $e) { return response()->json([ 'error' => $e->getMessage(), @@ -52,7 +56,7 @@ public function iframe(Request $request): string $url = (string) $request->string('url'); try { - $data = Unfold::unfold($url, UnfoldMethod::EMBED); + $data = Unfold::embed($url); } catch (UnfoldException) { return 'This URL cannot be embedded.'; } diff --git a/app/tests/Feature/UnfoldApiTest.php b/app/tests/Feature/UnfoldApiTest.php index 71390e4..b49880e 100644 --- a/app/tests/Feature/UnfoldApiTest.php +++ b/app/tests/Feature/UnfoldApiTest.php @@ -33,9 +33,7 @@ $response->assertStatus(200); $response->assertJsonFragment([ 'version' => '1.0', - 'method' => 'link', 'url' => 'https://nadil.me/google-cloud-platform', - 'embed' => null, 'title' => 'Google Cloud Platform (GCP): Setting up a GPU-Based Virtual Machine (VM) with Jupyter Notebook for LLMs', 'description' => 'A descriptive guide on how to setup a gpu based vm in google cloud platform', 'authors' => [ @@ -73,20 +71,7 @@ $response->assertStatus(200); $response->assertJsonFragment([ 'version' => '1.0', - 'method' => 'embed', 'url' => 'https://www.youtube.com/watch?v=Bag1gUxuU0g', 'embed' => '
', - 'title' => null, - 'description' => null, - 'authors' => [], - 'tags' => [], - 'siteName' => null, - 'siteUrl' => null, - 'canonicalUrl' => null, - 'publishedTime' => null, - 'modifiedTime' => null, - 'thumbnailUrl' => null, - 'iconUrl' => null, - 'locale' => null, ]); }); \ No newline at end of file