Protect against no valid assets selected

This commit is contained in:
Eric Bailey
2025-08-14 12:22:47 -05:00
parent 5a99ae973d
commit b554a388b5
+50 -42
View File
@@ -185,6 +185,12 @@ function classifyImagePickerAsset(asset: ImagePickerAsset):
type = 'image' type = 'image'
} }
console.log({
asset,
type,
mimeType,
})
/* /*
* If we weren't able to find a valid type, we don't support this asset. * 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 > 0) {
if (supportedAssets.length > selectionCountRemaining) { if (selectableAssetType === 'image') {
errors.add(SelectedAssetError.MaxImages) if (supportedAssets.length > selectionCountRemaining) {
supportedAssets = supportedAssets.slice(0, selectionCountRemaining) errors.add(SelectedAssetError.MaxImages)
} supportedAssets = supportedAssets.slice(0, selectionCountRemaining)
} else if (selectableAssetType === 'video') { }
if (supportedAssets.length > 1) { } else if (selectableAssetType === 'video') {
errors.add(SelectedAssetError.MaxVideos) if (supportedAssets.length > 1) {
supportedAssets = supportedAssets.slice(0, 1) errors.add(SelectedAssetError.MaxVideos)
} supportedAssets = supportedAssets.slice(0, 1)
}
const selectedVideo = supportedAssets[0] const selectedVideo = supportedAssets[0]
if (typeof selectedVideo.duration !== 'number') { if (typeof selectedVideo.duration !== 'number') {
try { try {
const metadata = await getAdditionalVideoMetadata(selectedVideo) const metadata = await getAdditionalVideoMetadata(selectedVideo)
selectedVideo.duration = metadata.duration selectedVideo.duration = metadata.duration
selectedVideo.width = metadata.width selectedVideo.width = metadata.width
selectedVideo.height = metadata.height selectedVideo.height = metadata.height
} catch (e: any) { } catch (e: any) {
logger.error(`processSelectedAssets: failed to get video metadata`, { logger.error(`processSelectedAssets: failed to get video metadata`, {
safeMessage: e.message, safeMessage: e.message,
}) })
errors.add(SelectedAssetError.Unsupported) 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 = [] supportedAssets = []
} }
} else { } else if (selectableAssetType === 'gif') {
/* if (supportedAssets.length > 1) {
* The `duration` is in seconds on web, but in milliseconds on errors.add(SelectedAssetError.MaxGIFs)
* native. We normalize to milliseconds. supportedAssets = supportedAssets.slice(0, 1)
*/
if (isWeb) {
selectedVideo.duration = selectedVideo.duration * 1000
} }
} }
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 { return {