Skip to content

Commit

Permalink
feat: files upload
Browse files Browse the repository at this point in the history
  • Loading branch information
huayemao committed May 26, 2024
1 parent f69ef30 commit e184aad
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
30 changes: 30 additions & 0 deletions app/(content)/upload/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import prisma from "@/lib/prisma";

export default async function UploadTest() {
const list = await prisma.file.findMany();

return (
<div className="py-4 pt-20 px-12">
{list.map((e) => (
<div key={e.id}>
{e.name}
{e.id}
</div>
))}

<form
action="/api/upload"
encType="multipart/form-data"
method="POST"
className="grid grid-cols-12 gap-6"
>
<input name="file" type="file" id="file"></input>
<div className="ltablet:col-span-6 col-span-12 lg:col-span-6">
<div className="prose space-y-4 border-muted-200 dark:border-muted-700 dark:bg-muted-800 relative w-full border bg-white transition-all duration-300 rounded-md ptablet:p-8 p-6 lg:p-8">
<button type="submit">确认</button>
</div>
</div>
</form>
</div>
);
}
2 changes: 0 additions & 2 deletions app/(home)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Footer } from "@/components/Footer";
import { Nav } from "@/components/Nav";
import { SITE_META } from "@/constants";
import { getAllCategories } from "@/lib/categories";
import { Categories } from "../../components/Categories";

export default async function MainLayout({
Expand All @@ -11,7 +10,6 @@ export default async function MainLayout({
children: JSX.Element;
params: any;
}) {
const categories = await getAllCategories();

return (
<>
Expand Down
27 changes: 27 additions & 0 deletions app/api/files/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import prisma from "@/lib/prisma";

export const revalidate = 60 * 60;

export async function GET(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const file = await prisma.file.findFirst({
where: {
name: params.id,
},
});

return new Response(file?.data, {
// headers: {
// "Content-Type": "image/jpeg",
// },
status: 200,
});
} catch (error) {
return new Response(`error: ${error.message}`, {
status: 400,
});
}
}
30 changes: 30 additions & 0 deletions app/api/upload/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import prisma from "@/lib/prisma";

export async function POST(request: Request) {
try {
const text = (await request.formData()).get("file");
if (text instanceof File) {
console.log(text.name);

const file = await prisma.file.create({
data: {
name: text.name,
data: Buffer.from(await text.arrayBuffer()),
mimeType: "",
},
});
console.log(file);
}

console.log(text instanceof File);
// Process the webhook payload
} catch (error) {
return new Response(`Webhook error: ${error.message}`, {
status: 400,
});
}

return new Response("Success!", {
status: 200,
});
}
7 changes: 7 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,10 @@ model settings {
key String @id
value String
}

model File {
id Int @id @default(autoincrement())
name String
data Bytes
mimeType String
}

0 comments on commit e184aad

Please sign in to comment.