Skip to content

Commit ae68a7a

Browse files
Copilotmtrezza
andcommitted
Remove default content-type, only set if explicitly provided
Changed ParseXFile and ParseFile to not set any default content-type header. Content-type is now only set if explicitly provided (e.g., via file.mimeType in XFile). This allows Parse Server to always infer the MIME type from the file extension, addressing feedback from @mtrezza. Co-authored-by: mtrezza <5673677+mtrezza@users.noreply.github.com>
1 parent 67b48bd commit ae68a7a

File tree

4 files changed

+64
-83
lines changed

4 files changed

+64
-83
lines changed

packages/dart/lib/src/objects/parse_file.dart

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ class ParseFile extends ParseFileBase {
44
/// Creates a new file
55
///
66
/// {https://docs.parseplatform.org/rest/guide/#files/}
7-
ParseFile(this.file,
8-
{String? name,
9-
super.url,
10-
super.debug,
11-
super.client,
12-
super.autoSendSessionId})
13-
: super(
14-
name: name ?? path.basename(file?.path ?? ''),
15-
);
7+
ParseFile(
8+
this.file, {
9+
String? name,
10+
super.url,
11+
super.debug,
12+
super.client,
13+
super.autoSendSessionId,
14+
}) : super(name: name ?? path.basename(file?.path ?? ''));
1615

1716
File? file;
1817
CancelToken? _cancelToken;
@@ -62,14 +61,15 @@ class ParseFile extends ParseFileBase {
6261
//Creates a Fake Response to return the correct result
6362
final Map<String, String> response = <String, String>{
6463
'url': url!,
65-
'name': name
64+
'name': name,
6665
};
6766
return handleResponse<ParseFile>(
68-
this,
69-
ParseNetworkResponse(data: json.encode(response), statusCode: 201),
70-
ParseApiRQ.upload,
71-
_debug,
72-
parseClassName);
67+
this,
68+
ParseNetworkResponse(data: json.encode(response), statusCode: 201),
69+
ParseApiRQ.upload,
70+
_debug,
71+
parseClassName,
72+
);
7373
}
7474

7575
progressCallback ??= _progressCallback;
@@ -80,14 +80,7 @@ class ParseFile extends ParseFileBase {
8080
HttpHeaders.contentLengthHeader: '${file!.lengthSync()}',
8181
};
8282

83-
// Only set content-type if the file has no extension
84-
// If extension exists, let the server infer the MIME type from the filename
85-
final bool hasExtension = path.extension(name).isNotEmpty;
86-
87-
if (!hasExtension) {
88-
// No extension, set content-type to application/octet-stream as fallback
89-
headers[HttpHeaders.contentTypeHeader] = 'application/octet-stream';
90-
}
83+
// Do not set content-type - let the server infer it from the filename
9184

9285
try {
9386
final String uri = ParseCoreData().serverUrl + _path;
@@ -105,7 +98,12 @@ class ParseFile extends ParseFileBase {
10598
}
10699

107100
return handleResponse<ParseFile>(
108-
this, response, ParseApiRQ.upload, _debug, parseClassName);
101+
this,
102+
response,
103+
ParseApiRQ.upload,
104+
_debug,
105+
parseClassName,
106+
);
109107
} on Exception catch (e) {
110108
return handleException(e, ParseApiRQ.upload, _debug, parseClassName);
111109
}

packages/dart/lib/src/objects/parse_x_file.dart

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ class ParseXFile extends ParseFileBase {
44
/// Creates a new file base XFile
55
///
66
/// {https://docs.parseplatform.org/rest/guide/#files/}
7-
ParseXFile(this.file,
8-
{String? name,
9-
super.url,
10-
super.debug,
11-
super.client,
12-
super.autoSendSessionId})
13-
: super(
14-
name: name ?? path.basename(file?.path ?? ''),
15-
);
7+
ParseXFile(
8+
this.file, {
9+
String? name,
10+
super.url,
11+
super.debug,
12+
super.client,
13+
super.autoSendSessionId,
14+
}) : super(name: name ?? path.basename(file?.path ?? ''));
1615

1716
XFile? file;
1817
CancelToken? _cancelToken;
@@ -79,28 +78,26 @@ class ParseXFile extends ParseFileBase {
7978
//Creates a Fake Response to return the correct result
8079
final Map<String, String> response = <String, String>{
8180
'url': url!,
82-
'name': name
81+
'name': name,
8382
};
8483
return handleResponse<ParseXFile>(
85-
this,
86-
ParseNetworkResponse(data: json.encode(response), statusCode: 201),
87-
ParseApiRQ.upload,
88-
_debug,
89-
parseClassName);
84+
this,
85+
ParseNetworkResponse(data: json.encode(response), statusCode: 201),
86+
ParseApiRQ.upload,
87+
_debug,
88+
parseClassName,
89+
);
9090
}
9191

9292
progressCallback ??= _progressCallback;
9393

9494
_cancelToken = CancelToken();
9595
Map<String, String> headers = <String, String>{};
9696

97-
// Only set content-type if the file has no extension
98-
// If extension exists, let the server infer the MIME type from the filename
99-
final bool hasExtension = path.extension(name).isNotEmpty;
100-
101-
if (!hasExtension) {
102-
// No extension, set content-type to application/octet-stream as fallback
103-
headers[HttpHeaders.contentTypeHeader] = 'application/octet-stream';
97+
// Only set content-type if explicitly provided by the XFile
98+
// Otherwise, let the server infer the MIME type from the filename
99+
if (file?.mimeType != null) {
100+
headers[HttpHeaders.contentTypeHeader] = file!.mimeType!;
104101
}
105102

106103
if (!parseIsWeb) {
@@ -112,8 +109,9 @@ class ParseXFile extends ParseFileBase {
112109

113110
Stream<List<int>>? data;
114111
if (parseIsWeb) {
115-
data = Stream<List<int>>.fromIterable(
116-
<List<int>>[await file!.readAsBytes()]);
112+
data = Stream<List<int>>.fromIterable(<List<int>>[
113+
await file!.readAsBytes(),
114+
]);
117115
} else {
118116
data = file!.openRead();
119117
}
@@ -131,7 +129,12 @@ class ParseXFile extends ParseFileBase {
131129
name = map['name'].toString();
132130
}
133131
return handleResponse<ParseXFile>(
134-
this, response, ParseApiRQ.upload, _debug, parseClassName);
132+
this,
133+
response,
134+
ParseApiRQ.upload,
135+
_debug,
136+
parseClassName,
137+
);
135138
} on Exception catch (e) {
136139
return handleException(e, ParseApiRQ.upload, _debug, parseClassName);
137140
}

packages/dart/test/src/objects/parse_file_test.dart

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'dart:io';
22
import 'package:parse_server_sdk/parse_server_sdk.dart';
33
import 'package:test/test.dart';
4-
import 'package:path/path.dart' as path;
54

65
import '../../test_utils.dart';
76

@@ -17,8 +16,7 @@ void main() {
1716
expect(parseFile.name, 'bb.jpg');
1817
});
1918

20-
test('should detect file extension correctly for files with extensions',
21-
() {
19+
test('should handle files with various extensions', () {
2220
// Test various file extensions
2321
final testCases = [
2422
'image.jpg',
@@ -31,28 +29,20 @@ void main() {
3129
for (final filename in testCases) {
3230
File file = File('/path/to/$filename');
3331
final parseFile = ParseFile(file, name: filename);
34-
// Verify that the extension is detected
35-
expect(path.extension(parseFile.name).isNotEmpty, true,
36-
reason: '$filename should have an extension');
32+
// Verify that the name is set correctly
33+
expect(parseFile.name, filename);
3734
}
3835
});
3936

40-
test(
41-
'should detect missing extension correctly for files without extensions',
42-
() {
37+
test('should handle files without extensions', () {
4338
// Test files without extensions
44-
final testCases = [
45-
'image',
46-
'file',
47-
'document',
48-
];
39+
final testCases = ['image', 'file', 'document'];
4940

5041
for (final filename in testCases) {
5142
File file = File('/path/to/$filename');
5243
final parseFile = ParseFile(file, name: filename);
53-
// Verify that no extension is detected
54-
expect(path.extension(parseFile.name).isEmpty, true,
55-
reason: '$filename should not have an extension');
44+
// Verify that the name is set correctly
45+
expect(parseFile.name, filename);
5646
}
5747
});
5848
});

packages/dart/test/src/objects/parse_x_file_test.dart

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'package:cross_file/cross_file.dart';
22
import 'package:parse_server_sdk/parse_server_sdk.dart';
33
import 'package:test/test.dart';
4-
import 'package:path/path.dart' as path;
54

65
import '../../test_utils.dart';
76

@@ -17,8 +16,7 @@ void main() {
1716
expect(parseFile.name, 'bb.jpg');
1817
});
1918

20-
test('should detect file extension correctly for files with extensions',
21-
() {
19+
test('should handle files with various extensions', () {
2220
// Test various file extensions
2321
final testCases = [
2422
'image.jpg',
@@ -31,28 +29,20 @@ void main() {
3129
for (final filename in testCases) {
3230
XFile file = XFile('/path/to/$filename');
3331
final parseFile = ParseXFile(file, name: filename);
34-
// Verify that the extension is detected
35-
expect(path.extension(parseFile.name).isNotEmpty, true,
36-
reason: '$filename should have an extension');
32+
// Verify that the name is set correctly
33+
expect(parseFile.name, filename);
3734
}
3835
});
3936

40-
test(
41-
'should detect missing extension correctly for files without extensions',
42-
() {
37+
test('should handle files without extensions', () {
4338
// Test files without extensions
44-
final testCases = [
45-
'image',
46-
'file',
47-
'document',
48-
];
39+
final testCases = ['image', 'file', 'document'];
4940

5041
for (final filename in testCases) {
5142
XFile file = XFile('/path/to/$filename');
5243
final parseFile = ParseXFile(file, name: filename);
53-
// Verify that no extension is detected
54-
expect(path.extension(parseFile.name).isEmpty, true,
55-
reason: '$filename should not have an extension');
44+
// Verify that the name is set correctly
45+
expect(parseFile.name, filename);
5646
}
5747
});
5848
});

0 commit comments

Comments
 (0)