-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5eee44
commit 2454fcc
Showing
9 changed files
with
307 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
-- CreateTable | ||
CREATE TABLE "Item" ( | ||
"id" SERIAL NOT NULL, | ||
"name" VARCHAR(50) NOT NULL, | ||
"mimeType" VARCHAR(255) NOT NULL, | ||
"blobUrl" VARCHAR(1024) NOT NULL, | ||
"ownerId" INTEGER NOT NULL, | ||
"parentId" INTEGER, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
"deletedAt" TIMESTAMP(3), | ||
|
||
CONSTRAINT "Item_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "Item_blobUrl_idx" ON "Item"("blobUrl"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "Item_ownerId_idx" ON "Item"("ownerId"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "Item_parentId_idx" ON "Item"("parentId"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "Item_ownerId_parentId_idx" ON "Item"("ownerId", "parentId"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "Item_deletedAt_idx" ON "Item"("deletedAt"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Item" ADD CONSTRAINT "Item_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Item" ADD CONSTRAINT "Item_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "Item"("id") ON DELETE SET NULL ON UPDATE CASCADE; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { FastifyInstance, FastifyPluginOptions } from 'fastify'; | ||
|
||
import fastifyPlugin from 'fastify-plugin'; | ||
import itemRoute from './item.route'; | ||
import { itemSchemas } from './item.schema'; | ||
|
||
export default fastifyPlugin(async (fastify: FastifyInstance, options: FastifyPluginOptions) => { | ||
for (const schema of itemSchemas) { | ||
fastify.addSchema(schema); | ||
} | ||
|
||
await fastify.register(itemRoute, options); | ||
}); |
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,89 @@ | ||
import { FastifyReply, FastifyRequest } from 'fastify'; | ||
import { UploadInput } from './item.schema'; | ||
import ItemService from './item.service'; | ||
import { HandleUploadBody } from '@vercel/blob/client'; | ||
import BlobService from '../blob/blob.service'; | ||
|
||
export default class ItemController { | ||
private itemService: ItemService; | ||
private blobService: BlobService; | ||
|
||
constructor(itemService: ItemService, blobService: BlobService) { | ||
this.itemService = itemService; | ||
this.blobService = blobService; | ||
} | ||
|
||
public async uploadHandler( | ||
request: FastifyRequest<{ | ||
Body: UploadInput; | ||
}>, | ||
reply: FastifyReply, | ||
) { | ||
try { | ||
const jsonResponse = await this.blobService.handleUpload( | ||
request.body as HandleUploadBody, | ||
request.raw, | ||
['text/plain'], | ||
async ({ blob, tokenPayload }) => { | ||
fastify.log.info('bloby blob', blob); | ||
|
||
if (!tokenPayload) { | ||
fastify.log.error( | ||
"Vercel blob storage didn't pass a token payload!", | ||
blob, | ||
tokenPayload, | ||
); | ||
throw new Error('Unauthorized'); | ||
} | ||
|
||
const tokenPayloadObject = JSON.parse(tokenPayload); | ||
if (!tokenPayloadObject.ownerId) { | ||
fastify.log.error( | ||
"Vercel blob storage didn't pass a valid token payload! ownerId is missing!", | ||
blob, | ||
tokenPayload, | ||
); | ||
throw new Error('Unauthorized'); | ||
} | ||
|
||
await this.itemService.createItem({ | ||
name: blob.pathname, | ||
mimeType: blob.contentType, | ||
blobUrl: blob.url, | ||
ownerId: tokenPayloadObject.ownerId, | ||
parentId: tokenPayloadObject.parentId ?? null, | ||
}); | ||
}, | ||
async (clientPayload, accessTokenPayload) => { | ||
if (!clientPayload) { | ||
throw new Error('No clientPayload was passed!'); | ||
} | ||
|
||
const clientPayloadObject = JSON.parse(clientPayload); | ||
if ('parentId' in clientPayloadObject === false) { | ||
throw new Error('clientPayload.parentId is required!'); | ||
} | ||
|
||
return JSON.stringify({ | ||
parentId: clientPayloadObject.parentId, | ||
ownerId: accessTokenPayload.sub, | ||
}); | ||
}, | ||
); | ||
|
||
return reply.code(200).send(jsonResponse); | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
if (e.message === 'Unauthorized') { | ||
return reply.unauthorized(); | ||
} | ||
|
||
console.error(e); | ||
|
||
return reply.badRequest(e.message); | ||
} | ||
|
||
return reply.badRequest(); | ||
} | ||
} | ||
} |
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,23 @@ | ||
import { FastifyInstance } from 'fastify'; | ||
import ItemController from './item.controller'; | ||
import { $ref } from './item.schema'; | ||
import ItemService from './item.service'; | ||
import BlobService from '../blob/blob.service'; | ||
|
||
export default async (fastify: FastifyInstance) => { | ||
const itemController = new ItemController(new ItemService(), new BlobService()); | ||
|
||
fastify.post( | ||
'/', | ||
{ | ||
schema: { | ||
headers: { | ||
Authorization: true, | ||
}, | ||
tags: ['Item'], | ||
body: $ref('uploadSchema'), | ||
}, | ||
}, | ||
itemController.uploadHandler.bind(itemController), | ||
); | ||
}; |
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,39 @@ | ||
import { z } from 'zod'; | ||
import { buildJsonSchemas } from 'fastify-zod'; | ||
|
||
const uploadSchema = z.object({ | ||
type: z.enum(['blob.generate-client-token', 'blob.upload-completed']), | ||
payload: z.object({ | ||
pathname: z.string().optional(), | ||
callbackUrl: z.string().optional(), | ||
clientPayload: z.string().optional(), | ||
tokenPayload: z.string().optional(), | ||
item: z | ||
.object({ | ||
url: z.string(), | ||
pathname: z.string(), | ||
contentType: z.string(), | ||
contentDisposition: z.string(), | ||
}) | ||
.optional(), | ||
}), | ||
}); | ||
|
||
export type UploadInput = z.infer<typeof uploadSchema>; | ||
|
||
export type CreateItem = { | ||
name: string; | ||
mimeType: string; | ||
blobUrl: string; | ||
ownerId: number; | ||
parentId: number | null; | ||
}; | ||
|
||
export const { schemas: itemSchemas, $ref } = buildJsonSchemas( | ||
{ | ||
uploadSchema, | ||
}, | ||
{ | ||
$id: 'itemSchema', | ||
}, | ||
); |
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,16 @@ | ||
import { prisma } from '../../plugins/prisma'; | ||
import { CreateItem } from './item.schema'; | ||
|
||
export default class ItemService { | ||
public async createItem(input: CreateItem) { | ||
await prisma.item.create({ | ||
data: { | ||
name: input.name, | ||
mimeType: input.mimeType, | ||
blobUrl: input.blobUrl, | ||
ownerId: input.ownerId, | ||
parentId: input.parentId, | ||
}, | ||
}); | ||
} | ||
} |