platform/packages/range_header/example/main.dart

29 lines
733 B
Dart
Raw Normal View History

2021-05-01 02:48:36 +00:00
import 'dart:io';
2021-05-15 11:58:09 +00:00
import 'package:angel3_range_header/angel3_range_header.dart';
2021-05-01 02:48:36 +00:00
2021-05-15 11:58:09 +00:00
var file = File('some_video.mp4');
2021-05-01 02:48:36 +00:00
2021-05-18 11:58:51 +00:00
void handleRequest(HttpRequest request) async {
2021-05-01 02:48:36 +00:00
// Parse the header
var header =
2021-05-15 11:58:09 +00:00
RangeHeader.parse(request.headers.value(HttpHeaders.rangeHeader)!);
2021-05-01 02:48:36 +00:00
// Optimize/canonicalize it
var items = RangeHeader.foldItems(header.items);
2021-05-15 11:58:09 +00:00
header = RangeHeader(items);
2021-05-01 02:48:36 +00:00
// Get info
header.items;
header.rangeUnit;
header.items.forEach((item) => item.toContentRange(400));
// Serve the file
var transformer =
2021-05-15 11:58:09 +00:00
RangeHeaderTransformer(header, 'video/mp4', await file.length());
2021-05-01 02:48:36 +00:00
await file
.openRead()
.cast<List<int>>()
.transform(transformer)
.pipe(request.response);
}