createTusDirectStreamUpload method

Future<CloudflareHTTPResponse<DataUploadDraft?>> createTusDirectStreamUpload({
  1. required int size,
  2. String? name,
  3. int? maxDurationSeconds,
  4. String? creator,
  5. num? thumbnailTimestampPct,
  6. List<String>? allowedOrigins,
  7. bool? requireSignedURLs,
  8. Watermark? watermark,
  9. DateTime? expiry,
})

Direct upload using tus(https://tus.io) protocol. Direct uploads allow users to upload videos without API keys. A common place to use direct uploads is on web apps, client side applications, or on mobile devices where users upload content directly to Stream.

IMPORTANT: when using tus protocol for direct stream upload it's not required to set a maxDurationSeconds because Cloudflare will reserve a loose amount o minutes for the video to be uploaded, for instance 240 minutes will be reserved from your available storage. Nevertheless it's recommended to set the maxDurationSeconds to avoid running out of available minutes when multiple simultaneously tus uploads are taking place.

Documentation: https://developers.cloudflare.com/stream/uploading-videos/direct-creator-uploads/#using-tus-recommended-for-videos-over-200mb

Implementation

Future<CloudflareHTTPResponse<DataUploadDraft?>> createTusDirectStreamUpload({
  /// The size of the file to upload in bytes
  required int size,

  /// The name of the video in the dashboard.
  String? name,

  /// Direct uploads occupy minutes of videos on your Stream account until
  /// they are expired. This value will be used to calculate the duration the
  /// video will occupy before the video is uploaded. After upload, the
  /// duration of the uploaded will be used instead. If a video longer than
  /// this value is uploaded, the video will result in an error.
  ///
  /// Min value: 1 second
  /// Max value: 21600 seconds which is 360 mins, 6 hours
  /// e.g: 300 seconds which is 5 mins
  int? maxDurationSeconds,

  /// User-defined identifier of the media creator
  ///
  /// Max length: 64
  /// e.g: "creator-id_abcde12345"
  String? creator,

  /// Timestamp location of thumbnail image calculated as a percentage value
  /// of the video's duration. To convert from a second-wise timestamp to a
  /// percentage, divide the desired timestamp by the total duration of the
  /// video. If this value is not set, the default thumbnail image will be
  /// from 0s of the video.
  ///
  /// Default value: 0
  /// Min value:0
  /// Max value:1
  /// e.g: 0.529241
  num? thumbnailTimestampPct,

  /// List which origins should be allowed to display the video. Enter
  /// allowed origin domains in an array and use * for wildcard subdomains.
  /// Empty array will allow the video to be viewed on any origin.
  ///
  /// e.g:
  /// [
  ///   "example.com"
  /// ]
  List<String>? allowedOrigins,

  /// Indicates whether the video can be a accessed only using it's UID. If
  /// set to true, a signed token needs to be generated with a signing key to
  /// view the video.
  ///
  /// Default value: false
  /// e.g: true
  bool? requireSignedURLs,

  /// A Watermark object with the id of an existing watermark profile
  /// e.g: Watermark(id: "ea95132c15732412d22c1476fa83f27a")
  Watermark? watermark,

  /// The date after upload will not be accepted.
  ///
  /// Min value: Now + 2 minutes.
  /// Max value: Now + 6 hours.
  /// Default value: Now + 30 minutes.
  /// e.g: "2021-01-02T02:20:00Z"
  DateTime? expiry,
}) async {
  assert(!isBasic, RestAPIService.authorizedRequestAssertMessage);
  final metadataMap = {
    Params.name: name,
    Params.maxDurationSeconds: maxDurationSeconds,
    Params.creator: creator,
    Params.thumbnailTimestampPct: thumbnailTimestampPct,
    Params.allowedOrigins: allowedOrigins,
    Params.requireSignedURLs: requireSignedURLs,
    Params.watermark: watermark?.id,
    Params.expiry: expiry?.toJson(),
  };
  final metadata = TusAPI.generateMetadata(metadataMap);
  final rawResponse = await getSaveResponse(
      service.createTusDirectUpload(
        size: size,
        metadata: metadata,
      ),
      parseCloudflareResponse: false);
  final id = rawResponse.headers[Params.streamMediaIdKC];
  final uploadURL = rawResponse.headers[Params.location];
  return CloudflareHTTPResponse<DataUploadDraft>(
    rawResponse.base,
    DataUploadDraft(
      id: id,
      uploadURL: uploadURL,
    ),
    error: rawResponse.error,
    extraData: rawResponse.extraData,
  );
}