Skip to content

Commit

Permalink
Update chunk-upload-action example
Browse files Browse the repository at this point in the history
  • Loading branch information
a179346 committed Feb 28, 2024
1 parent 2691518 commit 318d0e6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
29 changes: 10 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function UploadForm() {

"use server";

import { createWriteStream } from "fs";
import { FileHandle, open } from "fs/promises";
import { join } from "path";

export async function chunkUploadAction(
Expand All @@ -103,24 +103,15 @@ export async function chunkUploadAction(
metadata: { name: string }
) {
const buffer = Buffer.from(base64Chunk, "base64");
const path = join("./uploads", metadata.name);

const writeable = createWriteStream(
path,
offset === 0
? { flags: "w" }
: {
flags: "r+",
start: offset,
}
);

return new Promise<void>((resolve, reject) => {
writeable.on("finish", () => resolve());
writeable.on("error", reject);
writeable.write(buffer);
writeable.end();
});
const filePath = join("./uploads", metadata.name);

let fileHandle: FileHandle | null = null;
try {
fileHandle = await open(filePath, offset === 0 ? "w" : "r+");
await fileHandle.write(buffer, 0, buffer.length, offset);
} finally {
await fileHandle?.close();
}
}
```

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": "1.2.0",
"version": "1.3.0",
"description": "Uploading large files with chunking using server action in Next.js",
"main": "dist/index.js",
"scripts": {
Expand Down

0 comments on commit 318d0e6

Please sign in to comment.