-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 添加服务端文件校验 * 补充 config test * 优化 README * 添加一些注释、说明、以及部分实现调整 * 添加 0 size 的文件测试 * 提升兼容性、优化 README * 更新注释文档 * 更新版本号 * update
- Loading branch information
Showing
12 changed files
with
145 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { CRC32 } from './crc32' | ||
import { MB } from './helper' | ||
|
||
function mockFile(size = 4, name = 'mock.jpg', type = 'image/jpg'): File { | ||
if (size >= 1024) throw new Error('the size is set too large.') | ||
|
||
const blob = new Blob(['1'.repeat(size * MB)], { type }) | ||
return new File([blob], name) | ||
} | ||
|
||
describe('test crc32', async () => { | ||
test('file', async () => { | ||
const crc32One = new CRC32() | ||
await expect(crc32One.file(mockFile(0))).resolves.toEqual(0) | ||
|
||
const crc32Two = new CRC32() | ||
await expect(crc32Two.file(mockFile(0.5))).resolves.toEqual(1610895105) | ||
|
||
const crc32Three = new CRC32() | ||
await expect(crc32Three.file(mockFile(1))).resolves.toEqual(3172987001) | ||
|
||
const crc32Four = new CRC32() | ||
await expect(crc32Four.file(mockFile(2))).resolves.toEqual(847982614) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* eslint-disable no-bitwise */ | ||
|
||
import { MB } from './helper' | ||
|
||
/** | ||
* 以下 class 实现参考 | ||
* https://github.com/Stuk/jszip/blob/d4702a70834bd953d4c2d0bc155fad795076631a/lib/crc32.js | ||
* 该实现主要针对大文件优化、对计算的值进行了 `>>> 0` 运算(为与服务端保持一致) | ||
*/ | ||
export class CRC32 { | ||
private crc = -1 | ||
private table = this.makeTable() | ||
|
||
private makeTable() { | ||
const table = new Array<number>() | ||
for (let i = 0; i < 256; i++) { | ||
let t = i | ||
for (let j = 0; j < 8; j++) { | ||
if (t & 1) { | ||
// IEEE 标准 | ||
t = (t >>> 1) ^ 0xEDB88320 | ||
} else { | ||
t >>>= 1 | ||
} | ||
} | ||
table[i] = t | ||
} | ||
|
||
return table | ||
} | ||
|
||
private append(data: Uint8Array) { | ||
let crc = this.crc | ||
for (let offset = 0; offset < data.byteLength; offset++) { | ||
crc = (crc >>> 8) ^ this.table[(crc ^ data[offset]) & 0xFF] | ||
} | ||
this.crc = crc | ||
} | ||
|
||
private compute() { | ||
return (this.crc ^ -1) >>> 0 | ||
} | ||
|
||
private async readAsUint8Array(file: File | Blob): Promise<Uint8Array> { | ||
if (typeof file.arrayBuffer === 'function') { | ||
return new Uint8Array(await file.arrayBuffer()) | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader() | ||
reader.onload = () => { | ||
if (reader.result == null) { | ||
reject() | ||
return | ||
} | ||
|
||
if (typeof reader.result === 'string') { | ||
reject() | ||
return | ||
} | ||
|
||
resolve(new Uint8Array(reader.result)) | ||
} | ||
reader.readAsArrayBuffer(file) | ||
}) | ||
} | ||
|
||
async file(file: File): Promise<number> { | ||
if (file.size <= MB) { | ||
this.append(await this.readAsUint8Array(file)) | ||
return this.compute() | ||
} | ||
|
||
const count = Math.ceil(file.size / MB) | ||
for (let index = 0; index < count; index++) { | ||
const start = index * MB | ||
const end = index === (count - 1) ? file.size : start + MB | ||
// eslint-disable-next-line no-await-in-loop | ||
const chuck = await this.readAsUint8Array(file.slice(start, end)) | ||
this.append(new Uint8Array(chuck)) | ||
} | ||
|
||
return this.compute() | ||
} | ||
|
||
static file(file: File): Promise<number> { | ||
const crc = new CRC32() | ||
return crc.file(file) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters