Skip to content

Commit

Permalink
feat: auto recognize magnet uri (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Oct 8, 2023
1 parent fe55f1b commit c0338c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CreateController extends GetxController
final isResolving = false.obs;
final showAdvanced = false.obs;
late TabController advancedTabController;
final oldUrl = "".obs;
final fileDataUri = "".obs;

@override
Expand Down
21 changes: 21 additions & 0 deletions ui/flutter/lib/app/modules/create/views/create_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ class CreateView extends GetView<CreateController> {
_urlController.text = value!.text!;
_urlController.selection = TextSelection.fromPosition(
TextPosition(offset: _urlController.text.length));
return;
}

recognizeMagnetUri(value!.text!);
}
});
}
Expand Down Expand Up @@ -117,6 +120,10 @@ class CreateView extends GetView<CreateController> {
},
onChanged: (v) async {
controller.clearFileDataUri();
if (controller.oldUrl.value.isEmpty) {
recognizeMagnetUri(v);
}
controller.oldUrl.value = v;
},
),
),
Expand Down Expand Up @@ -237,6 +244,20 @@ class CreateView extends GetView<CreateController> {
);
}

// recognize magnet uri, if length == 40, auto add magnet prefix
recognizeMagnetUri(String text) {
if (text.length != 40) {
return;
}
final exp = RegExp(r"[0-9a-fA-F]+");
if (exp.hasMatch(text)) {
final uri = "magnet:?xt=urn:btih:$text";
_urlController.text = uri;
_urlController.selection = TextSelection.fromPosition(
TextPosition(offset: _urlController.text.length));
}
}

Future<void> _doResolve() async {
if (controller.isResolving.value) {
return;
Expand Down

0 comments on commit c0338c3

Please sign in to comment.