-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upload banner/avatar, rekognition support
- Loading branch information
Showing
7 changed files
with
256 additions
and
4 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
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,117 @@ | ||
import { AppHandler } from "../handler" | ||
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager" | ||
import { createRoute } from "@hono/zod-openapi" | ||
import { GenericResponses } from "@/v2/lib/response-schemas" | ||
import { z } from "@hono/zod-openapi" | ||
import { CheckLabels } from "@/v2/lib/helpers/check-image-tags" | ||
import { generateID } from "@/v2/lib/oslo" | ||
import { getConnection } from "@/v2/db/turso" | ||
import { authUser } from "@/v2/db/schema" | ||
import { eq } from "drizzle-orm" | ||
|
||
const responseSchema = z.object({ | ||
success: z.literal(true), | ||
}) | ||
|
||
const requestBodySchema = z.object({ | ||
avatar: z | ||
.any() | ||
.openapi({ | ||
description: "The image of the avatar to upload.", | ||
example: "avatar", | ||
}) | ||
.refine((files) => files?.length == 1, "An image is required.") | ||
.refine( | ||
(files) => files?.[0]?.size <= 5 * 1024 * 1024, | ||
`Max file size is 5MB)` | ||
) | ||
.refine( | ||
(files) => files?.[0]?.type === "image/png", | ||
`Only image/png is accepted.` | ||
), | ||
}) | ||
|
||
const openRoute = createRoute({ | ||
path: "/upload/avatar", | ||
method: "post", | ||
summary: "Upload an avatar", | ||
description: "Upload a new avatar, png only.", | ||
tags: ["Auth"], | ||
request: { | ||
body: { | ||
content: { | ||
"multipart/form-data": { | ||
schema: requestBodySchema, | ||
}, | ||
}, | ||
}, | ||
}, | ||
responses: { | ||
200: { | ||
description: "The uploaded avatar", | ||
content: { | ||
"application/json": { | ||
schema: responseSchema, | ||
}, | ||
}, | ||
}, | ||
...GenericResponses, | ||
}, | ||
}) | ||
|
||
export const UploadAvatarRoute = (handler: AppHandler) => { | ||
handler.openapi(openRoute, async (ctx) => { | ||
const { avatar } = ctx.req.valid("form") | ||
|
||
const authSessionManager = new AuthSessionManager(ctx) | ||
|
||
const { user } = await authSessionManager.validateSession() | ||
|
||
if (!user) { | ||
return ctx.json( | ||
{ | ||
success: false, | ||
message: "Unauthorized", | ||
}, | ||
401 | ||
) | ||
} | ||
|
||
const labels = await CheckLabels(ctx, avatar) | ||
|
||
if (labels) { | ||
return ctx.json( | ||
{ | ||
success: false, | ||
message: "Image contains potentially inappropriate content", | ||
}, | ||
400 | ||
) | ||
} | ||
|
||
const { drizzle } = await getConnection(ctx.env) | ||
|
||
if (user.avatarUrl) { | ||
await ctx.env.FILES_BUCKET.delete(user.avatarUrl) | ||
} | ||
|
||
const { key } = await ctx.env.FILES_BUCKET.put( | ||
`/avatars/${user.id}/${generateID(12)}.png`, | ||
avatar.content | ||
) | ||
|
||
await drizzle | ||
.update(authUser) | ||
.set({ | ||
avatarUrl: key, | ||
}) | ||
.where(eq(authUser.id, user.id)) | ||
|
||
return ctx.json( | ||
{ | ||
success: true, | ||
}, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { AppHandler } from "../handler" | ||
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager" | ||
import { createRoute } from "@hono/zod-openapi" | ||
import { GenericResponses } from "@/v2/lib/response-schemas" | ||
import { z } from "@hono/zod-openapi" | ||
import { CheckLabels } from "@/v2/lib/helpers/check-image-tags" | ||
import { generateID } from "@/v2/lib/oslo" | ||
import { getConnection } from "@/v2/db/turso" | ||
import { authUser } from "@/v2/db/schema" | ||
import { eq } from "drizzle-orm" | ||
|
||
const responseSchema = z.object({ | ||
success: z.literal(true), | ||
}) | ||
|
||
const requestBodySchema = z.object({ | ||
banner: z | ||
.any() | ||
.openapi({ | ||
description: "The image of the banner to upload.", | ||
example: "banner", | ||
}) | ||
.refine((files) => files?.length == 1, "An image is required.") | ||
.refine( | ||
(files) => files?.[0]?.size <= 5 * 1024 * 1024, | ||
`Max file size is 5MB)` | ||
) | ||
.refine( | ||
(files) => files?.[0]?.type === "image/png", | ||
`Only image/png is accepted.` | ||
), | ||
}) | ||
|
||
const openRoute = createRoute({ | ||
path: "/upload/banner", | ||
method: "post", | ||
summary: "Upload a banner", | ||
description: "Upload a new banner, png only, supporter only.", | ||
tags: ["Auth"], | ||
request: { | ||
body: { | ||
content: { | ||
"multipart/form-data": { | ||
schema: requestBodySchema, | ||
}, | ||
}, | ||
}, | ||
}, | ||
responses: { | ||
200: { | ||
description: "The uploaded banner", | ||
content: { | ||
"application/json": { | ||
schema: responseSchema, | ||
}, | ||
}, | ||
}, | ||
...GenericResponses, | ||
}, | ||
}) | ||
|
||
export const UploadBannerRoute = (handler: AppHandler) => { | ||
handler.openapi(openRoute, async (ctx) => { | ||
const { banner } = ctx.req.valid("form") | ||
|
||
const authSessionManager = new AuthSessionManager(ctx) | ||
|
||
const { user } = await authSessionManager.validateSession() | ||
|
||
if (!user || user.plan !== "supporter") { | ||
return ctx.json( | ||
{ | ||
success: false, | ||
message: "Unauthorized", | ||
}, | ||
401 | ||
) | ||
} | ||
|
||
const labels = await CheckLabels(ctx, banner) | ||
|
||
if (labels) { | ||
return ctx.json( | ||
{ | ||
success: false, | ||
message: "Image contains potentially inappropriate content", | ||
}, | ||
400 | ||
) | ||
} | ||
|
||
const { drizzle } = await getConnection(ctx.env) | ||
|
||
if (user.bannerUrl) { | ||
await ctx.env.FILES_BUCKET.delete(user.bannerUrl) | ||
} | ||
|
||
const { key } = await ctx.env.FILES_BUCKET.put( | ||
`/banners/${user.id}/${generateID(12)}.png`, | ||
banner.content | ||
) | ||
|
||
await drizzle | ||
.update(authUser) | ||
.set({ | ||
bannerUrl: key, | ||
}) | ||
.where(eq(authUser.id, user.id)) | ||
|
||
return ctx.json( | ||
{ | ||
success: true, | ||
}, | ||
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