115 lines
2.4 KiB
TypeScript
115 lines
2.4 KiB
TypeScript
import RNFetchBlob from 'rn-fetch-blob'
|
|
import ImageResizer from '@bam.tech/react-native-image-resizer'
|
|
|
|
export interface DownloadAndResizeOpts {
|
|
uri: string
|
|
width: number
|
|
height: number
|
|
mode: 'contain' | 'cover' | 'stretch'
|
|
maxSize: number
|
|
timeout: number
|
|
}
|
|
|
|
export interface Image {
|
|
path: string
|
|
mime: string
|
|
size: number
|
|
width: number
|
|
height: number
|
|
}
|
|
|
|
export async function downloadAndResize(opts: DownloadAndResizeOpts) {
|
|
let appendExt
|
|
try {
|
|
const urip = new URL(opts.uri)
|
|
const ext = urip.pathname.split('.').pop()
|
|
if (ext === 'jpg' || ext === 'jpeg') {
|
|
appendExt = 'jpeg'
|
|
} else if (ext === 'png') {
|
|
appendExt = 'png'
|
|
} else {
|
|
return
|
|
}
|
|
} catch (e: any) {
|
|
console.error('Invalid URI', opts.uri, e)
|
|
return
|
|
}
|
|
|
|
let downloadRes
|
|
try {
|
|
const downloadResPromise = RNFetchBlob.config({
|
|
fileCache: true,
|
|
appendExt,
|
|
}).fetch('GET', opts.uri)
|
|
const to1 = setTimeout(() => downloadResPromise.cancel(), opts.timeout)
|
|
downloadRes = await downloadResPromise
|
|
clearTimeout(to1)
|
|
|
|
let localUri = downloadRes.path()
|
|
if (!localUri.startsWith('file://')) {
|
|
localUri = `file://${localUri}`
|
|
}
|
|
|
|
return await resize(localUri, opts)
|
|
} finally {
|
|
if (downloadRes) {
|
|
downloadRes.flush()
|
|
}
|
|
}
|
|
}
|
|
|
|
export interface ResizeOpts {
|
|
width: number
|
|
height: number
|
|
mode: 'contain' | 'cover' | 'stretch'
|
|
maxSize: number
|
|
}
|
|
|
|
export async function resize(
|
|
localUri: string,
|
|
opts: ResizeOpts,
|
|
): Promise<Image> {
|
|
for (let i = 0; i < 9; i++) {
|
|
const quality = 1.0 - i / 10
|
|
const resizeRes = await ImageResizer.createResizedImage(
|
|
localUri,
|
|
opts.width,
|
|
opts.height,
|
|
'JPEG',
|
|
quality,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
{mode: opts.mode},
|
|
)
|
|
if (resizeRes.size < opts.maxSize) {
|
|
return {
|
|
path: resizeRes.path,
|
|
mime: 'image/jpeg',
|
|
size: resizeRes.size,
|
|
width: resizeRes.width,
|
|
height: resizeRes.height,
|
|
}
|
|
}
|
|
}
|
|
throw new Error(
|
|
`This image is too big! We couldn't compress it down to ${opts.maxSize} bytes`,
|
|
)
|
|
}
|
|
|
|
export async function compressIfNeeded(
|
|
img: Image,
|
|
maxSize: number,
|
|
): Promise<Image> {
|
|
const origUri = `file://${img.path}`
|
|
if (img.size < maxSize) {
|
|
return img
|
|
}
|
|
return await resize(origUri, {
|
|
width: img.width,
|
|
height: img.height,
|
|
mode: 'stretch',
|
|
maxSize,
|
|
})
|
|
}
|