From b554a388b5bfc0d3aa57330a2b78d11d0ecdd4a1 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Thu, 14 Aug 2025 12:22:47 -0500 Subject: [PATCH] Protect against no valid assets selected --- src/view/com/composer/SelectMediaButton.tsx | 92 +++++++++++---------- 1 file changed, 50 insertions(+), 42 deletions(-) diff --git a/src/view/com/composer/SelectMediaButton.tsx b/src/view/com/composer/SelectMediaButton.tsx index 4573080546..018667da4a 100644 --- a/src/view/com/composer/SelectMediaButton.tsx +++ b/src/view/com/composer/SelectMediaButton.tsx @@ -185,6 +185,12 @@ function classifyImagePickerAsset(asset: ImagePickerAsset): type = 'image' } + console.log({ + asset, + type, + mimeType, + }) + /* * If we weren't able to find a valid type, we don't support this asset. */ @@ -301,54 +307,56 @@ async function processImagePickerAssets( }) } - if (selectableAssetType === 'image') { - if (supportedAssets.length > selectionCountRemaining) { - errors.add(SelectedAssetError.MaxImages) - supportedAssets = supportedAssets.slice(0, selectionCountRemaining) - } - } else if (selectableAssetType === 'video') { - if (supportedAssets.length > 1) { - errors.add(SelectedAssetError.MaxVideos) - supportedAssets = supportedAssets.slice(0, 1) - } + if (supportedAssets.length > 0) { + if (selectableAssetType === 'image') { + if (supportedAssets.length > selectionCountRemaining) { + errors.add(SelectedAssetError.MaxImages) + supportedAssets = supportedAssets.slice(0, selectionCountRemaining) + } + } else if (selectableAssetType === 'video') { + if (supportedAssets.length > 1) { + errors.add(SelectedAssetError.MaxVideos) + supportedAssets = supportedAssets.slice(0, 1) + } - const selectedVideo = supportedAssets[0] + const selectedVideo = supportedAssets[0] - if (typeof selectedVideo.duration !== 'number') { - try { - const metadata = await getAdditionalVideoMetadata(selectedVideo) - selectedVideo.duration = metadata.duration - selectedVideo.width = metadata.width - selectedVideo.height = metadata.height - } catch (e: any) { - logger.error(`processSelectedAssets: failed to get video metadata`, { - safeMessage: e.message, - }) - errors.add(SelectedAssetError.Unsupported) + if (typeof selectedVideo.duration !== 'number') { + try { + const metadata = await getAdditionalVideoMetadata(selectedVideo) + selectedVideo.duration = metadata.duration + selectedVideo.width = metadata.width + selectedVideo.height = metadata.height + } catch (e: any) { + logger.error(`processSelectedAssets: failed to get video metadata`, { + safeMessage: e.message, + }) + errors.add(SelectedAssetError.Unsupported) + supportedAssets = [] + } + } else { + /* + * The `duration` is in seconds on web, but in milliseconds on + * native. We normalize to milliseconds. + */ + if (isWeb) { + selectedVideo.duration = selectedVideo.duration * 1000 + } + } + + if ( + selectedVideo.duration && + selectedVideo.duration > VIDEO_MAX_DURATION_MS + ) { + errors.add(SelectedAssetError.VideoTooLong) supportedAssets = [] } - } else { - /* - * The `duration` is in seconds on web, but in milliseconds on - * native. We normalize to milliseconds. - */ - if (isWeb) { - selectedVideo.duration = selectedVideo.duration * 1000 + } else if (selectableAssetType === 'gif') { + if (supportedAssets.length > 1) { + errors.add(SelectedAssetError.MaxGIFs) + supportedAssets = supportedAssets.slice(0, 1) } } - - if ( - selectedVideo.duration && - selectedVideo.duration > VIDEO_MAX_DURATION_MS - ) { - errors.add(SelectedAssetError.VideoTooLong) - supportedAssets = [] - } - } else if (selectableAssetType === 'gif') { - if (supportedAssets.length > 1) { - errors.add(SelectedAssetError.MaxGIFs) - supportedAssets = supportedAssets.slice(0, 1) - } } return {