-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(upload,large-file-upload): add authorization and file schema
- Loading branch information
Showing
28 changed files
with
591 additions
and
38 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
19 changes: 19 additions & 0 deletions
19
apps/large-file-upload/prisma/migrations/20240805032703_add_clerk_id/migration.sql
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,19 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `email` on the `User` table. All the data in the column will be lost. | ||
- You are about to drop the column `name` on the `User` table. All the data in the column will be lost. | ||
- A unique constraint covering the columns `[clerkId]` on the table `User` will be added. If there are existing duplicate values, this will fail. | ||
- Added the required column `clerkId` to the `User` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- DropIndex | ||
DROP INDEX "User_email_key"; | ||
|
||
-- AlterTable | ||
ALTER TABLE "User" DROP COLUMN "email", | ||
DROP COLUMN "name", | ||
ADD COLUMN "clerkId" TEXT NOT NULL; | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_clerkId_key" ON "User"("clerkId"); |
11 changes: 11 additions & 0 deletions
11
apps/large-file-upload/prisma/migrations/20240805071220_add_file_model/migration.sql
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,11 @@ | ||
-- CreateTable | ||
CREATE TABLE "File" ( | ||
"id" INTEGER NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"userId" INTEGER NOT NULL, | ||
|
||
CONSTRAINT "File_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "File" ADD CONSTRAINT "File_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
25 changes: 25 additions & 0 deletions
25
apps/large-file-upload/prisma/migrations/20240805080231_/migration.sql
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 @@ | ||
/* | ||
Warnings: | ||
- The primary key for the `File` table will be changed. If it partially fails, the table could be left without primary key constraint. | ||
- The primary key for the `User` table will be changed. If it partially fails, the table could be left without primary key constraint. | ||
*/ | ||
-- DropForeignKey | ||
ALTER TABLE "File" DROP CONSTRAINT "File_userId_fkey"; | ||
|
||
-- AlterTable | ||
ALTER TABLE "File" DROP CONSTRAINT "File_pkey", | ||
ALTER COLUMN "id" SET DATA TYPE TEXT, | ||
ALTER COLUMN "userId" SET DATA TYPE TEXT, | ||
ADD CONSTRAINT "File_pkey" PRIMARY KEY ("id"); | ||
|
||
-- AlterTable | ||
ALTER TABLE "User" DROP CONSTRAINT "User_pkey", | ||
ALTER COLUMN "id" DROP DEFAULT, | ||
ALTER COLUMN "id" SET DATA TYPE TEXT, | ||
ADD CONSTRAINT "User_pkey" PRIMARY KEY ("id"); | ||
DROP SEQUENCE "User_id_seq"; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "File" ADD CONSTRAINT "File_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
12 changes: 12 additions & 0 deletions
12
apps/large-file-upload/prisma/migrations/20240805080514_add_hash/migration.sql
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,12 @@ | ||
/* | ||
Warnings: | ||
- A unique constraint covering the columns `[hash]` on the table `File` will be added. If there are existing duplicate values, this will fail. | ||
- Added the required column `hash` to the `File` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "File" ADD COLUMN "hash" TEXT NOT NULL; | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "File_hash_key" ON "File"("hash"); |
29 changes: 29 additions & 0 deletions
29
apps/large-file-upload/prisma/migrations/20240805083021_m2m/migration.sql
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,29 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `userId` on the `File` table. All the data in the column will be lost. | ||
*/ | ||
-- DropForeignKey | ||
ALTER TABLE "File" DROP CONSTRAINT "File_userId_fkey"; | ||
|
||
-- AlterTable | ||
ALTER TABLE "File" DROP COLUMN "userId"; | ||
|
||
-- CreateTable | ||
CREATE TABLE "_UserFiles" ( | ||
"A" TEXT NOT NULL, | ||
"B" TEXT NOT NULL | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "_UserFiles_AB_unique" ON "_UserFiles"("A", "B"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "_UserFiles_B_index" ON "_UserFiles"("B"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "_UserFiles" ADD CONSTRAINT "_UserFiles_A_fkey" FOREIGN KEY ("A") REFERENCES "File"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "_UserFiles" ADD CONSTRAINT "_UserFiles_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
8 changes: 8 additions & 0 deletions
8
apps/large-file-upload/prisma/migrations/20240805084132_add_file_size/migration.sql
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,8 @@ | ||
/* | ||
Warnings: | ||
- Added the required column `size` to the `File` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "File" ADD COLUMN "size" INTEGER NOT NULL; |
2 changes: 2 additions & 0 deletions
2
apps/large-file-upload/prisma/migrations/20240805084417_remove_uniq_of_hash/migration.sql
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,2 @@ | ||
-- DropIndex | ||
DROP INDEX "File_hash_key"; |
6 changes: 6 additions & 0 deletions
6
.../large-file-upload/prisma/migrations/20240805091756_add_properties_for_file/migration.sql
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,6 @@ | ||
-- AlterTable | ||
ALTER TABLE "File" ADD COLUMN "chunkSize" INTEGER NOT NULL DEFAULT 0, | ||
ADD COLUMN "concurrency" INTEGER NOT NULL DEFAULT 0, | ||
ADD COLUMN "poolElapse" INTEGER NOT NULL DEFAULT 0, | ||
ADD COLUMN "state" INTEGER NOT NULL DEFAULT 0, | ||
ALTER COLUMN "size" SET DEFAULT 0; |
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
8 changes: 8 additions & 0 deletions
8
apps/large-file-upload/src/app/(auth)/sign-in/[[...sign-in]]/page.tsx
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,8 @@ | ||
import { SignIn } from "@clerk/nextjs"; | ||
import React from "react"; | ||
|
||
const SignInPage: React.FC = () => { | ||
return <SignIn />; | ||
}; | ||
|
||
export default SignInPage; |
8 changes: 8 additions & 0 deletions
8
apps/large-file-upload/src/app/(auth)/sign-up/[[...sign-up]]/page.tsx
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,8 @@ | ||
import { SignUp } from "@clerk/nextjs"; | ||
import React from "react"; | ||
|
||
const SignUpPage: React.FC = () => { | ||
return <SignUp />; | ||
}; | ||
|
||
export default SignUpPage; |
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 @@ | ||
import { log } from "@/log"; | ||
import { prisma } from "@/prisma/client"; | ||
import { WebhookEvent } from "@clerk/nextjs/server"; | ||
import { env } from "@npcs/env/server"; | ||
import { headers } from "next/headers"; | ||
import { Webhook } from "svix"; | ||
|
||
export async function POST(req: Request) { | ||
// You can find this in the Clerk Dashboard -> Webhooks -> choose the endpoint | ||
const WEBHOOK_SECRET = env.WEBHOOK_SECRET; | ||
|
||
if (!WEBHOOK_SECRET) { | ||
throw new Error( | ||
"Please add WEBHOOK_SECRET from Clerk Dashboard to .env or .env.local", | ||
); | ||
} | ||
|
||
// Get the headers | ||
const headerPayload = headers(); | ||
const svix_id = headerPayload.get("svix-id"); | ||
const svix_timestamp = headerPayload.get("svix-timestamp"); | ||
const svix_signature = headerPayload.get("svix-signature"); | ||
|
||
// If there are no headers, error out | ||
if (!svix_id || !svix_timestamp || !svix_signature) { | ||
return new Response("Error occured -- no svix headers", { | ||
status: 400, | ||
}); | ||
} | ||
|
||
// Get the body | ||
const payload = await req.json(); | ||
const body = JSON.stringify(payload); | ||
|
||
// Create a new Svix instance with your secret. | ||
const wh = new Webhook(WEBHOOK_SECRET); | ||
|
||
let evt: WebhookEvent; | ||
|
||
// Verify the payload with the headers | ||
try { | ||
evt = wh.verify(body, { | ||
"svix-id": svix_id, | ||
"svix-timestamp": svix_timestamp, | ||
"svix-signature": svix_signature, | ||
}) as WebhookEvent; | ||
} catch (err) { | ||
log.error("Error verifying webhook:", err); | ||
return new Response("Error occured", { | ||
status: 400, | ||
}); | ||
} | ||
|
||
// Do something with the payload | ||
// For this guide, you simply log the payload to the console | ||
const { id } = evt.data; | ||
const eventType = evt.type; | ||
switch (eventType) { | ||
case "user.created": { | ||
const { id } = evt.data; | ||
|
||
await prisma.user.create({ | ||
data: { | ||
clerkId: id, | ||
}, | ||
}); | ||
|
||
break; | ||
} | ||
|
||
case "user.deleted": { | ||
const { id } = evt.data; | ||
|
||
await prisma.user.delete({ | ||
where: { | ||
clerkId: id, | ||
}, | ||
}); | ||
|
||
break; | ||
} | ||
|
||
default: | ||
break; | ||
} | ||
log.log(`Webhook with and ID of ${id} and type of ${eventType}`); | ||
log.log("Webhook body:", body); | ||
|
||
return new Response("", { status: 200 }); | ||
} |
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
Oops, something went wrong.