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