From 1fdb4cfcc78f418799bb0ebe20431f0e395d4285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez?= <61871580+Alejandro-FA@users.noreply.github.com> Date: Thu, 7 Nov 2024 13:38:41 +0100 Subject: [PATCH] Fix issue when downloading CV --- README.md | 8 ++++++-- functions/r2/[[all]].js | 8 ++++++++ lib/repositories/storage_repository.dart | 17 +---------------- package-lock.json | 18 +++++++++--------- package.json | 2 +- pubspec.lock | 2 +- pubspec.yaml | 1 - 7 files changed, 26 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 499be7a..8d2e6c6 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,17 @@ Common development tasks have been unified into single interface of npm scripts npm run upgrade-deps ``` -### Running the development server +### Running a local development server ```bash npm run serve ``` -> You can also run the server in release mode with `npm run serve:release`, which will build the project as the production build. +> Alternatively, you can run the server in release mode with `npm run serve:release`, which will build the project as in a production environment. This also allows to emulate Cloudflare R2 storage by adding files to a local database. For example: +> +> ```bash +> npx wrangler r2 object put cv-documents/alejandro_fernandez_cv-en.pdf --local --file ~/Sync/Personal/CV/alejandro_fernandez_cv-en.pdf +> ``` ### Checking for linting errors diff --git a/functions/r2/[[all]].js b/functions/r2/[[all]].js index c2809df..58e0be1 100644 --- a/functions/r2/[[all]].js +++ b/functions/r2/[[all]].js @@ -4,6 +4,14 @@ function authorizeRequest(key) { return ALLOW_LIST.includes(key); } +export async function onRequestHead(context) { + const path = new URL(context.request.url).pathname.replace("/r2/", ""); + + return authorizeRequest(path) + ? new Response(null, { status: 200 }) + : new Response(null, { status: 404 }); +} + export async function onRequestGet(context) { const path = new URL(context.request.url).pathname.replace("/r2/", ""); diff --git a/lib/repositories/storage_repository.dart b/lib/repositories/storage_repository.dart index 893a016..6f0ab9e 100644 --- a/lib/repositories/storage_repository.dart +++ b/lib/repositories/storage_repository.dart @@ -1,5 +1,4 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:http/http.dart' as http; import 'package:riverpod_annotation/riverpod_annotation.dart'; part 'storage_repository.g.dart'; @@ -11,26 +10,12 @@ abstract interface class StorageRepository { class _R2CloudRepository implements StorageRepository { _R2CloudRepository(); - final _client = http.Client(); - @override - Future getUri(String fileName) async { - final uri = Uri.parse('/r2/$fileName'); - if (!await isAvailable(uri)) throw Exception('File $fileName not found'); - return uri; - } - - Future isAvailable(Uri uri) async { - final response = await _client.head(uri); - return response.statusCode == 200; - } - - void dispose() => _client.close(); + Future getUri(String fileName) async => Uri.parse('/r2/$fileName'); } @riverpod StorageRepository storageRepository(Ref ref) { final repository = _R2CloudRepository(); - ref.onDispose(repository.dispose); return repository; } diff --git a/package-lock.json b/package-lock.json index 5ebd7d0..83d0428 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "portfolio", - "version": "0.4.0", + "version": "0.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "portfolio", - "version": "0.4.0", + "version": "0.5.0", "license": "UNLICENSED", "dependencies": { "@tsconfig/node-lts": "^20.1.3", @@ -14,7 +14,7 @@ "esbuild": "^0.24.0", "fast-xml-parser": "^4.5.0", "tsx": "^4.19.2", - "wrangler": "^3.84.1" + "wrangler": "^3.85.0" }, "devDependencies": { "@eslint/js": "^9.11.0", @@ -1528,9 +1528,9 @@ "license": "MIT" }, "node_modules/cross-spawn": { - "version": "7.0.3", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", - "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", + "version": "7.0.4", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.4.tgz", + "integrity": "sha512-9KdyVPPtLHjPAD7tcuzSFs64UfHlLJt7U6qP4/bFVLyjLceyizj6s6jO6YBaV5d0G7g/9KnY/dOpLR4Rcg8YDg==", "dev": true, "license": "MIT", "dependencies": { @@ -4419,9 +4419,9 @@ } }, "node_modules/wrangler": { - "version": "3.84.1", - "resolved": "https://registry.npmjs.org/wrangler/-/wrangler-3.84.1.tgz", - "integrity": "sha512-w27/QpIk2qz6aMIVi9T8cDcXMvh/RXjcL+vf4o5J2GpQAE4U7wTCNHyaY9H3oTJWRN97KqCAEbiHBNtTKoUJEw==", + "version": "3.85.0", + "resolved": "https://registry.npmjs.org/wrangler/-/wrangler-3.85.0.tgz", + "integrity": "sha512-r5YCWUaF4ApLnloNE6jHHgRYdFzYHoajTlC1tns42UzQ2Ls63VAqD3b0cxOqzDUfmlSb3skpmu0B0Ssi3QWPAg==", "license": "MIT OR Apache-2.0", "dependencies": { "@cloudflare/kv-asset-handler": "0.3.4", diff --git a/package.json b/package.json index 4f2e10c..7b3d53f 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "esbuild": "^0.24.0", "fast-xml-parser": "^4.5.0", "tsx": "^4.19.2", - "wrangler": "^3.84.1" + "wrangler": "^3.85.0" }, "devDependencies": { "@eslint/js": "^9.11.0", diff --git a/pubspec.lock b/pubspec.lock index 06b6362..fc181aa 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -379,7 +379,7 @@ packages: source: hosted version: "4.2.0" http: - dependency: "direct main" + dependency: transitive description: name: http sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010 diff --git a/pubspec.yaml b/pubspec.yaml index 03567be..54818bb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -39,7 +39,6 @@ dependencies: flutter_svg: ^2.0.13 flutter_web_plugins: sdk: flutter - http: ^1.2.2 intl: ^0.19.0 path: ^1.9.0 riverpod_annotation: ^2.6.1