diff --git a/src/components/Chat.tsx b/src/components/Chat.tsx index deca0d8..5dfe501 100644 --- a/src/components/Chat.tsx +++ b/src/components/Chat.tsx @@ -10,7 +10,7 @@ import { DiscordEmoji, } from "../lib/discordEmoji"; import { trimmedPost } from "../lib/reply"; -import { uploadFile } from "../lib/upload"; +import { uploadFile, getImageSize } from "../lib/upload"; import { useShallow } from "zustand/react/shallow"; import { Button } from "./Button"; import { Textarea } from "./Input"; @@ -207,6 +207,7 @@ export const EnterPostBase = (props: EnterPostBaseProps) => { errors.push(uploadedFile.message); break; } + const imageSize = await getImageSize(uploadedFile.response); setAttachments((attachments) => [ ...attachments, { @@ -214,6 +215,8 @@ export const EnterPostBase = (props: EnterPostBaseProps) => { id: uploadedFile.response.id, mime: file.type, size: file.size, + width: imageSize.width, + height: imageSize.height, } satisfies Attachment, ]); } diff --git a/src/lib/api/posts.ts b/src/lib/api/posts.ts index 3812cbf..6ac32e4 100644 --- a/src/lib/api/posts.ts +++ b/src/lib/api/posts.ts @@ -3,10 +3,7 @@ import { Slice } from "."; import { getCloudlink } from "./cloudlink"; import { Errorable, loadMore, request } from "./utils"; -export type Attachment = Omit< - z.infer, - "height" | "width" -> & { width?: number; height?: number }; +export type Attachment = z.infer; const ATTACHMENT_SCHEMA = z.object({ filename: z.string(), height: z.number(), diff --git a/src/lib/upload.ts b/src/lib/upload.ts index d105eac..b5485e0 100644 --- a/src/lib/upload.ts +++ b/src/lib/upload.ts @@ -47,6 +47,21 @@ export const uploadFile = async ( return { error: false, response: response }; }; +export const getImageSize = (file: UploadedFile) => { + return new Promise((resolve) => { + const image = new Image(); + image.addEventListener("load", () => { + resolve({ width: image.width, height: image.height }); + }); + image.src = `https://uploads.meower.org/attachments/${file.id}/${file.filename}`; + }); +}; + +export type ImageSize = { + width: number; + height: number; +}; + const IMAGE_SCHEMA = z.object({ bucket: z.string(), claimed: z.boolean(),