Skip to content

Commit

Permalink
Fix extension server & Other improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
AminBhst committed Nov 12, 2023
1 parent 744a482 commit 9975f08
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 117 deletions.
119 changes: 33 additions & 86 deletions lib/browser_extension/browser_extension_server.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';

import 'package:brisk/db/hive_util.dart';
import 'package:brisk/model/download_item.dart';
import 'package:brisk/model/file_metadata.dart';
import 'package:brisk/model/isolate/isolate_args_pair.dart';
import 'package:brisk/util/download_addition_ui_util.dart';
import 'package:brisk/util/http_util.dart';
import 'package:brisk/util/parse_util.dart';
Expand All @@ -18,7 +15,7 @@ import 'package:window_to_front/window_to_front.dart';

class BrowserExtensionServer {
static bool _isServerRunning = false;
static int _requestRateLimitMillis = 1000;
static bool _cancelClicked = false;

static void setup(BuildContext context) async {
if (_isServerRunning) return;
Expand All @@ -33,16 +30,9 @@ class BrowserExtensionServer {
}
}

static int _latestRequestServed = 0;

static Future<void> handleExtensionRequests(server, context) async {
await for (final request in server) {
await for (HttpRequest request in server) {
await for (final body in request) {
if (_tooManyRequestsReached) {
await handleTooManyRequests(request);
continue;
}
_latestRequestServed = DateTime.now().millisecondsSinceEpoch;
final jsonBody = jsonDecode(String.fromCharCodes(body));
if (_windowToFrontEnabled) {
WindowToFront.activate();
Expand All @@ -52,16 +42,6 @@ class BrowserExtensionServer {
}
}

static Future<void> handleTooManyRequests(HttpRequest request) async {
request.response.statusCode = HttpStatus.tooManyRequests;
await request.response.flush();
await request.response.close();
}

static get _tooManyRequestsReached =>
_latestRequestServed + _requestRateLimitMillis >
DateTime.now().millisecondsSinceEpoch;

static Future<void> _handleDownloadAddition(
jsonBody, context, request) async {
final type = jsonBody["type"];
Expand All @@ -75,87 +55,53 @@ class BrowserExtensionServer {
await request.response.close();
}

static void _handleMultiDownloadRequest(
jsonBody, context, HttpRequest request) {
static void _handleMultiDownloadRequest(jsonBody, context, request) {
List downloadHrefs = jsonBody["data"]["downloadHrefs"];
if (downloadHrefs.isEmpty) return;
final downloadItems = downloadHrefs.map((e) => DownloadItem.fromUrl(e));
_cancelClicked = false;
_showLoadingDialog(context);
_spawnFileInfoRetrieverIsolate(downloadItems.toList()).then((rPort) {
retrieveFilesInfo(rPort).then((fileInfos) {
Navigator.of(context).pop();
showDialog(
context: context,
barrierDismissible: false,
builder: (_) => MultiDownloadAdditionDialog(fileInfos),
);
_cancelRequest(context);
}).onError(
(e, ee) {
_cancelRequest(context);
showDialog(
context: context,
builder: (_) => const ErrorDialog(
text: 'Could not retrieve file information!',
),
);
},
requestFileInfoBatch(downloadItems.toList()).then((fileInfos) {
if (_cancelClicked || fileInfos == null) {
return;
}
Navigator.of(context).pop();
showDialog(
context: context,
barrierDismissible: false,
builder: (_) => MultiDownloadAdditionDialog(fileInfos),
);
});
}).onError((error, stackTrace) => onFileInfoRetrievalError(context));
}

/// TODO add log file
static onFileInfoRetrievalError(context) {
Navigator.of(context).pop();
showDialog(
context: context,
builder: (_) => const ErrorDialog(
textHeight: 0,
title: "Could not retrieve file information!",
),
);
}

static void _showLoadingDialog(context) {
showDialog(
barrierDismissible: false,
context: context,
builder: (_) => FileInfoLoader(onCancelPressed: () {
_cancelRequest(context);
_cancelClicked = true;
Navigator.of(context).pop();
}),
);
}

static Future<List<FileInfo>> retrieveFilesInfo(
ReceivePort receivePort) async {
final Completer<List<FileInfo>> completer = Completer();
receivePort.listen((message) {
if (message is List<FileInfo>) {
completer.complete(message);
} else {
completer.completeError(message);
}
});
return completer.future;
}

static void _cancelRequest(BuildContext context) {
fileInfoExtractorIsolate?.kill(priority: 0);
}

static Isolate? fileInfoExtractorIsolate = null;

static Future<ReceivePort> _spawnFileInfoRetrieverIsolate(
List<DownloadItem> downloadItems) async {
final ReceivePort receivePort = ReceivePort();
fileInfoExtractorIsolate =
await Isolate.spawn<IsolateArgsPair<List<DownloadItem>>>(
requestFileInfoIsolate,
IsolateArgsPair(receivePort.sendPort, downloadItems),
paused: true,
);
fileInfoExtractorIsolate?.addErrorListener(receivePort.sendPort);
fileInfoExtractorIsolate
?.resume(fileInfoExtractorIsolate!.pauseCapability!);
return receivePort;
}

static Future<void> requestFileInfoIsolate(IsolateArgsPair args) async {
final result = await requestFileInfoBatch(args.obj);
args.sendPort.send(result);
}

static void _handleSingleDownloadRequest(jsonBody, context, request) async {
DownloadAdditionUiUtil.handleDownloadAddition(
context, jsonBody['data']['url']);
context,
jsonBody['data']['url'],
);
addCORSHeaders(request);
request.response.statusCode = HttpStatus.ok;
}
Expand All @@ -176,7 +122,8 @@ class BrowserExtensionServer {
context: context,
builder: (context) => ErrorDialog(
width: 750,
height: 100,
height: 130,
textHeight: 70,
title: "Port ${port} is already in use by another process!",
text:
"\nFor optimal browser integration, please change the extension port in [Settings->Extension->Port] then restart the app."
Expand Down
15 changes: 11 additions & 4 deletions lib/util/download_addition_ui_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ class DownloadAdditionUiUtil {
if (!isUrlValid(url)) {
showDialog(
context: context,
builder: (_) => const ErrorDialog(height: 45, title: 'Invalid URL'),
builder: (_) => const ErrorDialog(
width: 30,
height: 45,
title: 'Invalid URL',
),
);
return;
}
Expand All @@ -53,7 +57,8 @@ class DownloadAdditionUiUtil {
showDialog(
context: context,
builder: (_) => const ErrorDialog(
text: 'Could not retrieve file information!',
textHeight: 0,
title: "Could not retrieve file information!",
),
);
},
Expand Down Expand Up @@ -135,7 +140,8 @@ class DownloadAdditionUiUtil {
context.loaderOverlay.hide();
}

static void showAskDuplicationActionDialog(BuildContext context, DownloadItem item, bool additionalPop) {
static void showAskDuplicationActionDialog(
BuildContext context, DownloadItem item, bool additionalPop) {
showDialog(
context: context,
builder: (context) => AskDuplicationAction(
Expand Down Expand Up @@ -169,7 +175,8 @@ class DownloadAdditionUiUtil {
));
}

static void showDownloadInfoDialog(BuildContext context, DownloadItem item, bool additionalPop) {
static void showDownloadInfoDialog(
BuildContext context, DownloadItem item, bool additionalPop) {
if (additionalPop) {
Navigator.of(context).pop();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/util/http_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Future<FileInfo?> requestFileInfo(DownloadItem downloadItem,
final client = http.Client();
var response = client.send(request);
Completer<FileInfo?> completer = Completer();
response.asStream().listen((streamedResponse) {
response.asStream().timeout(Duration(seconds: 10)).listen((streamedResponse) {
final headers = streamedResponse.headers;
var filename = extractFilenameFromHeaders(headers);
if (filename != null) {
Expand Down
20 changes: 20 additions & 0 deletions lib/util/responsive_util.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:flutter/material.dart';


double minimizedSideMenuWidth = 150;

double resolveSideMenuWidth(Size size) {
if (minimizedSideMenu(size)) {
return minimizedSideMenuWidth;
}
return size.width * 0.2;
}

double resolveWindowWidth(Size size) {
if (size.width < 1300) {
return size.width - minimizedSideMenuWidth;
}
return size.width * 0.8;
}

bool minimizedSideMenu(Size size) => size.width < 1300;
14 changes: 12 additions & 2 deletions lib/widget/base/error_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ class ErrorDialog extends StatelessWidget {
final double width;
final double height;
final String? title;
final double textHeight;

const ErrorDialog({
super.key,
this.text = '',
this.width = 300,
this.height = 60,
this.title = null,
this.textHeight = 90,
});

@override
Expand All @@ -24,10 +26,16 @@ class ErrorDialog extends StatelessWidget {
height: height,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.warning_rounded, color: Colors.red),
SizedBox(
height: 25,
child:
const Icon(Icons.warning_rounded, color: Colors.red)),
const SizedBox(width: 10),
if (title != null)
Text(
Expand All @@ -39,7 +47,9 @@ class ErrorDialog extends StatelessWidget {
),
],
),
Text(text, style: const TextStyle(color: Colors.red)),
SizedBox(
height: textHeight,
child: Text(text, style: const TextStyle(color: Colors.red))),
],
),
),
Expand Down
5 changes: 3 additions & 2 deletions lib/widget/download/download_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:brisk/provider/download_request_provider.dart';
import 'package:brisk/provider/pluto_grid_util.dart';
import 'package:brisk/provider/queue_provider.dart';
import 'package:brisk/util/file_util.dart';
import 'package:brisk/util/responsive_util.dart';
import 'package:brisk/widget/download/download_row_pop_up_menu_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
Expand Down Expand Up @@ -37,7 +38,7 @@ class _DownloadGridState extends State<DownloadGrid> {
),
PlutoColumn(
enableRowChecked: true,
width: 300,
width: 360,
title: 'File Name',
field: 'file_name',
type: PlutoColumnType.text(),
Expand Down Expand Up @@ -150,7 +151,7 @@ class _DownloadGridState extends State<DownloadGrid> {
type: MaterialType.transparency,
child: Container(
height: size.height - 70,
width: size.width * 0.8,
width: resolveWindowWidth(size),
decoration: const BoxDecoration(color: Colors.black26),
child: PlutoGrid(
key: UniqueKey(),
Expand Down
2 changes: 1 addition & 1 deletion lib/widget/queue/create_queue_window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class _CreateQueueWindowState extends State<CreateQueueWindow> {
showDialog(
context: context,
builder: (context) => ErrorDialog(
title: "A queue with this name already exists!",
title: "Queue with this name already exists!",
height: 50,
width: 400,
),
Expand Down
3 changes: 2 additions & 1 deletion lib/widget/queue/download_queue_list.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:brisk/db/hive_util.dart';
import 'package:brisk/util/responsive_util.dart';
import 'package:brisk/widget/queue/queue_list_item.dart';
import 'package:flutter/material.dart';

Expand All @@ -12,7 +13,7 @@ class DownloadQueueList extends StatelessWidget {
type: MaterialType.transparency,
child: Container(
height: size.height - 70,
width: size.width * 0.8,
width: resolveWindowWidth(size),
color: Color.fromRGBO(40, 46, 58, 1),
child: Column(
children: buildQueues(context),
Expand Down
18 changes: 2 additions & 16 deletions lib/widget/side_menu/side_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:brisk/db/hive_util.dart';
import 'package:brisk/model/download_queue.dart';
import 'package:brisk/provider/pluto_grid_util.dart';
import 'package:brisk/provider/settings_provider.dart';
import 'package:brisk/util/responsive_util.dart';
import 'package:brisk/widget/setting/settings_window.dart';
import 'package:brisk/widget/side_menu/side_menu_expansion_tile.dart';
import 'package:brisk/widget/side_menu/side_menu_item.dart';
Expand All @@ -22,7 +23,7 @@ class SideMenu extends StatelessWidget {
final queueProvider = Provider.of<QueueProvider>(context);
final size = MediaQuery.of(context).size;
return Container(
width: size.width * 0.2,
width: resolveSideMenuWidth(size),
height: double.infinity,
color: const Color.fromRGBO(55, 64, 81, 1),
child: Material(
Expand Down Expand Up @@ -112,20 +113,6 @@ class SideMenu extends StatelessWidget {
leading: Icon(Icons.queue, color: Colors.white),
title: "Queues",
),
// SideMenuExpansionTile(
// icon: Icon(Icons.queue, color: Colors.white),
// title: 'Queues',
// onTap: null,
// children: HiveBoxes.instance.downloadQueueBox.values
// .map(
// (e) => SideMenuListTileItem(
// text: e.name,
// responsive: false,
// icon: null,
// onTap: () => onQueueItemPressed(queueProvider, e),
// ),
// ).toList(),
// ),
const Spacer(),
Padding(
padding: const EdgeInsets.all(50),
Expand Down Expand Up @@ -184,5 +171,4 @@ class SideMenu extends StatelessWidget {
"status", DownloadStatus.assembleComplete);
}

bool minimizedSideMenu(Size size) => size.width < 1300;
}
Loading

0 comments on commit 9975f08

Please sign in to comment.