https://github.com/jwygithub/file-slice
A file slicing tool
https://github.com/jwygithub/file-slice
Last synced: 10 months ago
JSON representation
A file slicing tool
- Host: GitHub
- URL: https://github.com/jwygithub/file-slice
- Owner: jwyGithub
- License: mit
- Created: 2024-03-19T03:55:11.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-19T07:23:45.000Z (about 2 years ago)
- Last Synced: 2025-08-21T21:30:36.084Z (10 months ago)
- Language: TypeScript
- Size: 39.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# file-slice
A file slicing tool
## Install
### pnpm
```sh
pnpm add @jiangweiye/file-slice
```
### yarn
```sh
yarn add @jiangweiye/file-slice
```
### npm
```sh
npm install @jiangweiye/file-slice
```
## Usage
### vite
```typescript
import { sliceFile } from '@jiangweiye/file-slice';
// specify the imported file as worker
import WorkerScript from '@jiangweiye/file-slice/worker?worker';
const CHUNK_SIZE = 1024 * 1024 * 5;
const THREAD_COUNT = 4;
const selectFile = document.querySelector('#select-file');
selectFile?.addEventListener('change', async e => {
const file = (e.target as HTMLInputElement).files?.[0];
const chunks = await sliceFile({
file: file!,
chunkSize: CHUNK_SIZE,
threadCount: THREAD_COUNT,
workerScript: () => new WorkerScript()
});
console.log('chunks', chunks);
});
```
### webpack
```bash
npm install worker-loader -D
```
```javascript
module.exports = {
module: {
rules: [
{
test: /\.worker\.js$/,
use: { loader: 'worker-loader' }
}
]
}
};
```
```typescript
import { sliceFile } from '@jiangweiye/file-slice';
import WorkerScript from '@jiangweiye/file-slice/slice.worker.js';
const CHUNK_SIZE = 1024 * 1024 * 5;
const THREAD_COUNT = 4;
document.getElementById('select-file').addEventListener('change', async function (e) {
const file = e.target.files?.[0];
const chunks = await sliceFile({
file: file,
chunkSize: CHUNK_SIZE,
threadCount: THREAD_COUNT,
workerScript: () => new WorkerScript()
});
console.log('chunks', chunks);
});
```
### Options
```typescript
interface ISliceFileOptions {
/**
* @description 文件
*/
file: File;
/**
* @description 切片大小
* @default 1024 * 1024
*/
chunkSize?: number;
/**
* @description 线程数
* @default navigator.hardwareConcurrency || 4
*/
threadCount?: number;
/**
* @description worker脚本
*/
workerScript: ((...args: any[]) => Worker) | string;
}
```