From e94c030c45c4c2f8c62f2db140c5f89994587b2c Mon Sep 17 00:00:00 2001 From: Hamza Fouad <43830395+HamzaFouad@users.noreply.github.com> Date: Tue, 7 Nov 2023 10:06:52 +0200 Subject: [PATCH] feat: Add user's avatar to POST and PATCH requests in users endpoints (#11187) Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com> Co-authored-by: Peer Richelsen --- apps/api/lib/utils/isValidBase64Image.ts | 4 ++++ apps/api/lib/validations/user.ts | 3 +++ apps/api/pages/api/users/[userId]/_patch.ts | 3 +++ apps/api/pages/api/users/_post.ts | 3 +++ 4 files changed, 13 insertions(+) create mode 100644 apps/api/lib/utils/isValidBase64Image.ts diff --git a/apps/api/lib/utils/isValidBase64Image.ts b/apps/api/lib/utils/isValidBase64Image.ts new file mode 100644 index 0000000000..8c69df5506 --- /dev/null +++ b/apps/api/lib/utils/isValidBase64Image.ts @@ -0,0 +1,4 @@ +export function isValidBase64Image(input: string): boolean { + const regex = /^data:image\/[^;]+;base64,(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/; + return regex.test(input); +} diff --git a/apps/api/lib/validations/user.ts b/apps/api/lib/validations/user.ts index de9e22a976..09d389e41a 100644 --- a/apps/api/lib/validations/user.ts +++ b/apps/api/lib/validations/user.ts @@ -4,6 +4,7 @@ import { checkUsername } from "@calcom/lib/server/checkUsername"; import { _UserModel as User } from "@calcom/prisma/zod"; import { iso8601 } from "@calcom/prisma/zod-utils"; +import { isValidBase64Image } from "~/lib/utils/isValidBase64"; import { timeZone } from "~/lib/validations/shared/timeZone"; // @note: These are the ONLY values allowed as weekStart. So user don't introduce bad data. @@ -106,6 +107,7 @@ const schemaUserEditParams = z.object({ .optional() .nullable(), locale: z.nativeEnum(locales).optional().nullable(), + avatar: z.string().refine(isValidBase64Image).optional(), }); // @note: These are the values that are editable via PATCH method on the user Model, @@ -128,6 +130,7 @@ const schemaUserCreateParams = z.object({ .nullable(), locale: z.nativeEnum(locales).optional(), createdDate: iso8601.optional(), + avatar: z.string().refine(isValidBase64Image).optional(), }); // @note: These are the values that are editable via PATCH method on the user Model, diff --git a/apps/api/pages/api/users/[userId]/_patch.ts b/apps/api/pages/api/users/[userId]/_patch.ts index 84f6ffb45b..ff917c2048 100644 --- a/apps/api/pages/api/users/[userId]/_patch.ts +++ b/apps/api/pages/api/users/[userId]/_patch.ts @@ -65,6 +65,9 @@ import { schemaUserEditBodyParams, schemaUserReadPublic } from "~/lib/validation * locale: * description: The user's locale. Acceptable values are one of [EN, FR, IT, RU, ES, DE, PT, RO, NL, PT_BR, ES_419, KO, JA, PL, AR, IW, ZH_CH, ZH_TW, CS, SR, SV, VI] * type: string + * avatar: + * description: The user's avatar, in base64 format + * type: string * examples: * user: * summary: An example of USER diff --git a/apps/api/pages/api/users/_post.ts b/apps/api/pages/api/users/_post.ts index 15c68aa31d..bcc4f24315 100644 --- a/apps/api/pages/api/users/_post.ts +++ b/apps/api/pages/api/users/_post.ts @@ -60,6 +60,9 @@ import { schemaUserCreateBodyParams } from "~/lib/validations/user"; * locale: * description: The new user's locale. Acceptable values are one of [EN, FR, IT, RU, ES, DE, PT, RO, NL, PT_BR, ES_419, KO, JA, PL, AR, IW, ZH_CH, ZH_TW, CS, SR, SV, VI] * type: string + * avatar: + * description: The user's avatar, in base64 format + * type: string * examples: * user: * summary: An example of USER