Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
￴￴ committed Jun 24, 2024
1 parent be778f3 commit fca582c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: '0.0.0'
environment:
sdk: '>=3.3.3 <4.0.0'
dependencies:
server_universe: '^0.0.14'
server_universe: '^0.0.17'
general_lib: '^0.0.42'
dependency_overrides:
pointycastle: '3.8.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,18 @@ import '../extensions/request_helpers.dart';
import '../extensions/response_helpers.dart';
import 'type_handler.dart';

TypeHandler get directoryTypeHandler =>
TypeHandler<Directory>((req, res, Directory directory) async {
TypeHandler get directoryTypeHandler => TypeHandler<Directory>((req, res, Directory directory) async {
directory = directory.absolute;
final usedRoute = req.route;
String? virtualPath;
if (usedRoute.contains('*')) {
virtualPath = req.uri.path
.substring(min(req.uri.path.length, usedRoute.indexOf('*')));
virtualPath = req.uri.path.substring(min(req.uri.path.length, usedRoute.indexOf('*')));
}

if (req.method == 'GET' || req.method == 'HEAD') {
assert(usedRoute.contains('*'),
'TypeHandler of type Directory GET request needs a route declaration that contains a wildcard (*). Found: $usedRoute');
assert(usedRoute.contains('*'), 'TypeHandler of type Directory GET request needs a route declaration that contains a wildcard (*). Found: $usedRoute');

final filePath =
'${directory.path}/${Uri.decodeComponent(virtualPath!)}';
final filePath = '${directory.path}/${Uri.decodeComponent(virtualPath!)}';

req.preventTraversal(filePath, directory);

Expand All @@ -76,8 +72,7 @@ TypeHandler get directoryTypeHandler =>
req.log(() => 'Respond with file: ${match.path}');
await _respondWithFile(res, match, headerOnly: req.method == 'HEAD');
} on StateError {
req.log(() =>
'Could not match with any file. Expected file at: $filePath');
req.log(() => 'Could not match with any file. Expected file at: $filePath');
}
}
if (req.method == 'POST' || req.method == 'PUT') {
Expand All @@ -98,18 +93,15 @@ TypeHandler get directoryTypeHandler =>

req.preventTraversal(fileToWrite.path, directory);

await fileToWrite.writeAsBytes(
(body['file'] as HttpBodyFileUpload).content as List<int>);
final publicPath =
"${req.requestedUri.toString() + (virtualPath != null ? '/$virtualPath' : '')}/$fileName";
await fileToWrite.writeAsBytes((body['file'] as HttpBodyFileUpload).content as List<int>);
final publicPath = "${req.requestedUri.toString() + (virtualPath != null ? '/$virtualPath' : '')}/$fileName";
req.log(() => 'Uploaded file $publicPath');

await res.json({'path': publicPath});
}
}
if (req.method == 'DELETE') {
final fileToDelete =
File('${directory.path}/${Uri.decodeComponent(virtualPath!)}');
final fileToDelete = File('${directory.path}/${Uri.decodeComponent(virtualPath!)}');

req.preventTraversal(fileToDelete.path, directory);

Expand All @@ -123,8 +115,7 @@ TypeHandler get directoryTypeHandler =>
}
});

Future _respondWithFile(HttpResponse res, File file,
{bool headerOnly = false}) async {
Future _respondWithFile(HttpResponse res, File file, {bool headerOnly = false}) async {
res.setContentTypeFromFile(file);

// This is necessary to deal with 'HEAD' requests
Expand All @@ -135,8 +126,7 @@ Future _respondWithFile(HttpResponse res, File file,
}

extension _Logger on HttpRequest {
void log(String Function() msgFn) => server_universe.logWriter(
() => 'DirectoryTypeHandler: ${msgFn()}', LogType.debug);
void log(String Function() msgFn) => server_universe.logWriter(() => 'DirectoryTypeHandler: ${msgFn()}', LogType.debug);

void preventTraversal(String filePath, Directory absDir) {
final check = File(filePath).absolute;
Expand Down
2 changes: 1 addition & 1 deletion package/server_universe_dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ topics:
environment:
sdk: '>=3.3.3 <4.0.0'
dependencies:
server_universe: '^0.0.14'
server_universe: '^0.0.17'
dependency_overrides:
pointycastle: '3.8.0'
dev_dependencies:
Expand Down
2 changes: 1 addition & 1 deletion package/server_universe_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ environment:
dependencies:
flutter:
sdk: 'flutter'
server_universe: '^0.0.14'
server_universe: '^0.0.17'
dev_dependencies:
flutter_test:
sdk: 'flutter'
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies:
yaml: '^3.1.2'
yaml_edit: '^2.2.0'
yaml_writer: '^2.0.0'
server_universe: '^0.0.14'
server_universe: '^0.0.17'
dev_dependencies:
lints: '^2.1.0'
test: '^1.24.0'
Expand Down

0 comments on commit fca582c

Please sign in to comment.