auto method

Future<String> auto(
  1. Object resource, {
  2. bool runInIsolate = false,
  3. bool? storeMode,
  4. ProgressListener? onProgress,
  5. CancelToken? cancelToken,
  6. Map<String, String>? metadata,
})

Upload file resource according to type if String makes fromUrl upload if it is http/https url or try retrieve UCFile if path is absolute, otherwise make an UCFile request according to size

Implementation

Future<String> auto(
  Object resource, {
  bool runInIsolate = false,
  bool? storeMode,
  ProgressListener? onProgress,
  CancelToken? cancelToken,

  /// **Since v0.7**
  Map<String, String>? metadata,
}) async {
  assert(resource is String || resource is UCFile,
      'The resource should be one of `UCFile` or `URL` and `File` path');

  if (runInIsolate) {
    return _runInIsolate(
      resource,
      storeMode: storeMode,
      onProgress: onProgress,
      cancelToken: cancelToken,
      metadata: metadata,
    );
  }

  if (resource is String && resource.isNotEmpty) {
    Uri? uri = Uri.tryParse(resource);

    if (uri != null) {
      if (['http', 'https'].contains(uri.scheme)) {
        return fromUrl(
          resource,
          storeMode: storeMode,
          onProgress: onProgress,
          metadata: metadata,
        );
      } else if (uri.hasAbsolutePath) {
        resource = UCFile.fromUri(uri);
      } else {
        throw ArgumentError('Cannot parse URL from string');
      }
    }
  }

  if (resource is UCFile) {
    final file = resource;
    final filesize = await file.length();

    if (filesize > _kRecomendedMaxFilesizeForBaseUpload) {
      return multipart(
        file,
        storeMode: storeMode,
        onProgress: onProgress,
        cancelToken: cancelToken,
        metadata: metadata,
      );
    }

    return base(
      file,
      storeMode: storeMode,
      onProgress: onProgress,
      cancelToken: cancelToken,
      metadata: metadata,
    );
  }

  throw ArgumentError('Make sure you passed File or URL string');
}