Skip to content
46 changes: 28 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,32 @@ 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()}',
};

// Try to detect content-type using lookupMimeType
// If it cannot be determined, let the server infer from the filename
final String? contentType = lookupMimeType(file!.path);
if (contentType != null) {
headers[HttpHeaders.contentTypeHeader] = contentType;
}

try {
final String uri = ParseCoreData().serverUrl + _path;
final ParseNetworkResponse response = await _client.postBytes(
Expand All @@ -98,7 +103,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
75 changes: 44 additions & 31 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,52 @@ 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;
Map<String, String> headers = <String, String>{};

// Set content-type from XFile.mimeType or detect using lookupMimeType
// If neither provides a type, let the server infer from the filename
String? contentType;
if (parseIsWeb) {
headers = <String, String>{
HttpHeaders.contentTypeHeader: file?.mimeType ??
lookupMimeType(url ?? file?.name ?? name,
headerBytes: await file?.readAsBytes()) ??
'application/octet-stream',
};
contentType =
file?.mimeType ??
lookupMimeType(
url ?? file?.name ?? name,
headerBytes: await file?.readAsBytes(),
);
} else {
headers = <String, String>{
HttpHeaders.contentTypeHeader: file?.mimeType ??
lookupMimeType(file!.path) ??
'application/octet-stream',
HttpHeaders.contentLengthHeader: '${await file!.length()}',
};
contentType = file?.mimeType ?? lookupMimeType(file!.path);
}

if (contentType != null) {
headers[HttpHeaders.contentTypeHeader] = contentType;
}

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 +141,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