get image aspect ratio when restoring

This commit is contained in:
Samuel Newman
2026-01-28 11:07:18 +02:00
parent 39024e6a36
commit fe57c73c38
2 changed files with 138 additions and 113 deletions
+1 -1
View File
@@ -461,7 +461,7 @@ export const ComposePost = ({
}) })
// Convert server draft to composer posts (videos returned separately) // Convert server draft to composer posts (videos returned separately)
const {posts, restoredVideos} = draftToComposerPosts( const {posts, restoredVideos} = await draftToComposerPosts(
draftSummary.draft, draftSummary.draft,
loadedMedia, loadedMedia,
) )
+39 -14
View File
@@ -4,6 +4,7 @@
import {type AppBskyDraftDefs, RichText} from '@atproto/api' import {type AppBskyDraftDefs, RichText} from '@atproto/api'
import {nanoid} from 'nanoid/non-secure' import {nanoid} from 'nanoid/non-secure'
import {getImageDim} from '#/lib/media/manip'
import {type ComposerImage} from '#/state/gallery' import {type ComposerImage} from '#/state/gallery'
import {type Gif} from '#/state/queries/tenor' import {type Gif} from '#/state/queries/tenor'
import { import {
@@ -399,13 +400,14 @@ function parseGifFromUrl(
* the compression and upload pipeline. The caller should handle the restoredVideos * the compression and upload pipeline. The caller should handle the restoredVideos
* by initiating video processing for each entry. * by initiating video processing for each entry.
*/ */
export function draftToComposerPosts( export async function draftToComposerPosts(
draft: AppBskyDraftDefs.Draft, draft: AppBskyDraftDefs.Draft,
loadedMedia: Map<string, string>, loadedMedia: Map<string, string>,
): {posts: PostDraft[]; restoredVideos: Map<number, RestoredVideo>} { ): Promise<{posts: PostDraft[]; restoredVideos: Map<number, RestoredVideo>}> {
const restoredVideos = new Map<number, RestoredVideo>() const restoredVideos = new Map<number, RestoredVideo>()
const posts = draft.posts.map((post, index) => { const posts = await Promise.all(
draft.posts.map(async (post, index) => {
const richtext = new RichText({text: post.text || ''}) const richtext = new RichText({text: post.text || ''})
richtext.detectFacetsWithoutResolution() richtext.detectFacetsWithoutResolution()
@@ -417,28 +419,49 @@ export function draftToComposerPosts(
// Restore images // Restore images
if (post.embedImages && post.embedImages.length > 0) { if (post.embedImages && post.embedImages.length > 0) {
const images: ComposerImage[] = [] const imagePromises = post.embedImages.map(async img => {
for (const img of post.embedImages) {
const path = loadedMedia.get(img.localRef.path) const path = loadedMedia.get(img.localRef.path)
if (path) { if (!path) {
return null
}
let width = 0
let height = 0
try {
const dims = await getImageDim(path)
width = dims.width
height = dims.height
} catch (e) {
logger.warn('Failed to get image dimensions', {
path,
error: e,
})
}
logger.debug('restoring image with localRefPath', { logger.debug('restoring image with localRefPath', {
localRefPath: img.localRef.path, localRefPath: img.localRef.path,
loadedPath: path, loadedPath: path,
width,
height,
}) })
images.push({
return {
alt: img.alt || '', alt: img.alt || '',
// Preserve the original localRefPath for reuse when saving // Preserve the original localRefPath for reuse when saving
localRefPath: img.localRef.path, localRefPath: img.localRef.path,
source: { source: {
id: nanoid(), id: nanoid(),
path, path,
width: 0, // Will be recalculated when loaded width,
height: 0, height,
mime: 'image/jpeg', // Default, will be detected mime: 'image/jpeg',
}, },
} as ComposerImage
}) })
}
} const images = (await Promise.all(imagePromises)).filter(
(img): img is ComposerImage => img !== null,
)
if (images.length > 0) { if (images.length > 0) {
embed.media = {type: 'images', images} embed.media = {type: 'images', images}
} }
@@ -501,7 +524,8 @@ export function draftToComposerPosts(
mimeType, mimeType,
localRefPath: vid.localRef.path, localRefPath: vid.localRef.path,
captions: captions:
vid.captions?.map(c => ({lang: c.lang, content: c.content})) ?? [], vid.captions?.map(c => ({lang: c.lang, content: c.content})) ??
[],
}) })
} }
} }
@@ -538,7 +562,8 @@ export function draftToComposerPosts(
labels, labels,
embed, embed,
} as PostDraft } as PostDraft
}) }),
)
return {posts, restoredVideos} return {posts, restoredVideos}
} }