pass through mimetype rather than assuming data uri

This commit is contained in:
Samuel Newman
2025-11-21 16:49:57 +02:00
parent 181c0c83d8
commit e2335da909
4 changed files with 13 additions and 6 deletions
+8 -2
View File
@@ -103,9 +103,15 @@ export function createInitialImages(
export async function pasteImage( export async function pasteImage(
uri: string, uri: string,
mimeType?: string,
): Promise<ComposerImageWithoutTransformation> { ): Promise<ComposerImageWithoutTransformation> {
const {width, height} = await getImageDim(uri) const {width, height} = await getImageDim(uri)
const match = /^data:(.+?);/.exec(uri)
let mime = mimeType
if (!mime) {
const match = /^data:(.+?);/.exec(uri)
mime = match ? match[1] : 'image/jpeg'
}
return { return {
alt: '', alt: '',
@@ -114,7 +120,7 @@ export async function pasteImage(
path: uri, path: uri,
width: width, width: width,
height: height, height: height,
mime: match ? match[1] : 'image/jpeg', mime,
}, },
} }
} }
+3 -2
View File
@@ -886,7 +886,7 @@ let ComposerPost = React.memo(function ComposerPost({
) )
const onPhotoPasted = useCallback( const onPhotoPasted = useCallback(
async (uri: string) => { async (uri: string, mimeType?: string) => {
if ( if (
uri.startsWith('data:video/') || uri.startsWith('data:video/') ||
(isWeb && uri.startsWith('data:image/gif')) (isWeb && uri.startsWith('data:image/gif'))
@@ -905,7 +905,8 @@ let ComposerPost = React.memo(function ComposerPost({
.then(blob => new File([blob], name, {type: mimeType})) .then(blob => new File([blob], name, {type: mimeType}))
onSelectVideo(post.id, await getVideoMetadata(file)) onSelectVideo(post.id, await getVideoMetadata(file))
} else { } else {
const res = await pasteImage(uri) const res = await pasteImage(uri, mimeType)
onImageAdd([res]) onImageAdd([res])
} }
}, },
@@ -130,7 +130,7 @@ export function TextInput({
const file = files.find(f => isAcceptedImageMimeType(f.type)) const file = files.find(f => isAcceptedImageMimeType(f.type))
if (file) { if (file) {
onPhotoPasted(file.data) onPhotoPasted(file.data, file.type)
} }
}, },
[onPhotoPasted], [onPhotoPasted],
@@ -26,7 +26,7 @@ export type TextInputProps = {
hasRightPadding: boolean hasRightPadding: boolean
isActive: boolean isActive: boolean
setRichText: (v: RichText) => void setRichText: (v: RichText) => void
onPhotoPasted: (uri: string) => void onPhotoPasted: (uri: string, mimeType?: string) => void
onPressPublish: (richtext: RichText) => void onPressPublish: (richtext: RichText) => void
onNewLink: (uri: string) => void onNewLink: (uri: string) => void
onError: (err: string) => void onError: (err: string) => void