Skip to content

Commit

Permalink
Fix issue when downloading CV
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro-FA committed Nov 7, 2024
1 parent 9b46a0f commit 1fdb4cf
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 30 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions functions/r2/[[all]].js
Original file line number Diff line number Diff line change
Expand Up @@ -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/", "");

Expand Down
17 changes: 1 addition & 16 deletions lib/repositories/storage_repository.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,26 +10,12 @@ abstract interface class StorageRepository {
class _R2CloudRepository implements StorageRepository {
_R2CloudRepository();

final _client = http.Client();

@override
Future<Uri> getUri(String fileName) async {
final uri = Uri.parse('/r2/$fileName');
if (!await isAvailable(uri)) throw Exception('File $fileName not found');
return uri;
}

Future<bool> isAvailable(Uri uri) async {
final response = await _client.head(uri);
return response.statusCode == 200;
}

void dispose() => _client.close();
Future<Uri> getUri(String fileName) async => Uri.parse('/r2/$fileName');
}

@riverpod
StorageRepository storageRepository(Ref ref) {
final repository = _R2CloudRepository();
ref.onDispose(repository.dispose);
return repository;
}
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ packages:
source: hosted
version: "4.2.0"
http:
dependency: "direct main"
dependency: transitive
description:
name: http
sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010
Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1fdb4cf

Please sign in to comment.