Skip to content

Commit

Permalink
Refactor ChunkUploadHandler and metadata interface
Browse files Browse the repository at this point in the history
  • Loading branch information
a179346 committed Feb 29, 2024
1 parent 10b229f commit d273d3b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ import { ChunkUploadHandler } from "nextjs-chunk-upload-action";

export const chunkUploadAction: ChunkUploadHandler<{ name: string }> = async (
chunkFormData,
offset,
metadata
) => {
const blob = chunkFormData.get("blob");
const offset = Number(chunkFormData.get("offset"));
const buffer = Buffer.from(await blob.arrayBuffer());
const filePath = join("./uploads", metadata.name);

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nextjs-chunk-upload-action",
"version": "2.0.0",
"version": "3.0.0",
"description": "Uploading large files with chunking using server action in Next.js",
"main": "dist/index.js",
"scripts": {
Expand Down
30 changes: 21 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
* Uploading large files with chunking using server action in Next.js
*/

export type Metadata = Record<string, string | number | boolean | undefined | null>;
type Primitive = string | boolean | number | undefined | null;

export type Metadata = Record<string, Primitive> | Primitive;

export interface ChunkFormData {
get(name: 'blob'): Blob;
get(name: 'offset'): `${number}`;
get(name: 'length'): `${number}`;
get(name: 'retry'): `${number}`;
get(name: 'total'): `${number}`;
get(name: 'isLastChunk'): 'true' | 'false';
}

export type ChunkUploadHandler<TMetadata extends Metadata> = (
export type ChunkUploadHandler<TMetadata extends Metadata = undefined> = (
chunkFormData: ChunkFormData,
offset: number,
metadata: TMetadata
) => Promise<void>;

Expand Down Expand Up @@ -53,9 +59,9 @@ export class ChunkUploader<TMetadata extends Metadata> {
this._onSuccess = options.onSuccess;
}

/**
/**********
* Public
*/
**********/
public get status() {
return this._status;
}
Expand All @@ -66,9 +72,9 @@ export class ChunkUploader<TMetadata extends Metadata> {
this._uploadChunk(0, 0);
}

/**
/**********
* Protected
*/
**********/
protected _status: ChunkUploaderStatus;

protected readonly _file: File;
Expand All @@ -87,8 +93,14 @@ export class ChunkUploader<TMetadata extends Metadata> {

const blob = this._file.slice(offset, to);
const chunkFormData = new FormData();
chunkFormData.append('blob', blob);
this._onChunkUpload(chunkFormData as ChunkFormData, offset, this._metadata)
chunkFormData.set('blob', blob);
chunkFormData.set('offset', offset.toString());
chunkFormData.set('length', blob.size.toString());
chunkFormData.set('retry', currentChunkRetry.toString());
chunkFormData.set('total', this._file.size.toString());
chunkFormData.set('isLastChunk', isLastChunk ? 'true' : 'false');

this._onChunkUpload(chunkFormData as ChunkFormData, this._metadata)
.then(() => {
if (this._onChunkComplete) this._onChunkComplete(to, this._file.size);
if (isLastChunk) {
Expand Down

0 comments on commit d273d3b

Please sign in to comment.