platform/lib/src/virtual_directory.dart

112 lines
3 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
2016-11-23 20:14:05 +00:00
import 'package:angel_route/angel_route.dart';
2017-01-25 22:40:41 +00:00
typedef StaticFileCallback(File file, RequestContext req, ResponseContext res);
final RegExp _param = new RegExp(r':([A-Za-z0-9_]+)(\((.+)\))?');
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
String _pathify(String path) {
var p = path.replaceAll(_straySlashes, '');
Map<String, String> replace = {};
for (Match match in _param.allMatches(p)) {
if (match[3] != null) replace[match[0]] = ':${match[1]}';
}
replace.forEach((k, v) {
p = p.replaceAll(k, v);
});
return p;
}
2016-11-23 20:14:05 +00:00
class VirtualDirectory {
final bool debug;
String _prefix;
Directory _source;
Directory get source => _source;
2017-01-25 22:40:41 +00:00
final StaticFileCallback callback;
final List<String> indexFileNames;
final String publicPath;
VirtualDirectory(
{Directory source,
2016-11-23 20:14:05 +00:00
this.debug: false,
this.indexFileNames: const ['index.html'],
2017-01-25 22:40:41 +00:00
this.publicPath: '/',
this.callback}) {
2016-11-23 20:14:05 +00:00
_prefix = publicPath.replaceAll(_straySlashes, '');
if (source != null) {
_source = source;
} else {
String dirPath = Platform.environment['ANGEL_ENV'] == 'production'
? './build/web'
: './web';
_source = new Directory(dirPath);
}
}
_printDebug(msg) {
if (debug) print(msg);
}
2016-11-23 20:14:05 +00:00
call(AngelBase app) async => serve(app);
2017-01-25 22:40:41 +00:00
Future<bool> sendFile(
File file, RequestContext req, ResponseContext res) async {
_printDebug('Sending file ${file.absolute.path}...');
2016-12-21 17:51:43 +00:00
res.statusCode = 200;
2017-01-25 22:40:41 +00:00
if (callback != null) {
var r = callback(file, req, res);
r = r is Future ? await r : r;
if (r != null && r != true) return r;
}
await res.streamFile(file);
return false;
}
2016-11-23 20:14:05 +00:00
void serve(Router router) {
_printDebug('Source directory: ${source.absolute.path}');
_printDebug('Public path prefix: "$_prefix"');
2017-01-25 22:40:41 +00:00
router.get('$publicPath/*',
(RequestContext req, ResponseContext res) async {
2016-11-23 20:14:05 +00:00
var path = req.path.replaceAll(_straySlashes, '');
2017-01-25 22:40:41 +00:00
return serveFile(path, req, res);
});
2016-11-23 20:14:05 +00:00
}
2017-01-25 22:40:41 +00:00
serveFile(String path, RequestContext req, ResponseContext res) async {
2016-11-23 20:14:05 +00:00
if (_prefix.isNotEmpty) {
path = path.replaceAll(new RegExp('^' + _pathify(_prefix)), '');
}
final file = new File.fromUri(source.absolute.uri.resolve(path));
_printDebug('Attempting to statically serve file: ${file.absolute.path}');
if (await file.exists()) {
2017-01-25 22:40:41 +00:00
return sendFile(file, req, res);
2016-11-23 20:14:05 +00:00
} else {
// Try to resolve index
if (path.isEmpty) {
for (String indexFileName in indexFileNames) {
final index =
new File.fromUri(source.absolute.uri.resolve(indexFileName));
if (await index.exists()) {
2017-01-25 22:40:41 +00:00
return await sendFile(index, req, res);
2016-11-23 20:14:05 +00:00
}
}
} else {
_printDebug('File "$path" does not exist, and is not an index.');
return true;
}
}
}
}