Skip to content

Commit

Permalink
fix mime checks (#5118)
Browse files Browse the repository at this point in the history
  • Loading branch information
mozzius authored Sep 3, 2024
1 parent ea4d8bc commit 0bd0146
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
9 changes: 9 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,12 @@ export const GIF_FEATURED = (params: string) =>
`${GIF_SERVICE}/tenor/v2/featured?${params}`

export const MAX_LABELERS = 20

export const SUPPORTED_MIME_TYPES = [
'video/mp4',
'video/mpeg',
'video/webm',
'video/quicktime',
] as const

export type SupportedMimeTypes = (typeof SUPPORTED_MIME_TYPES)[number]
2 changes: 1 addition & 1 deletion src/lib/media/video/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ export async function compressVideo(

const info = await getVideoMetaData(compressed)

return {uri: compressed, size: info.size, mimeType: `video/${info.extension}`}
return {uri: compressed, size: info.size, mimeType: `video/mp4`}
}
6 changes: 5 additions & 1 deletion src/state/queries/video/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {useMemo} from 'react'
import {AtpAgent} from '@atproto/api'

import {SupportedMimeTypes} from '#/lib/constants'

const UPLOAD_ENDPOINT = 'https://video.bsky.app/'

export const createVideoEndpointUrl = (
Expand All @@ -25,14 +27,16 @@ export function useVideoAgent() {
}, [])
}

export function mimeToExt(mimeType: string) {
export function mimeToExt(mimeType: SupportedMimeTypes | (string & {})) {
switch (mimeType) {
case 'video/mp4':
return 'mp4'
case 'video/webm':
return 'webm'
case 'video/mpeg':
return 'mpeg'
case 'video/quicktime':
return 'mov'
default:
throw new Error(`Unsupported mime type: ${mimeType}`)
}
Expand Down
25 changes: 13 additions & 12 deletions src/state/queries/video/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {QueryClient, useQuery, useQueryClient} from '@tanstack/react-query'

import {SUPPORTED_MIME_TYPES, SupportedMimeTypes} from '#/lib/constants'
import {logger} from '#/logger'
import {isWeb} from '#/platform/detection'
import {ServerError, VideoTooLargeError} from 'lib/media/video/errors'
Expand Down Expand Up @@ -175,19 +176,19 @@ export function useUploadVideo({
})

const selectVideo = (asset: ImagePickerAsset) => {
switch (getMimeType(asset)) {
case 'video/mp4':
case 'video/mpeg':
case 'video/webm':
dispatch({
type: 'SetAsset',
asset,
})
onSelectVideo(asset)
break
default:
throw new Error(_(msg`Unsupported video type: ${getMimeType(asset)}`))
// compression step on native converts to mp4, so no need to check there
if (isWeb) {
const mimeType = getMimeType(asset)
if (!SUPPORTED_MIME_TYPES.includes(mimeType as SupportedMimeTypes)) {
throw new Error(_(msg`Unsupported video type: ${mimeType}`))
}
}

dispatch({
type: 'SetAsset',
asset,
})
onSelectVideo(asset)
}

const clearVideo = () => {
Expand Down

0 comments on commit 0bd0146

Please sign in to comment.