Skip to content

An open-source cross-platform alternative to AirDrop

feat: improve PIN visualization

Tien Do Nam67d58a335bca
diff --git a/app/assets/web/main.js b/app/assets/web/main.js
index 212446ed..1d19e2f2 100644
--- a/app/assets/web/main.js
+++ b/app/assets/web/main.js
@@ -2,11 +2,23 @@ const BASE_URL = '/api/localsend/v2';
let i18n = {};
let sessionId = sessionStorage.getItem('sessionId');
+let queryPin = new URLSearchParams(window.location.search).get('pin');
async function requestFiles() {
document.getElementById('status-text').innerText = i18n.waiting;
+
+ let initialUrl = new URL(`${BASE_URL}/prepare-download`, document.location);
+ if (sessionId) {
+ initialUrl.searchParams.append('sessionId', sessionId);
+ }
+ if (queryPin) {
+ initialUrl.searchParams.append('pin', queryPin);
+ }
+
+ console.log(initialUrl);
+
let response = await fetch(
- `${BASE_URL}/prepare-download?sessionId=${sessionId}`,
+ initialUrl,
{
method: 'POST',
},
diff --git a/app/lib/pages/web_send_page.dart b/app/lib/pages/web_send_page.dart
index 5a98f9c3..c8170cff 100644
--- a/app/lib/pages/web_send_page.dart
+++ b/app/lib/pages/web_send_page.dart
@@ -140,6 +140,10 @@ class _WebSendPageState extends State<WebSendPage> with Refena {
children: [
...networkState.localIps.map((ip) {
final url = '${_encrypted ? 'https' : 'http'}://$ip:${serverState.port}';
+ final urlWithPin = switch (webSendState.pin) {
+ String() => '$url/?pin=${Uri.encodeQueryComponent(webSendState.pin!)}',
+ null => url,
+ };
return Padding(
padding: const EdgeInsets.all(5),
child: Row(
@@ -166,8 +170,10 @@ class _WebSendPageState extends State<WebSendPage> with Refena {
await showDialog(
context: context,
builder: (_) => QrDialog(
- data: url,
+ data: urlWithPin,
+ label: url,
listenIncomingWebSendRequests: true,
+ pin: webSendState.pin,
),
);
},
@@ -181,14 +187,15 @@ class _WebSendPageState extends State<WebSendPage> with Refena {
await showDialog(
context: context,
builder: (_) => ZoomDialog(
- data: url,
+ label: url,
+ pin: webSendState.pin,
listenIncomingWebSendRequests: true,
),
);
},
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 2),
- child: Icon(Icons.zoom_in_map, size: 16),
+ child: Icon(Icons.tv, size: 16),
),
),
],
diff --git a/app/lib/provider/network/server/controller/send_controller.dart b/app/lib/provider/network/server/controller/send_controller.dart
index efaaadbd..9f2fd014 100644
--- a/app/lib/provider/network/server/controller/send_controller.dart
+++ b/app/lib/provider/network/server/controller/send_controller.dart
@@ -102,12 +102,13 @@ class SendController {
}
if (state.webSendState!.pin != null) {
+ final attempts = state.webSendState!.pinAttempts[request.ip] ?? 0;
+ if (attempts >= 2) {
+ return server.responseJson(429, message: 'Too many attempts.');
+ }
+
final pin = request.url.queryParameters['pin'];
if (pin != state.webSendState!.pin) {
- final attempts = state.webSendState!.pinAttempts[request.ip] ?? 0;
- if (attempts >= 2) {
- return server.responseJson(429, message: 'Too many attempts.');
- }
if (pin?.isNotEmpty ?? false) {
state.webSendState!.pinAttempts[request.ip] = attempts + 1;
}
diff --git a/app/lib/widget/dialogs/qr_dialog.dart b/app/lib/widget/dialogs/qr_dialog.dart
index c4c86f76..2ca46335 100644
--- a/app/lib/widget/dialogs/qr_dialog.dart
+++ b/app/lib/widget/dialogs/qr_dialog.dart
@@ -9,11 +9,15 @@ import 'package:routerino/routerino.dart';
class QrDialog extends StatelessWidget {
final String data;
+ final String? label;
final bool listenIncomingWebSendRequests;
+ final String? pin;
const QrDialog({
required this.data,
+ this.label,
this.listenIncomingWebSendRequests = false,
+ this.pin,
});
@override
@@ -48,7 +52,7 @@ class QrDialog extends StatelessWidget {
),
),
const SizedBox(height: 10),
- Text(data, textAlign: TextAlign.center),
+ Text(label ?? data, textAlign: TextAlign.center),
if (listenIncomingWebSendRequests && webSendState != null)
Builder(
builder: (context) {
@@ -67,6 +71,18 @@ class QrDialog extends StatelessWidget {
}
},
),
+ if (pin != null)
+ Padding(
+ padding: const EdgeInsets.only(top: 10),
+ child: Row(
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ const Icon(Icons.vpn_key),
+ const SizedBox(width: 5),
+ Text(pin!),
+ ],
+ ),
+ ),
],
),
actions: [
diff --git a/app/lib/widget/dialogs/zoom_dialog.dart b/app/lib/widget/dialogs/zoom_dialog.dart
index 12c5ef71..f66b1449 100644
--- a/app/lib/widget/dialogs/zoom_dialog.dart
+++ b/app/lib/widget/dialogs/zoom_dialog.dart
@@ -8,12 +8,14 @@ import 'package:refena_flutter/refena_flutter.dart';
import 'package:routerino/routerino.dart';
class ZoomDialog extends StatelessWidget {
- final String data;
+ final String label;
final bool listenIncomingWebSendRequests;
+ final String? pin;
const ZoomDialog({
- required this.data,
+ required this.label,
this.listenIncomingWebSendRequests = false,
+ this.pin,
});
@override
@@ -38,10 +40,19 @@ class ZoomDialog extends StatelessWidget {
child: FittedBox(
fit: BoxFit.fill,
clipBehavior: Clip.antiAlias,
- child: Text(data, textAlign: TextAlign.center, style: TextStyle(fontSize: fontSize)),
+ child: Text(label, textAlign: TextAlign.center, style: TextStyle(fontSize: fontSize)),
),
),
const SizedBox(height: 10),
+ if (pin != null)
+ Padding(
+ padding: const EdgeInsets.all(8.0),
+ child: FittedBox(
+ fit: BoxFit.fill,
+ clipBehavior: Clip.antiAlias,
+ child: Text('🔑 $pin', textAlign: TextAlign.center, style: const TextStyle(fontSize: 100)),
+ ),
+ ),
if (listenIncomingWebSendRequests && webSendState != null)
Builder(
builder: (context) {