directStreamUpload method
Future<CloudflareHTTPResponse<CloudflareStreamVideo?> >
directStreamUpload({
- required DataUploadDraft dataUploadDraft,
- DataTransmit<
File> ? contentFromFile, - DataTransmit<
String> ? contentFromPath, - DataTransmit<
Uint8List> ? contentFromBytes, - String? fileName,
For video direct stream upload without API key or token. This function is to be used specifically after a video createDirectStreamUpload has been requested.
A video up to 200 MegaBytes can be uploaded using a single HTTP POST (multipart/form-data) request. For larger file sizes, please upload using the TUS protocol.
Documentation: https://developers.cloudflare.com/stream/uploading-videos/direct-creator-uploads/
Implementation
Future<CloudflareHTTPResponse<CloudflareStreamVideo?>> directStreamUpload({
/// Information on where to stream upload the video without an API key or token
required DataUploadDraft dataUploadDraft,
/// Video file to upload
DataTransmit<File>? contentFromFile,
/// Path to the video file to upload
DataTransmit<String>? contentFromPath,
/// Image byte array representation to upload
DataTransmit<Uint8List>? contentFromBytes,
/// To specify a filename for the content to be uploaded.
String? fileName,
}) async {
assert(
contentFromFile != null ||
contentFromPath != null ||
contentFromBytes != null,
'One of the content must be specified.');
if (contentFromPath != null) {
contentFromFile ??= DataTransmit<File>(
data: File(contentFromPath.data),
progressCallback: contentFromPath.progressCallback,
cancelToken: contentFromPath.cancelToken,
);
}
/// Web support
if (contentFromFile != null && PlatformUtils.isWeb) {
contentFromBytes ??= DataTransmit<Uint8List>(
data: contentFromFile.data.readAsBytesSync(),
progressCallback: contentFromFile.progressCallback,
cancelToken: contentFromFile.cancelToken,
);
contentFromFile = null;
}
final dio = restAPI.dio;
final formData = FormData();
CancelToken? cancelToken;
ProgressCallback? progressCallback;
if (contentFromFile != null) {
cancelToken = contentFromFile.cancelToken;
final file = contentFromFile.data;
progressCallback = contentFromFile.progressCallback;
formData.files.add(MapEntry(
Params.file,
MultipartFile.fromFileSync(file.path,
filename:
fileName ?? file.path.split(Platform.pathSeparator).last)));
} else {
cancelToken = contentFromBytes!.cancelToken;
final bytes = contentFromBytes.data;
progressCallback = contentFromBytes.progressCallback;
formData.files.add(MapEntry(
Params.file,
MultipartFile.fromBytes(
bytes,
filename: fileName,
)));
}
final rawResponse = await dio.fetch(Options(
method: 'POST',
// headers: _headers,
// responseType: ResponseType.plain,
contentType: 'multipart/form-data',
).compose(BaseOptions(baseUrl: dataUploadDraft.uploadURL), '',
data: formData,
onSendProgress: progressCallback,
cancelToken: cancelToken));
final rawHttpResponse = HttpResponse(rawResponse.data, rawResponse);
final rawCloudflareResponse = await getSaveResponse(
Future.value(rawHttpResponse),
parseCloudflareResponse: false);
return CloudflareHTTPResponse<CloudflareStreamVideo?>(
rawCloudflareResponse.base,
CloudflareStreamVideo(
id: dataUploadDraft.id,
readyToStream: true,
watermark: dataUploadDraft.watermark),
error: rawCloudflareResponse.error,
extraData: rawCloudflareResponse.extraData);
}