fromUrl method

Future<String> fromUrl(
  1. String url, {
  2. Duration checkInterval = const Duration(seconds: 1),
  3. bool? storeMode,
  4. ProgressListener? onProgress,
  5. Map<String, String>? metadata,
  6. bool? checkURLDuplicates,
  7. bool? saveURLDuplicates,
})

Make upload to /fromUrl endpoint

Implementation

Future<String> fromUrl(
  String url, {
  Duration checkInterval = const Duration(seconds: 1),
  bool? storeMode,
  ProgressListener? onProgress,

  /// **Since v0.7**
  Map<String, String>? metadata,

  /// If set to `true`, enables the [url] duplicates prevention.
  /// Specifically, if the [url] had already been fetched and uploaded previously, this request will return information about the already uploaded file.
  bool? checkURLDuplicates,

  /// Determines if the requested [url] should be kept in the history of fetched/uploaded URLs.
  /// If the value is not defined explicitly, it is set to the value of the [checkURLDuplicates] parameter.
  bool? saveURLDuplicates,
}) async {
  _ensureRightVersionForMetadata(metadata);

  metadata ??= {};

  final request = createMultipartRequest(
    'POST',
    buildUri('$uploadUrl/from_url/'),
    false,
  )..fields.addAll({
      'pub_key': publicKey,
      'store': resolveStoreModeParam(storeMode),
      'source_url': url,
      if (checkURLDuplicates != null)
        'check_URL_duplicates': resolveStoreModeParam(checkURLDuplicates),
      if (saveURLDuplicates != null)
        'save_URL_duplicates': resolveStoreModeParam(saveURLDuplicates),
      if (options.useSignedUploads) ..._signUpload(),
      for (MapEntry entry in metadata.entries)
        'metadata[${entry.key}]': entry.value,
    });

  final result = await resolveStreamedResponse(request.send());

  if (result['uuid'] != null) {
    return result['uuid'] as String;
  }

  final token = result['token'] as String;

  await for (UrlUploadStatusEntity response
      in _urlUploadStatusAsStream(token, checkInterval)) {
    if (response.status == UrlUploadStatusValue.Error) {
      throw ClientException(response.errorMessage);
    }

    if (response.status == UrlUploadStatusValue.Success) {
      return response.fileInfo!.id;
    }

    if (response.progress != null && onProgress != null) {
      onProgress(response.progress!);
    }
  }

  throw ClientException('Unsupported response received');
}