Skip to content

Commit

Permalink
Use already loaded file instead of fetching it for size
Browse files Browse the repository at this point in the history
  • Loading branch information
mybearworld committed Jul 6, 2024
1 parent b966fb9 commit f96492f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
{
Expand Down
17 changes: 17 additions & 0 deletions src/lib/imageSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const getImageSize = (file: string) => {
return new Promise<ImageSize>((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;
};
15 changes: 0 additions & 15 deletions src/lib/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,6 @@ export const uploadFile = async (
return { error: false, response: response };
};

export const getImageSize = (file: UploadedFile) => {
return new Promise<ImageSize>((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(),
Expand Down

0 comments on commit f96492f

Please sign in to comment.