Skip to content

Commit e35ca71

Browse files
trashcoderfacebook-github-bot
authored andcommitted
Trashcoder/blob slice content type (#38078)
Summary: I added the contentType parameter to Blob.slice like it's in the MDN Web docs. This PR fixes #38058 When i slice a Blob for chunked uploads with react native i lost the content type, e.g. "image/jpeg", so the server doesn't know what kind of file he gets. In the docs of MDN the slice method was described with a third contentType parameter which was missing in Metas implementation. ## Changelog: [GENERAL] [ADDED] added a third parameter "contentType" to method slice of class Blob. <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests Pull Request resolved: #38078 Test Plan: I tested it with the unit-tests: yarn run test Blob-test.js yarn run v1.22.19 $ jest Blob-test.js PASS packages/react-native/Libraries/Blob/__tests__/Blob-test.js Blob ✓ should create empty blob (5 ms) ✓ should create blob from other blobs and strings ✓ should slice a blob (1 ms) ✓ should slice a blob and sets a contentType ✓ should close a blob (4 ms) My added unit test results "✓ should slice a blob and sets a contentType". Reviewed By: hoxyq Differential Revision: D47057162 Pulled By: blakef fbshipit-source-id: 0931b0b828f81b9b90562ffd51d4111c81034ffc
1 parent ebbd22c commit e35ca71

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

packages/react-native/Libraries/Blob/Blob.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Blob {
8181
return this._data;
8282
}
8383

84-
slice(start?: number, end?: number): Blob {
84+
slice(start?: number, end?: number, contentType: string = ''): Blob {
8585
const BlobManager = require('./BlobManager');
8686
let {offset, size} = this.data;
8787

@@ -109,6 +109,7 @@ class Blob {
109109
blobId: this.data.blobId,
110110
offset,
111111
size,
112+
type: contentType,
112113
/* Since `blob.slice()` creates a new view onto the same binary
113114
* data as the original blob, we should re-use the same collector
114115
* object so that the underlying resource gets deallocated when

packages/react-native/Libraries/Blob/__tests__/Blob-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ describe('Blob', function () {
7878
expect(sliceC.size).toBe(Math.min(blob.data.size, 34569) - 34543);
7979
});
8080

81+
it('should slice a blob and sets a contentType', () => {
82+
const blob = new Blob();
83+
84+
blob.data.size = 34546;
85+
86+
const slice = blob.slice(0, 2354, 'text/plain');
87+
88+
expect(slice.data.offset).toBe(0);
89+
expect(slice.size).toBe(2354);
90+
expect(slice.type).toBe('text/plain');
91+
});
92+
8193
it('should close a blob', () => {
8294
const blob = new Blob();
8395

0 commit comments

Comments
 (0)