From c180f7502c37902d71bada0b1707951abd1a148d Mon Sep 17 00:00:00 2001 From: Yash Kumar Date: Wed, 2 Sep 2026 20:57:30 +0530 Subject: [PATCH] refactor: extract resizeImage to shared utility --- .../organization/handle-organization.tsx | 39 +------------------ apps/dokploy/utils/image-processing.ts | 37 ++++++++++++++++++ 2 files changed, 38 insertions(+), 38 deletions(-) create mode 100644 apps/dokploy/utils/image-processing.ts diff --git a/apps/dokploy/components/dashboard/organization/handle-organization.tsx b/apps/dokploy/components/dashboard/organization/handle-organization.tsx index 786efb61a..4c5ba3e48 100644 --- a/apps/dokploy/components/dashboard/organization/handle-organization.tsx +++ b/apps/dokploy/components/dashboard/organization/handle-organization.tsx @@ -26,6 +26,7 @@ import { } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { api } from "@/utils/api"; +import { resizeImage } from "@/utils/image-processing"; import { sanitizeSvg } from "@/utils/sanitize-svg"; const organizationSchema = z.object({ @@ -44,44 +45,6 @@ interface Props { onOpenChange?: (open: boolean) => void; } -const resizeImage = (file: File, maxSize: number): Promise => { - return new Promise((resolve, reject) => { - const reader = new FileReader(); - reader.onload = (event) => { - const img = new Image(); - img.onload = () => { - let { width, height } = img; - - if (width > maxSize || height > maxSize) { - if (width > height) { - height = Math.round((height * maxSize) / width); - width = maxSize; - } else { - width = Math.round((width * maxSize) / height); - height = maxSize; - } - } - - const canvas = document.createElement("canvas"); - canvas.width = width; - canvas.height = height; - const ctx = canvas.getContext("2d"); - if (!ctx) { - resolve(event.target?.result as string); - return; - } - - ctx.drawImage(img, 0, 0, width, height); - resolve(canvas.toDataURL("image/webp", 0.8)); - }; - img.onerror = reject; - img.src = event.target?.result as string; - }; - reader.onerror = reject; - reader.readAsDataURL(file); - }); -}; - export function AddOrganization({ organizationId, open: controlledOpen, diff --git a/apps/dokploy/utils/image-processing.ts b/apps/dokploy/utils/image-processing.ts new file mode 100644 index 000000000..d2ae56f81 --- /dev/null +++ b/apps/dokploy/utils/image-processing.ts @@ -0,0 +1,37 @@ +export const resizeImage = (file: File, maxSize: number): Promise => { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = (event) => { + const img = new Image(); + img.onload = () => { + let { width, height } = img; + + if (width > maxSize || height > maxSize) { + if (width > height) { + height = Math.round((height * maxSize) / width); + width = maxSize; + } else { + width = Math.round((width * maxSize) / height); + height = maxSize; + } + } + + const canvas = document.createElement("canvas"); + canvas.width = width; + canvas.height = height; + const ctx = canvas.getContext("2d"); + if (!ctx) { + resolve(event.target?.result as string); + return; + } + + ctx.drawImage(img, 0, 0, width, height); + resolve(canvas.toDataURL("image/webp", 0.8)); + }; + img.onerror = reject; + img.src = event.target?.result as string; + }; + reader.onerror = reject; + reader.readAsDataURL(file); + }); +};