-
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
8218f36
commit fa0f34b
Showing
16 changed files
with
890 additions
and
90 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { HeadBlobResult, PutBlobResult, del, head } from '@vercel/blob'; | ||
import { HandleUploadBody, handleUpload } from '@vercel/blob/client'; | ||
import { IncomingMessage } from 'http'; | ||
import { accessTokenPayload, jwt } from '../../plugins/jwt'; | ||
|
||
export default class BlobService { | ||
public async deleteBlob(url: string | string[]): Promise<void> { | ||
await del(url); | ||
} | ||
|
||
public async getBlobMetaData(url: string): Promise<HeadBlobResult | null> { | ||
return await head(url); | ||
} | ||
|
||
public async handleUpload( | ||
body: HandleUploadBody, | ||
request: IncomingMessage, | ||
allowedContentTypes: string[], | ||
onUploadCompleted: (body: { | ||
blob: PutBlobResult; | ||
tokenPayload?: string | undefined; | ||
}) => Promise<void>, | ||
formatTokenPayload?: ( | ||
clientPayload: string | undefined, | ||
accessTokenPayload: accessTokenPayload, | ||
) => string | undefined | Promise<string | undefined>, | ||
): Promise< | ||
| { type: 'blob.generate-client-token'; clientToken: string } | ||
| { type: 'blob.upload-completed'; response: 'ok' } | ||
> { | ||
return await handleUpload({ | ||
body: body, | ||
request: request, | ||
onBeforeGenerateToken: async (pathname, clientPayload) => { | ||
const accessToken = request.headers.authorization?.replace('Bearer ', '') ?? ''; | ||
|
||
try { | ||
jwt.verify(accessToken); | ||
} catch (error) { | ||
throw new Error('Unauthorized'); | ||
} | ||
|
||
const accessTokenPayload = jwt.decodeAccessToken(accessToken); | ||
|
||
return { | ||
allowedContentTypes, | ||
tokenPayload: formatTokenPayload | ||
? await formatTokenPayload(clientPayload, accessTokenPayload) | ||
: undefined, | ||
}; | ||
}, | ||
onUploadCompleted, | ||
}); | ||
} | ||
} |
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,90 @@ | ||
import { FastifyReply, FastifyRequest } from 'fastify'; | ||
import { UploadInput } from './item.schema'; | ||
import BlobService from './blob.service'; | ||
import { HandleUploadBody } from '@vercel/blob/client'; | ||
import ItemService from './item.service'; | ||
|
||
export default class ItemController { | ||
private itemService: ItemService; | ||
private blobService: BlobService; | ||
|
||
constructor(itemService: ItemService, blobService: BlobService) { | ||
this.blobService = blobService; | ||
this.itemService = itemService; | ||
} | ||
|
||
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 }) => { | ||
try { | ||
if (!tokenPayload) { | ||
request.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) { | ||
request.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, | ||
}); | ||
} catch (e) { | ||
request.log.error(e); | ||
await this.blobService.deleteBlob(blob.url); | ||
} | ||
}, | ||
async (clientPayload, accessTokenPayload) => { | ||
if (!clientPayload) { | ||
throw new Error('No clientPayload was passed!'); | ||
} | ||
|
||
const clientPayloadObject = JSON.parse(clientPayload); | ||
if (clientPayloadObject.parentId === undefined) { | ||
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(); | ||
} | ||
|
||
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.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(), | ||
blob: 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, | ||
}, | ||
}); | ||
} | ||
} |
Oops, something went wrong.