Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed May 17, 2017
2 parents af161a3 + dd42ca9 commit b6e30c8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.5

* Add an optional `contentTypeResolver` argument to `createStaticHandler`.

## 0.2.4

* Add support for "sniffing" the content of the file for the content-type via an optional
Expand Down
14 changes: 10 additions & 4 deletions lib/src/static_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ import 'util.dart';
///
/// If [useHeaderBytesForContentType] is `true`, the contents of the
/// file will be used along with the file path to determine the content type.
///
/// Specify a custom [contentTypeResolver] to customize automatic content type
/// detection.
Handler createStaticHandler(String fileSystemPath,
{bool serveFilesOutsidePath: false,
String defaultDocument,
bool listDirectories: false,
bool useHeaderBytesForContentType: false}) {
bool useHeaderBytesForContentType: false,
mime.MimeTypeResolver contentTypeResolver}) {
var rootDir = new Directory(fileSystemPath);
if (!rootDir.existsSync()) {
throw new ArgumentError('A directory corresponding to fileSystemPath '
Expand All @@ -51,6 +55,8 @@ Handler createStaticHandler(String fileSystemPath,
}
}

contentTypeResolver ??= new mime.MimeTypeResolver();

return (Request request) async {
var segs = [fileSystemPath]..addAll(request.url.pathSegments);

Expand Down Expand Up @@ -110,15 +116,15 @@ Handler createStaticHandler(String fileSystemPath,
String contentType;
if (useHeaderBytesForContentType) {
var length =
math.min(mime.defaultMagicNumbersMaxLength, file.lengthSync());
math.min(contentTypeResolver.magicNumbersMaxLength, file.lengthSync());

var byteSink = new ByteAccumulatorSink();

await file.openRead(0, length).listen(byteSink.add).asFuture();

contentType = mime.lookupMimeType(file.path, headerBytes: byteSink.bytes);
contentType = contentTypeResolver.lookup(file.path, headerBytes: byteSink.bytes);
} else {
contentType = mime.lookupMimeType(file.path);
contentType = contentTypeResolver.lookup(file.path);
}

if (contentType != null) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: shelf_static
version: 0.2.4
version: 0.2.5
author: Dart Team <misc@dartlang.org>
description: Static file server support for Shelf
homepage: https://github.com/dart-lang/shelf_static
Expand Down
26 changes: 23 additions & 3 deletions test/basic_file_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:io';
import 'dart:convert';
import 'package:http_parser/http_parser.dart';
import 'package:mime/mime.dart' as mime;
import 'package:path/path.dart' as p;
import 'package:scheduled_test/descriptor.dart' as d;
import 'package:scheduled_test/scheduled_test.dart';
Expand Down Expand Up @@ -34,10 +35,13 @@ void main() {
r"OJE7pB/VXmF3CdseucmjxaAruR41Pl9p/Gbyoq5B9FeL2OR7zJ+3aC/X8QdQCyIArPs"
r"HkQAAAABJRU5ErkJggg==";

var webpBytesContent = r"UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAQAcJaQAA3AA/v3AgAA=";

d.dir('files', [
d.file('test.txt', 'test txt content'),
d.file('with space.txt', 'with space content'),
d.binaryFile('header_bytes_test_image', BASE64.decode(pngBytesContent))
d.binaryFile('header_bytes_test_image', BASE64.decode(pngBytesContent)),
d.binaryFile('header_bytes_test_webp', BASE64.decode(webpBytesContent))
]).create();

currentSchedule.onComplete.schedule(() {
Expand Down Expand Up @@ -207,9 +211,9 @@ void main() {
});
});

test('magic_bytes_test_image should be image/png', () {
test('header_bytes_test_image should be image/png', () {
schedule(() {
var handler = createStaticHandler(d.defaultRoot,
final dynamic handler = createStaticHandler(d.defaultRoot,
useHeaderBytesForContentType: true);

return makeRequest(handler, '/files/header_bytes_test_image')
Expand All @@ -218,5 +222,21 @@ void main() {
});
});
});

test('header_bytes_test_webp should be image/webp', () {
schedule(() {
final mime.MimeTypeResolver resolver = new mime.MimeTypeResolver();
resolver.addMagicNumber(<int>[0x52,0x49,0x46,0x46,0x00,0x00,0x00,0x00,0x57,0x45,0x42,0x50], "image/webp",
mask: <int>[0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF]);
final dynamic handler = createStaticHandler(d.defaultRoot,
useHeaderBytesForContentType: true, contentTypeResolver: resolver);

return makeRequest(handler, '/files/header_bytes_test_webp')
.then((response) {
expect(response.mimeType, "image/webp");
});
});
});

});
}

0 comments on commit b6e30c8

Please sign in to comment.