1.1.1
This commit is contained in:
parent
0ab637eb15
commit
7d424436c7
3 changed files with 26 additions and 15 deletions
|
@ -1,6 +1,6 @@
|
||||||
# angel_static
|
# angel_static
|
||||||
|
|
||||||
![version 1.1.0-dev+2](https://img.shields.io/badge/version-1.1.0--dev+2-red.svg)
|
![version 1.1.1](https://img.shields.io/badge/version-1.1.1-red.svg)
|
||||||
![build status](https://travis-ci.org/angel-dart/static.svg?branch=master)
|
![build status](https://travis-ci.org/angel-dart/static.svg?branch=master)
|
||||||
|
|
||||||
Static server middleware for Angel.
|
Static server middleware for Angel.
|
||||||
|
@ -39,3 +39,5 @@ The `VirtualDirectory` API accepts a few named parameters:
|
||||||
angel_static is serving your files. If you are not serving static files at the site root,
|
angel_static is serving your files. If you are not serving static files at the site root,
|
||||||
please include this.
|
please include this.
|
||||||
- **debug**: Print verbose debug output.
|
- **debug**: Print verbose debug output.
|
||||||
|
- **callback**: Runs before sending a file to a client. Use this to set headers, etc. If it returns anything other than `null` or `true`,
|
||||||
|
then the callback's result will be sent to the user, instead of the file contents.
|
||||||
|
|
|
@ -3,6 +3,8 @@ import 'dart:io';
|
||||||
import 'package:angel_framework/angel_framework.dart';
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
import 'package:angel_route/angel_route.dart';
|
import 'package:angel_route/angel_route.dart';
|
||||||
|
|
||||||
|
typedef StaticFileCallback(File file, RequestContext req, ResponseContext res);
|
||||||
|
|
||||||
final RegExp _param = new RegExp(r':([A-Za-z0-9_]+)(\((.+)\))?');
|
final RegExp _param = new RegExp(r':([A-Za-z0-9_]+)(\((.+)\))?');
|
||||||
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
|
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
|
||||||
|
|
||||||
|
@ -27,6 +29,7 @@ class VirtualDirectory {
|
||||||
String _prefix;
|
String _prefix;
|
||||||
Directory _source;
|
Directory _source;
|
||||||
Directory get source => _source;
|
Directory get source => _source;
|
||||||
|
final StaticFileCallback callback;
|
||||||
final List<String> indexFileNames;
|
final List<String> indexFileNames;
|
||||||
final String publicPath;
|
final String publicPath;
|
||||||
|
|
||||||
|
@ -34,7 +37,8 @@ class VirtualDirectory {
|
||||||
{Directory source,
|
{Directory source,
|
||||||
this.debug: false,
|
this.debug: false,
|
||||||
this.indexFileNames: const ['index.html'],
|
this.indexFileNames: const ['index.html'],
|
||||||
this.publicPath: '/'}) {
|
this.publicPath: '/',
|
||||||
|
this.callback}) {
|
||||||
_prefix = publicPath.replaceAll(_straySlashes, '');
|
_prefix = publicPath.replaceAll(_straySlashes, '');
|
||||||
|
|
||||||
if (source != null) {
|
if (source != null) {
|
||||||
|
@ -53,9 +57,17 @@ class VirtualDirectory {
|
||||||
|
|
||||||
call(AngelBase app) async => serve(app);
|
call(AngelBase app) async => serve(app);
|
||||||
|
|
||||||
Future<bool> sendFile(File file, ResponseContext res) async {
|
Future<bool> sendFile(
|
||||||
_printDebug('Streaming file ${file.absolute.path}...');
|
File file, RequestContext req, ResponseContext res) async {
|
||||||
|
_printDebug('Sending file ${file.absolute.path}...');
|
||||||
res.statusCode = 200;
|
res.statusCode = 200;
|
||||||
|
|
||||||
|
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);
|
await res.streamFile(file);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -63,17 +75,14 @@ class VirtualDirectory {
|
||||||
void serve(Router router) {
|
void serve(Router router) {
|
||||||
_printDebug('Source directory: ${source.absolute.path}');
|
_printDebug('Source directory: ${source.absolute.path}');
|
||||||
_printDebug('Public path prefix: "$_prefix"');
|
_printDebug('Public path prefix: "$_prefix"');
|
||||||
|
router.get('$publicPath/*',
|
||||||
handler(RequestContext req, ResponseContext res) async {
|
(RequestContext req, ResponseContext res) async {
|
||||||
var path = req.path.replaceAll(_straySlashes, '');
|
var path = req.path.replaceAll(_straySlashes, '');
|
||||||
|
return serveFile(path, req, res);
|
||||||
return serveFile(path, res);
|
});
|
||||||
}
|
|
||||||
|
|
||||||
router.get('$publicPath/*', handler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
serveFile(String path, ResponseContext res) async {
|
serveFile(String path, RequestContext req, ResponseContext res) async {
|
||||||
if (_prefix.isNotEmpty) {
|
if (_prefix.isNotEmpty) {
|
||||||
path = path.replaceAll(new RegExp('^' + _pathify(_prefix)), '');
|
path = path.replaceAll(new RegExp('^' + _pathify(_prefix)), '');
|
||||||
}
|
}
|
||||||
|
@ -82,7 +91,7 @@ class VirtualDirectory {
|
||||||
_printDebug('Attempting to statically serve file: ${file.absolute.path}');
|
_printDebug('Attempting to statically serve file: ${file.absolute.path}');
|
||||||
|
|
||||||
if (await file.exists()) {
|
if (await file.exists()) {
|
||||||
return sendFile(file, res);
|
return sendFile(file, req, res);
|
||||||
} else {
|
} else {
|
||||||
// Try to resolve index
|
// Try to resolve index
|
||||||
if (path.isEmpty) {
|
if (path.isEmpty) {
|
||||||
|
@ -90,7 +99,7 @@ class VirtualDirectory {
|
||||||
final index =
|
final index =
|
||||||
new File.fromUri(source.absolute.uri.resolve(indexFileName));
|
new File.fromUri(source.absolute.uri.resolve(indexFileName));
|
||||||
if (await index.exists()) {
|
if (await index.exists()) {
|
||||||
return await sendFile(index, res);
|
return await sendFile(index, req, res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -4,7 +4,7 @@ environment:
|
||||||
sdk: ">=1.19.0"
|
sdk: ">=1.19.0"
|
||||||
homepage: https://github.com/angel-dart/angel_static
|
homepage: https://github.com/angel-dart/angel_static
|
||||||
author: thosakwe <thosakwe@gmail.com>
|
author: thosakwe <thosakwe@gmail.com>
|
||||||
version: 1.1.0-dev+2
|
version: 1.1.1
|
||||||
dependencies:
|
dependencies:
|
||||||
angel_framework: ^1.0.0-dev
|
angel_framework: ^1.0.0-dev
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
|
|
Loading…
Reference in a new issue