diff --git a/src/components/Chat.tsx b/src/components/Chat.tsx index 5dfe501..5f2d767 100644 --- a/src/components/Chat.tsx +++ b/src/components/Chat.tsx @@ -9,8 +9,9 @@ import { urlFromDiscordEmoji, DiscordEmoji, } from "../lib/discordEmoji"; +import { getImageSize } from "../lib/imageSize"; import { trimmedPost } from "../lib/reply"; -import { uploadFile, getImageSize } from "../lib/upload"; +import { uploadFile } from "../lib/upload"; import { useShallow } from "zustand/react/shallow"; import { Button } from "./Button"; import { Textarea } from "./Input"; @@ -207,7 +208,7 @@ export const EnterPostBase = (props: EnterPostBaseProps) => { errors.push(uploadedFile.message); break; } - const imageSize = await getImageSize(uploadedFile.response); + const imageSize = await getImageSize(URL.createObjectURL(file)); setAttachments((attachments) => [ ...attachments, { diff --git a/src/lib/imageSize.ts b/src/lib/imageSize.ts new file mode 100644 index 0000000..d5555cd --- /dev/null +++ b/src/lib/imageSize.ts @@ -0,0 +1,17 @@ +export const getImageSize = (file: string) => { + return new Promise((resolve) => { + const image = new Image(); + image.addEventListener("load", () => { + resolve({ width: image.width, height: image.height }); + }); + image.addEventListener("error", () => { + resolve({ width: 0, height: 0 }); + }); + image.src = file; + }); +}; + +export type ImageSize = { + width: number; + height: number; +}; \ No newline at end of file diff --git a/src/lib/upload.ts b/src/lib/upload.ts index b5485e0..d105eac 100644 --- a/src/lib/upload.ts +++ b/src/lib/upload.ts @@ -47,21 +47,6 @@ 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(),