Skip to content

Commit 1eb73d0

Browse files
Nerlinrolandjitsu
authored andcommitted
fix: replaced File constructor with Blob for Edge (#1)
1 parent 2eba2ab commit 1eb73d0

File tree

3 files changed

+901
-246
lines changed

3 files changed

+901
-246
lines changed

src/file.spec.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
import {COMMON_MIME_TYPES, toFileWithPath} from './file';
1+
import {clone, COMMON_MIME_TYPES, toFileWithPath} from './file';
2+
3+
class MockFile {
4+
public lastModified: number = 0;
5+
public name: string = '';
6+
public size: number = 0;
7+
public type: string = '';
8+
public blob: Blob;
9+
10+
constructor(fileBits: BlobPart[], fileName: string, options: FilePropertyBag) {
11+
this.lastModified = options.lastModified || 0;
12+
this.name = fileName;
13+
this.type = options.type || '';
14+
this.blob = new Blob(fileBits);
15+
this.size = this.blob.size;
16+
throw new Error('Error!');
17+
}
18+
19+
public slice() {
20+
return this.blob;
21+
}
22+
}
223

324
describe('toFile()', () => {
425
it('should be an instance of a File', () => {
@@ -56,6 +77,27 @@ describe('toFile()', () => {
5677
expect(fwp.lastModified).toEqual(file.lastModified);
5778
});
5879

80+
it('clones the File as a blob', () => {
81+
const g = global as any;
82+
const opts: FilePropertyBag = {
83+
type: 'plain/text',
84+
lastModified: 1234567
85+
};
86+
const data = JSON.stringify({ping: true});
87+
const file = new File([data], 'test.txt', opts);
88+
g.OriginalFile = File;
89+
g.File = MockFile;
90+
const clonedFile = clone(file);
91+
g.File = g.OriginalFile;
92+
93+
expect(clonedFile).toBeInstanceOf(Blob);
94+
expect(clonedFile === file).toBe(false);
95+
expect(clonedFile.name).toEqual(file.name);
96+
expect(clonedFile.type).toEqual(file.type);
97+
expect(clonedFile.size).toEqual(file.size);
98+
expect(clonedFile.lastModified).toEqual(file.lastModified);
99+
});
100+
59101
it('should behave like a File', done => {
60102
const data = {ping: true};
61103
const json = JSON.stringify(data);

src/file.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,16 @@ function withMimeType(file: File) {
4444
return clone(file);
4545
}
4646

47-
function clone(file: File, type?: string) {
47+
export function clone(file: File, fType?: string) {
4848
const data = file.slice();
49-
return new File([data], file.name, {
50-
lastModified: file.lastModified,
51-
type: type || file.type
52-
});
49+
const {name, lastModified} = file;
50+
const type = fType || file.type;
51+
52+
try {
53+
return new File([data], name, {lastModified, type});
54+
} catch (e) {
55+
const blob = new Blob([data], {type});
56+
Object.assign(blob, {name, lastModified});
57+
return blob as File;
58+
}
5359
}

0 commit comments

Comments
 (0)