Clarify props and API contracts
This commit is contained in:
@@ -91,7 +91,7 @@ describe('downloadAndResize', () => {
|
|||||||
}
|
}
|
||||||
const resizedDimensionsOne = getResizedDimensions(
|
const resizedDimensionsOne = getResizedDimensions(
|
||||||
initialDimensionsOne,
|
initialDimensionsOne,
|
||||||
IMAGE_SIZE_CONFIG_2K_1MB,
|
IMAGE_SIZE_CONFIG_2K_1MB.maxDimension,
|
||||||
)
|
)
|
||||||
|
|
||||||
const initialDimensionsTwo = {
|
const initialDimensionsTwo = {
|
||||||
@@ -100,7 +100,7 @@ describe('downloadAndResize', () => {
|
|||||||
}
|
}
|
||||||
const resizedDimensionsTwo = getResizedDimensions(
|
const resizedDimensionsTwo = getResizedDimensions(
|
||||||
initialDimensionsTwo,
|
initialDimensionsTwo,
|
||||||
IMAGE_SIZE_CONFIG_2K_1MB,
|
IMAGE_SIZE_CONFIG_2K_1MB.maxDimension,
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(resizedDimensionsOne).toEqual(initialDimensionsOne)
|
expect(resizedDimensionsOne).toEqual(initialDimensionsOne)
|
||||||
@@ -114,7 +114,7 @@ describe('downloadAndResize', () => {
|
|||||||
}
|
}
|
||||||
const resizedDimensionsOne = getResizedDimensions(
|
const resizedDimensionsOne = getResizedDimensions(
|
||||||
initialDimensionsOne,
|
initialDimensionsOne,
|
||||||
IMAGE_SIZE_CONFIG_2K_1MB,
|
IMAGE_SIZE_CONFIG_2K_1MB.maxDimension,
|
||||||
)
|
)
|
||||||
|
|
||||||
const initialDimensionsTwo = {
|
const initialDimensionsTwo = {
|
||||||
@@ -123,7 +123,7 @@ describe('downloadAndResize', () => {
|
|||||||
}
|
}
|
||||||
const resizedDimensionsTwo = getResizedDimensions(
|
const resizedDimensionsTwo = getResizedDimensions(
|
||||||
initialDimensionsTwo,
|
initialDimensionsTwo,
|
||||||
IMAGE_SIZE_CONFIG_2K_1MB,
|
IMAGE_SIZE_CONFIG_2K_1MB.maxDimension,
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(resizedDimensionsOne).toEqual({
|
expect(resizedDimensionsOne).toEqual({
|
||||||
|
|||||||
@@ -257,8 +257,7 @@ export async function imageToThumb(
|
|||||||
try {
|
try {
|
||||||
const img = await downloadAndResize({
|
const img = await downloadAndResize({
|
||||||
uri: imageUri,
|
uri: imageUri,
|
||||||
maxDimension: IMAGE_SIZE_CONFIG_2K_1MB.width,
|
...IMAGE_SIZE_CONFIG_2K_1MB,
|
||||||
maxSize: IMAGE_SIZE_CONFIG_2K_1MB.size,
|
|
||||||
timeout: 15e3,
|
timeout: 15e3,
|
||||||
})
|
})
|
||||||
if (img) {
|
if (img) {
|
||||||
|
|||||||
@@ -97,15 +97,13 @@ export const STAGING_FEEDS = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
export const IMAGE_SIZE_CONFIG_POSTS = {
|
export const IMAGE_SIZE_CONFIG_POSTS = {
|
||||||
width: 4000,
|
maxDimension: 4000,
|
||||||
height: 4000,
|
maxSize: 2000000,
|
||||||
size: 2000000,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const IMAGE_SIZE_CONFIG_2K_1MB = {
|
export const IMAGE_SIZE_CONFIG_2K_1MB = {
|
||||||
width: 2000,
|
maxDimension: 2000,
|
||||||
height: 2000,
|
maxSize: 1000000,
|
||||||
size: 1000000,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const STAGING_LINK_META_PROXY =
|
export const STAGING_LINK_META_PROXY =
|
||||||
|
|||||||
@@ -24,14 +24,14 @@ import {convertCdnPreset, getResizedDimensions} from './util'
|
|||||||
|
|
||||||
export async function compressIfNeeded(
|
export async function compressIfNeeded(
|
||||||
img: PickerImage,
|
img: PickerImage,
|
||||||
max: {width: number; size: number},
|
{maxDimension, maxSize}: {maxDimension: number; maxSize: number},
|
||||||
): Promise<PickerImage> {
|
): Promise<PickerImage> {
|
||||||
if (img.size < max.size) {
|
if (img.size < maxSize) {
|
||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
const resizedImage = await doResize(normalizePath(img.path), {
|
const resizedImage = await doResize(normalizePath(img.path), {
|
||||||
maxDimension: max.width,
|
maxDimension,
|
||||||
maxSize: max.size,
|
maxSize,
|
||||||
})
|
})
|
||||||
const finalImageMovedPath = await moveToPermanentPath(
|
const finalImageMovedPath = await moveToPermanentPath(
|
||||||
resizedImage.path,
|
resizedImage.path,
|
||||||
@@ -205,7 +205,7 @@ async function doResize(
|
|||||||
width: imageRes.width,
|
width: imageRes.width,
|
||||||
height: imageRes.height,
|
height: imageRes.height,
|
||||||
},
|
},
|
||||||
{width: opts.maxDimension, height: opts.maxDimension},
|
opts.maxDimension,
|
||||||
)
|
)
|
||||||
|
|
||||||
let minQualityPercentage = 0
|
let minQualityPercentage = 0
|
||||||
|
|||||||
@@ -9,14 +9,14 @@ import {
|
|||||||
|
|
||||||
export async function compressIfNeeded(
|
export async function compressIfNeeded(
|
||||||
img: PickerImage,
|
img: PickerImage,
|
||||||
max: {width: number; size: number},
|
{maxDimension, maxSize}: {maxDimension: number; maxSize: number},
|
||||||
): Promise<PickerImage> {
|
): Promise<PickerImage> {
|
||||||
if (img.size < max.size) {
|
if (img.size < maxSize) {
|
||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
return await doResize(img.path, {
|
return await doResize(img.path, {
|
||||||
maxDimension: max.width,
|
maxDimension,
|
||||||
maxSize: max.size,
|
maxSize,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,10 +83,7 @@ async function doResize(
|
|||||||
opts: DoResizeOpts,
|
opts: DoResizeOpts,
|
||||||
): Promise<PickerImage> {
|
): Promise<PickerImage> {
|
||||||
const sourceDims = await getImageDim(dataUri)
|
const sourceDims = await getImageDim(dataUri)
|
||||||
const newDimensions = getResizedDimensions(sourceDims, {
|
const newDimensions = getResizedDimensions(sourceDims, opts.maxDimension)
|
||||||
width: opts.maxDimension,
|
|
||||||
height: opts.maxDimension,
|
|
||||||
})
|
|
||||||
|
|
||||||
let newDataUri
|
let newDataUri
|
||||||
|
|
||||||
|
|||||||
@@ -7,15 +7,18 @@ export function getResizedDimensions(
|
|||||||
width: number
|
width: number
|
||||||
height: number
|
height: number
|
||||||
},
|
},
|
||||||
max: {width: number; height: number},
|
maxDimension: number,
|
||||||
) {
|
) {
|
||||||
if (originalDims.width <= max.width && originalDims.height <= max.height) {
|
if (
|
||||||
|
originalDims.width <= maxDimension &&
|
||||||
|
originalDims.height <= maxDimension
|
||||||
|
) {
|
||||||
return originalDims
|
return originalDims
|
||||||
}
|
}
|
||||||
|
|
||||||
const ratio = Math.min(
|
const ratio = Math.min(
|
||||||
max.width / originalDims.width,
|
maxDimension / originalDims.width,
|
||||||
max.height / originalDims.height,
|
maxDimension / originalDims.height,
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -203,15 +203,15 @@ export function resetImageManipulation(
|
|||||||
|
|
||||||
export async function compressImage(
|
export async function compressImage(
|
||||||
img: ComposerImage,
|
img: ComposerImage,
|
||||||
max: {width: number; size: number},
|
{maxDimension, maxSize}: {maxDimension: number; maxSize: 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 `max.width` but shrunk per attempt below, so keep the passed-in
|
// Seeded from `maxDimension` but shrunk per attempt below, so keep the
|
||||||
// value pristine.
|
// passed-in value pristine.
|
||||||
let currentDimension = max.width
|
let currentDimension = maxDimension
|
||||||
const maxBytes = max.size
|
const maxBytes = maxSize
|
||||||
|
|
||||||
let minQualityPercentage = 0
|
let minQualityPercentage = 0
|
||||||
let maxQualityPercentage = 101 // exclusive
|
let maxQualityPercentage = 101 // exclusive
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ 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 {IMAGE_SIZE_CONFIG_POSTS} 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 +34,7 @@ export function OpenCameraBtn({disabled, onAdd}: Props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const img = await openCamera({
|
const img = await openCamera({
|
||||||
aspect: [IMAGE_SIZE_CONFIG_POSTS.width, IMAGE_SIZE_CONFIG_POSTS.height],
|
aspect: [1, 1],
|
||||||
})
|
})
|
||||||
|
|
||||||
// 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
|
||||||
|
|||||||
@@ -93,8 +93,7 @@ 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: IMAGE_SIZE_CONFIG_POSTS.width,
|
...IMAGE_SIZE_CONFIG_POSTS,
|
||||||
maxSize: IMAGE_SIZE_CONFIG_POSTS.size,
|
|
||||||
timeout: 15e3,
|
timeout: 15e3,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user