Skip to content

Commit a0024c8

Browse files
authored
ENG-1418 - Add file size validation for asset props in Tldraw component (#768)
* Add file size validation for asset props in Tldraw component - Introduced `getValidFileSize` function to validate file size, ensuring it is a finite number greater than zero. - Updated `fileSizeProps` to conditionally include `fileSize` based on validation. - Refactored asset handling in `InsideEditorAndUiContext` to utilize the new validation logic for file sizes. * 0.17.1
1 parent 4dcc5b9 commit a0024c8

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

apps/roam/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "roam",
3-
"version": "0.17.0",
3+
"version": "0.17.1",
44
"description": "Discourse Graph Plugin for roamresearch.com",
55
"scripts": {
66
"dev": "tsx scripts/dev.ts",

apps/roam/src/components/canvas/Tldraw.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ export const MAX_WIDTH = "400px";
127127

128128
const ICON_URL = `data:image/svg+xml;utf8,${encodeURIComponent(WHITE_LOGO_SVG)}`;
129129

130+
/** Valid file size for asset props; undefined when unknown (e.g. Roam/file API not a real File) to avoid persisting null. */
131+
const getValidFileSize = (file: { size?: number }): number | undefined =>
132+
typeof file.size === "number" && Number.isFinite(file.size) && file.size > 0
133+
? file.size
134+
: undefined;
135+
136+
const fileSizeProps = (size: number | undefined): { fileSize?: number } =>
137+
size !== undefined ? { fileSize: size } : {};
138+
130139
export const isPageUid = (uid: string) =>
131140
!!window.roamAlphaAPI.pull("[:node/title]", [":block/uid", uid])?.[
132141
":node/title"
@@ -954,7 +963,7 @@ const InsideEditorAndUiContext = ({
954963
src: dataUrl,
955964
w: size.w,
956965
h: size.h,
957-
fileSize: file.size,
966+
...fileSizeProps(getValidFileSize(file)),
958967
mimeType: file.type,
959968
isAnimated,
960969
},
@@ -1025,7 +1034,7 @@ const InsideEditorAndUiContext = ({
10251034
src: dataUrl,
10261035
w: width,
10271036
h: height,
1028-
fileSize: file.size,
1037+
...fileSizeProps(getValidFileSize(file)),
10291038
mimeType: "image/svg+xml",
10301039
isAnimated: false,
10311040
},

0 commit comments

Comments
 (0)