Rename image size constants and require them as params

POST_IMG_MAX no longer described post images, so rename it to the
semantically accurate pair: POST_IMG_MAX_SIZE (4000px/2MB, post images
and pasted URLs) and PROFILE_IMAGES_MAX_SIZE (2000px/1MB, avatars,
banners, onboarding, link thumbnails).

Drop the constant defaults from compressImage, compressIfNeeded, and
getResizedDimensions; callers now pass the appropriate max config object
directly so the size policy lives at the call site. Web avatar/banner
edits now use PROFILE_IMAGES_MAX_SIZE, matching the native profile path.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Eric Bailey
2026-06-02 11:44:52 -05:00
parent e14d559a13
commit 736acb8a61
14 changed files with 72 additions and 51 deletions
+17 -4
View File
@@ -1,6 +1,7 @@
import {createDownloadResumable, deleteAsync} from 'expo-file-system/legacy' import {createDownloadResumable, deleteAsync} from 'expo-file-system/legacy'
import {manipulateAsync, SaveFormat} from 'expo-image-manipulator' import {manipulateAsync, SaveFormat} from 'expo-image-manipulator'
import {PROFILE_IMAGES_MAX_SIZE} from '../../src/lib/constants'
import { import {
downloadAndResize, downloadAndResize,
type DownloadAndResizeOpts, type DownloadAndResizeOpts,
@@ -88,13 +89,19 @@ describe('downloadAndResize', () => {
width: 1200, width: 1200,
height: 1000, height: 1000,
} }
const resizedDimensionsOne = getResizedDimensions(initialDimensionsOne) const resizedDimensionsOne = getResizedDimensions(
initialDimensionsOne,
PROFILE_IMAGES_MAX_SIZE,
)
const initialDimensionsTwo = { const initialDimensionsTwo = {
width: 1000, width: 1000,
height: 1200, height: 1200,
} }
const resizedDimensionsTwo = getResizedDimensions(initialDimensionsTwo) const resizedDimensionsTwo = getResizedDimensions(
initialDimensionsTwo,
PROFILE_IMAGES_MAX_SIZE,
)
expect(resizedDimensionsOne).toEqual(initialDimensionsOne) expect(resizedDimensionsOne).toEqual(initialDimensionsOne)
expect(resizedDimensionsTwo).toEqual(initialDimensionsTwo) expect(resizedDimensionsTwo).toEqual(initialDimensionsTwo)
@@ -105,13 +112,19 @@ describe('downloadAndResize', () => {
width: 3000, width: 3000,
height: 1500, height: 1500,
} }
const resizedDimensionsOne = getResizedDimensions(initialDimensionsOne) const resizedDimensionsOne = getResizedDimensions(
initialDimensionsOne,
PROFILE_IMAGES_MAX_SIZE,
)
const initialDimensionsTwo = { const initialDimensionsTwo = {
width: 2000, width: 2000,
height: 4000, height: 4000,
} }
const resizedDimensionsTwo = getResizedDimensions(initialDimensionsTwo) const resizedDimensionsTwo = getResizedDimensions(
initialDimensionsTwo,
PROFILE_IMAGES_MAX_SIZE,
)
expect(resizedDimensionsOne).toEqual({ expect(resizedDimensionsOne).toEqual({
width: 2000, width: 2000,
+5 -1
View File
@@ -21,6 +21,7 @@ import {sha256} from 'js-sha256'
import {CID} from 'multiformats/cid' import {CID} from 'multiformats/cid'
import * as Hasher from 'multiformats/hashes/hasher' import * as Hasher from 'multiformats/hashes/hasher'
import {POST_IMG_MAX_SIZE} from '#/lib/constants'
import {isNetworkError} from '#/lib/strings/errors' import {isNetworkError} from '#/lib/strings/errors'
import {shortenLinks, stripInvalidMentions} from '#/lib/strings/rich-text-manip' import {shortenLinks, stripInvalidMentions} from '#/lib/strings/rich-text-manip'
import {logger} from '#/logger' import {logger} from '#/logger'
@@ -323,7 +324,10 @@ async function resolveMedia(
const images: AppBskyEmbedImages.Image[] = await Promise.all( const images: AppBskyEmbedImages.Image[] = await Promise.all(
imagesDraft.map(async (image, i) => { imagesDraft.map(async (image, i) => {
logger.debug(`Compressing image #${i}`) logger.debug(`Compressing image #${i}`)
const {path, width, height, mime} = await compressImage(image) const {path, width, height, mime} = await compressImage(
image,
POST_IMG_MAX_SIZE,
)
logger.debug(`Uploading image #${i}`) logger.debug(`Uploading image #${i}`)
const res = await uploadBlob(agent, path, mime) const res = await uploadBlob(agent, path, mime)
return { return {
+3 -3
View File
@@ -6,7 +6,7 @@ import {
} from '@atproto/api' } from '@atproto/api'
import {AtUri} from '@atproto/api' import {AtUri} from '@atproto/api'
import {POST_IMG_MAX} from '#/lib/constants' import {PROFILE_IMAGES_MAX_SIZE} from '#/lib/constants'
import {getLinkMeta, type LinkMeta} from '#/lib/link-meta/link-meta' import {getLinkMeta, type LinkMeta} from '#/lib/link-meta/link-meta'
import {resolveShortLink} from '#/lib/link-meta/resolve-short-link' import {resolveShortLink} from '#/lib/link-meta/resolve-short-link'
import {downloadAndResize} from '#/lib/media/manip' import {downloadAndResize} from '#/lib/media/manip'
@@ -257,8 +257,8 @@ export async function imageToThumb(
try { try {
const img = await downloadAndResize({ const img = await downloadAndResize({
uri: imageUri, uri: imageUri,
maxDimension: POST_IMG_MAX.width, maxDimension: PROFILE_IMAGES_MAX_SIZE.width,
maxSize: POST_IMG_MAX.size, maxSize: PROFILE_IMAGES_MAX_SIZE.size,
timeout: 15e3, timeout: 15e3,
}) })
if (img) { if (img) {
+7 -7
View File
@@ -96,18 +96,18 @@ export const STAGING_FEEDS = [
`feedgen|${STAGING_DEFAULT_FEED('thevids')}`, `feedgen|${STAGING_DEFAULT_FEED('thevids')}`,
] ]
export const POST_IMG_MAX = { export const POST_IMG_MAX_SIZE = {
width: 2000,
height: 2000,
size: 1000000,
}
export const POST_IMG_MAX_HIGH_RES = {
width: 4000, width: 4000,
height: 4000, height: 4000,
size: 2000000, size: 2000000,
} }
export const PROFILE_IMAGES_MAX_SIZE = {
width: 2000,
height: 2000,
size: 1000000,
}
export const STAGING_LINK_META_PROXY = export const STAGING_LINK_META_PROXY =
'https://cardyb.staging.bsky.dev/v1/extract?url=' 'https://cardyb.staging.bsky.dev/v1/extract?url='
+4 -5
View File
@@ -16,7 +16,6 @@ import {manipulateAsync, SaveFormat} from 'expo-image-manipulator'
import * as MediaLibrary from 'expo-media-library' import * as MediaLibrary from 'expo-media-library'
import * as Sharing from 'expo-sharing' import * as Sharing from 'expo-sharing'
import {POST_IMG_MAX} from '#/lib/constants'
import {logger} from '#/logger' import {logger} from '#/logger'
import {IS_ANDROID, IS_IOS} from '#/env' import {IS_ANDROID, IS_IOS} from '#/env'
import {type PickerImage} from './picker.shared' import {type PickerImage} from './picker.shared'
@@ -25,14 +24,14 @@ import {convertCdnPreset, getResizedDimensions} from './util'
export async function compressIfNeeded( export async function compressIfNeeded(
img: PickerImage, img: PickerImage,
maxSize: number = POST_IMG_MAX.size, max: {width: number; size: number},
): Promise<PickerImage> { ): Promise<PickerImage> {
if (img.size < maxSize) { if (img.size < max.size) {
return img return img
} }
const resizedImage = await doResize(normalizePath(img.path), { const resizedImage = await doResize(normalizePath(img.path), {
maxDimension: POST_IMG_MAX.width, maxDimension: max.width,
maxSize, maxSize: max.size,
}) })
const finalImageMovedPath = await moveToPermanentPath( const finalImageMovedPath = await moveToPermanentPath(
resizedImage.path, resizedImage.path,
+4 -5
View File
@@ -1,4 +1,3 @@
import {POST_IMG_MAX} from '#/lib/constants'
import {type PickerImage} from './picker.shared' import {type PickerImage} from './picker.shared'
import {type Dimensions} from './types' import {type Dimensions} from './types'
import { import {
@@ -10,14 +9,14 @@ import {
export async function compressIfNeeded( export async function compressIfNeeded(
img: PickerImage, img: PickerImage,
maxSize: number, max: {width: number; size: number},
): Promise<PickerImage> { ): Promise<PickerImage> {
if (img.size < maxSize) { if (img.size < max.size) {
return img return img
} }
return await doResize(img.path, { return await doResize(img.path, {
maxDimension: POST_IMG_MAX.width, maxDimension: max.width,
maxSize, maxSize: max.size,
}) })
} }
+11 -7
View File
@@ -8,6 +8,7 @@ import ExpoImageCropTool, {
type OpenCropperOptions, type OpenCropperOptions,
} from '@bsky.app/expo-image-crop-tool' } from '@bsky.app/expo-image-crop-tool'
import {PROFILE_IMAGES_MAX_SIZE} from '#/lib/constants'
import {compressIfNeeded} from './manip' import {compressIfNeeded} from './manip'
import {type PickerImage} from './picker.shared' import {type PickerImage} from './picker.shared'
@@ -28,13 +29,16 @@ async function getFile() {
throw new Error('Failed to get file info') throw new Error('Failed to get file info')
} }
return await compressIfNeeded({ return await compressIfNeeded(
path: file, {
mime: 'image/jpeg', path: file,
size: fileInfo.size, mime: 'image/jpeg',
width: 4288, size: fileInfo.size,
height: 2848, width: 4288,
}) height: 2848,
},
PROFILE_IMAGES_MAX_SIZE,
)
} }
export async function openPicker(): Promise<PickerImage[]> { export async function openPicker(): Promise<PickerImage[]> {
+1 -3
View File
@@ -1,5 +1,3 @@
import {POST_IMG_MAX} from '#/lib/constants'
export function extractDataUriMime(uri: string): string { export function extractDataUriMime(uri: string): string {
return uri.substring(uri.indexOf(':') + 1, uri.indexOf(';')) return uri.substring(uri.indexOf(':') + 1, uri.indexOf(';'))
} }
@@ -9,7 +7,7 @@ export function getResizedDimensions(
width: number width: number
height: number height: number
}, },
max: {width: number; height: number} = POST_IMG_MAX, max: {width: number; height: number},
) { ) {
if (originalDims.width <= max.width && originalDims.height <= max.height) { if (originalDims.width <= max.width && originalDims.height <= max.height) {
return originalDims return originalDims
+2 -1
View File
@@ -19,6 +19,7 @@ import {msg} from '@lingui/core/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
import {Trans} from '@lingui/react/macro' import {Trans} from '@lingui/react/macro'
import {PROFILE_IMAGES_MAX_SIZE} from '#/lib/constants'
import {usePhotoLibraryPermission} from '#/lib/hooks/usePermissions' import {usePhotoLibraryPermission} from '#/lib/hooks/usePermissions'
import {compressIfNeeded} from '#/lib/media/manip' import {compressIfNeeded} from '#/lib/media/manip'
import {openCropper} from '#/lib/media/picker' import {openCropper} from '#/lib/media/picker'
@@ -212,7 +213,7 @@ export function StepProfile() {
} }
} }
} }
image = await compressIfNeeded(image, 1000000) image = await compressIfNeeded(image, PROFILE_IMAGES_MAX_SIZE)
// If we are on mobile, prefetching the image will load the image into memory before we try and display it, // If we are on mobile, prefetching the image will load the image into memory before we try and display it,
// stopping any brief flickers. // stopping any brief flickers.
+5 -8
View File
@@ -13,7 +13,6 @@ import {
} from 'expo-image-manipulator' } from 'expo-image-manipulator'
import {nanoid} from 'nanoid/non-secure' import {nanoid} from 'nanoid/non-secure'
import {POST_IMG_MAX_HIGH_RES} from '#/lib/constants'
import {getImageDim} from '#/lib/media/manip' import {getImageDim} from '#/lib/media/manip'
import {openCropper} from '#/lib/media/picker' import {openCropper} from '#/lib/media/picker'
import {type PickerImage} from '#/lib/media/picker.shared' import {type PickerImage} from '#/lib/media/picker.shared'
@@ -204,17 +203,15 @@ export function resetImageManipulation(
export async function compressImage( export async function compressImage(
img: ComposerImage, img: ComposerImage,
{ max: {width: number; size: number},
maxDimension = POST_IMG_MAX_HIGH_RES.width,
maxBytes = POST_IMG_MAX_HIGH_RES.size,
}: {maxDimension?: number; maxBytes?: number} = {},
): Promise<PickerImage> { ): Promise<PickerImage> {
const source = img.transformed || img.source const source = img.transformed || img.source
let attempts = 0 let attempts = 0
// Seeded from `maxDimension` but shrunk per attempt below, so keep the param // Seeded from `max.width` but shrunk per attempt below, so keep the passed-in
// itself pristine. // value pristine.
let currentDimension = maxDimension let currentDimension = max.width
const maxBytes = max.size
let minQualityPercentage = 0 let minQualityPercentage = 0
let maxQualityPercentage = 101 // exclusive let maxQualityPercentage = 101 // exclusive
@@ -3,7 +3,7 @@ import * as MediaLibrary from 'expo-media-library'
import {msg} from '@lingui/core/macro' import {msg} from '@lingui/core/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
import {POST_IMG_MAX} from '#/lib/constants' import {POST_IMG_MAX_SIZE} from '#/lib/constants'
import {useCameraPermission} from '#/lib/hooks/usePermissions' import {useCameraPermission} from '#/lib/hooks/usePermissions'
import {openCamera} from '#/lib/media/picker' import {openCamera} from '#/lib/media/picker'
import {logger} from '#/logger' import {logger} from '#/logger'
@@ -35,7 +35,7 @@ export function OpenCameraBtn({disabled, onAdd}: Props) {
} }
const img = await openCamera({ const img = await openCamera({
aspect: [POST_IMG_MAX.width, POST_IMG_MAX.height], aspect: [POST_IMG_MAX_SIZE.width, POST_IMG_MAX_SIZE.height],
}) })
// If we don't have permissions it's fine, we just wont save it. The post itself will still have access to // If we don't have permissions it's fine, we just wont save it. The post itself will still have access to
@@ -16,7 +16,7 @@ import {type PasteEventPayload, TextInputWrapper} from 'expo-paste-input'
import {AppBskyRichtextFacet, RichText} from '@atproto/api' import {AppBskyRichtextFacet, RichText} from '@atproto/api'
import {useLingui} from '@lingui/react/macro' import {useLingui} from '@lingui/react/macro'
import {POST_IMG_MAX_HIGH_RES} from '#/lib/constants' import {POST_IMG_MAX_SIZE} from '#/lib/constants'
import {downloadAndResize} from '#/lib/media/manip' import {downloadAndResize} from '#/lib/media/manip'
import {isUriImage} from '#/lib/media/util' import {isUriImage} from '#/lib/media/util'
import {getMentionAt, insertMentionAt} from '#/lib/strings/mention-manip' import {getMentionAt, insertMentionAt} from '#/lib/strings/mention-manip'
@@ -93,8 +93,8 @@ export function TextInput({
if (isUriImage(feature.uri)) { if (isUriImage(feature.uri)) {
const res = await downloadAndResize({ const res = await downloadAndResize({
uri: feature.uri, uri: feature.uri,
maxDimension: POST_IMG_MAX_HIGH_RES.width, maxDimension: POST_IMG_MAX_SIZE.width,
maxSize: POST_IMG_MAX_HIGH_RES.size, maxSize: POST_IMG_MAX_SIZE.size,
timeout: 15e3, timeout: 15e3,
}) })
+4 -1
View File
@@ -17,6 +17,7 @@ import {useLingui} from '@lingui/react'
import {Trans} from '@lingui/react/macro' import {Trans} from '@lingui/react/macro'
import {useQueryClient} from '@tanstack/react-query' import {useQueryClient} from '@tanstack/react-query'
import {PROFILE_IMAGES_MAX_SIZE} from '#/lib/constants'
import {useHaptics} from '#/lib/haptics' import {useHaptics} from '#/lib/haptics'
import { import {
useCameraPermission, useCameraPermission,
@@ -394,6 +395,7 @@ let EditableUserAvatar = ({
await openCamera({ await openCamera({
aspect: [1, 1], aspect: [1, 1],
}), }),
PROFILE_IMAGES_MAX_SIZE,
), ),
) )
}, [onSelectNewAvatar, requestCameraAccessIfNeeded]) }, [onSelectNewAvatar, requestCameraAccessIfNeeded])
@@ -422,6 +424,7 @@ let EditableUserAvatar = ({
shape: circular ? 'circle' : 'rectangle', shape: circular ? 'circle' : 'rectangle',
aspectRatio: 1, aspectRatio: 1,
}), }),
PROFILE_IMAGES_MAX_SIZE,
), ),
) )
} else { } else {
@@ -448,7 +451,7 @@ let EditableUserAvatar = ({
const onChangeEditImage = useCallback( const onChangeEditImage = useCallback(
async (image: ComposerImage) => { async (image: ComposerImage) => {
const compressed = await compressImage(image) const compressed = await compressImage(image, PROFILE_IMAGES_MAX_SIZE)
onSelectNewAvatar(compressed) onSelectNewAvatar(compressed)
}, },
[onSelectNewAvatar], [onSelectNewAvatar],
+4 -1
View File
@@ -6,6 +6,7 @@ import {msg} from '@lingui/core/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
import {Trans} from '@lingui/react/macro' import {Trans} from '@lingui/react/macro'
import {PROFILE_IMAGES_MAX_SIZE} from '#/lib/constants'
import { import {
useCameraPermission, useCameraPermission,
usePhotoLibraryPermission, usePhotoLibraryPermission,
@@ -62,6 +63,7 @@ export function UserBanner({
await openCamera({ await openCamera({
aspect: [3, 1], aspect: [3, 1],
}), }),
PROFILE_IMAGES_MAX_SIZE,
), ),
) )
}, [onSelectNewBanner, requestCameraAccessIfNeeded]) }, [onSelectNewBanner, requestCameraAccessIfNeeded])
@@ -83,6 +85,7 @@ export function UserBanner({
imageUri: items[0].path, imageUri: items[0].path,
aspectRatio: 3 / 1, aspectRatio: 3 / 1,
}), }),
PROFILE_IMAGES_MAX_SIZE,
), ),
) )
} else { } else {
@@ -108,7 +111,7 @@ export function UserBanner({
const onChangeEditImage = useCallback( const onChangeEditImage = useCallback(
async (image: ComposerImage) => { async (image: ComposerImage) => {
const compressed = await compressImage(image) const compressed = await compressImage(image, PROFILE_IMAGES_MAX_SIZE)
onSelectNewBanner?.(compressed) onSelectNewBanner?.(compressed)
}, },
[onSelectNewBanner], [onSelectNewBanner],