Skip to content
41 changes: 23 additions & 18 deletions packages/dart/lib/src/objects/parse_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ class ParseFile extends ParseFileBase {
/// Creates a new file
///
/// {https://docs.parseplatform.org/rest/guide/#files/}
ParseFile(this.file,
{String? name,
super.url,
super.debug,
super.client,
super.autoSendSessionId})
: super(
name: name ?? path.basename(file?.path ?? ''),
);
ParseFile(
this.file, {
String? name,
super.url,
super.debug,
super.client,
super.autoSendSessionId,
}) : super(name: name ?? path.basename(file?.path ?? ''));

File? file;
CancelToken? _cancelToken;
Expand Down Expand Up @@ -62,26 +61,27 @@ class ParseFile extends ParseFileBase {
//Creates a Fake Response to return the correct result
final Map<String, String> response = <String, String>{
'url': url!,
'name': name
'name': name,
};
return handleResponse<ParseFile>(
this,
ParseNetworkResponse(data: json.encode(response), statusCode: 201),
ParseApiRQ.upload,
_debug,
parseClassName);
this,
ParseNetworkResponse(data: json.encode(response), statusCode: 201),
ParseApiRQ.upload,
_debug,
parseClassName,
);
}

progressCallback ??= _progressCallback;

_cancelToken = CancelToken();

final Map<String, String> headers = <String, String>{
HttpHeaders.contentTypeHeader:
lookupMimeType(file!.path) ?? 'application/octet-stream',
HttpHeaders.contentLengthHeader: '${file!.lengthSync()}',
};

// Do not set content-type - let the server infer it from the filename

try {
final String uri = ParseCoreData().serverUrl + _path;
final ParseNetworkResponse response = await _client.postBytes(
Expand All @@ -98,7 +98,12 @@ class ParseFile extends ParseFileBase {
}

return handleResponse<ParseFile>(
this, response, ParseApiRQ.upload, _debug, parseClassName);
this,
response,
ParseApiRQ.upload,
_debug,
parseClassName,
);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.upload, _debug, parseClassName);
}
Expand Down
67 changes: 34 additions & 33 deletions packages/dart/lib/src/objects/parse_x_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ class ParseXFile extends ParseFileBase {
/// Creates a new file base XFile
///
/// {https://docs.parseplatform.org/rest/guide/#files/}
ParseXFile(this.file,
{String? name,
super.url,
super.debug,
super.client,
super.autoSendSessionId})
: super(
name: name ?? path.basename(file?.path ?? ''),
);
ParseXFile(
this.file, {
String? name,
super.url,
super.debug,
super.client,
super.autoSendSessionId,
}) : super(name: name ?? path.basename(file?.path ?? ''));

XFile? file;
CancelToken? _cancelToken;
Expand Down Expand Up @@ -79,43 +78,40 @@ class ParseXFile extends ParseFileBase {
//Creates a Fake Response to return the correct result
final Map<String, String> response = <String, String>{
'url': url!,
'name': name
'name': name,
};
return handleResponse<ParseXFile>(
this,
ParseNetworkResponse(data: json.encode(response), statusCode: 201),
ParseApiRQ.upload,
_debug,
parseClassName);
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()}',
};
Map<String, String> headers = <String, String>{};

// Only set content-type if explicitly provided by the XFile
// Otherwise, let the server infer the MIME type from the filename
if (file?.mimeType != null) {
headers[HttpHeaders.contentTypeHeader] = file!.mimeType!;
}

if (!parseIsWeb) {
headers[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()]);
data = Stream<List<int>>.fromIterable(<List<int>>[
await file!.readAsBytes(),
]);
} else {
data = file!.openRead();
}
Expand All @@ -133,7 +129,12 @@ class ParseXFile extends ParseFileBase {
name = map['name'].toString();
}
return handleResponse<ParseXFile>(
this, response, ParseApiRQ.upload, _debug, parseClassName);
this,
response,
ParseApiRQ.upload,
_debug,
parseClassName,
);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.upload, _debug, parseClassName);
}
Expand Down
Loading
Loading