Skip to content

Commit

Permalink
Trashcoder/blob slice content type (#38078)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
trashcoder authored and facebook-github-bot committed Jun 27, 2023
1 parent ebbd22c commit e35ca71
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/react-native/Libraries/Blob/Blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Blob {
return this._data;
}

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

Expand Down Expand Up @@ -109,6 +109,7 @@ class Blob {
blobId: this.data.blobId,
offset,
size,
type: contentType,
/* Since `blob.slice()` creates a new view onto the same binary
* data as the original blob, we should re-use the same collector
* object so that the underlying resource gets deallocated when
Expand Down
12 changes: 12 additions & 0 deletions packages/react-native/Libraries/Blob/__tests__/Blob-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ describe('Blob', function () {
expect(sliceC.size).toBe(Math.min(blob.data.size, 34569) - 34543);
});

it('should slice a blob and sets a contentType', () => {
const blob = new Blob();

blob.data.size = 34546;

const slice = blob.slice(0, 2354, 'text/plain');

expect(slice.data.offset).toBe(0);
expect(slice.size).toBe(2354);
expect(slice.type).toBe('text/plain');
});

it('should close a blob', () => {
const blob = new Blob();

Expand Down

0 comments on commit e35ca71

Please sign in to comment.