upload method

  1. @override
Future<ParseResponse> upload({
  1. ProgressCallback? progressCallback,
})
override

Uploads a file to Parse Server

Implementation

@override
Future<ParseResponse> upload({ProgressCallback? progressCallback}) async {
  if (saved) {
    //Creates a Fake Response to return the correct result
    final Map<String, String> response = <String, String>{
      'url': url!,
      'name': name
    };
    return handleResponse<ParseXFile>(
        this,
        ParseNetworkResponse(data: json.encode(response), statusCode: 201),
        ParseApiRQ.upload,
        _debug,
        parseClassName);
  }

  progressCallback ??= _progressCallback;

  _cancelToken = CancelToken();
  Map<String, String> headers;
  if (parseIsWeb) {
    headers = <String, String>{
      HttpHeaders.contentTypeHeader: file?.mimeType ??
          lookupMimeType(url ?? file?.name ?? name,
              headerBytes: await file?.readAsBytes()) ??
          'application/octet-stream',
    };
  } else {
    headers = <String, String>{
      HttpHeaders.contentTypeHeader: file?.mimeType ??
          lookupMimeType(file!.path) ??
          'application/octet-stream',
      HttpHeaders.contentLengthHeader: '${await file!.length()}',
    };
  }

  try {
    final String uri = ParseCoreData().serverUrl + _path;

    Stream<List<int>>? data;
    if (parseIsWeb) {
      data = Stream<List<int>>.fromIterable(
          <List<int>>[await file!.readAsBytes()]);
    } else {
      data = file!.openRead();
    }

    final ParseNetworkResponse response = await _client.postBytes(
      uri,
      options: ParseNetworkOptions(headers: headers),
      data: data,
      onSendProgress: progressCallback,
      cancelToken: _cancelToken,
    );
    if (response.statusCode == 201) {
      final Map<String, dynamic> map = json.decode(response.data);
      url = map['url'].toString();
      name = map['name'].toString();
    }
    return handleResponse<ParseXFile>(
        this, response, ParseApiRQ.upload, _debug, parseClassName);
  } on Exception catch (e) {
    return handleException(e, ParseApiRQ.upload, _debug, parseClassName);
  }
}