1- import { sha256 } from '@noble/hashes/sha2' ;
21import { v2 , type PolycentricClient } from '@polycentric/react-native' ;
32import { File } from 'expo-file-system' ;
43import { isWeb } from '@/src/common/util/platform' ;
@@ -11,16 +10,12 @@ export type ProcessAndUploadOptions = {
1110 sizes ?: number [ ] ;
1211 /** `fill` crops to a square (default, for avatars). `fit` preserves aspect. */
1312 mode ?: 'fill' | 'fit' ;
14- /** Source image width. Required for `mode: 'fit'` so variants record correct dims. */
15- sourceWidth ?: number ;
16- /** Source image height. Required for `mode: 'fit'` so variants record correct dims. */
17- sourceHeight ?: number ;
1813} ;
1914
2015/**
2116 * Fetch an image from `uri`, resize it into each size in `sizes` via
22- * the core's JPEG encoder, upload each variant's bytes to the client's
23- * servers, and return the assembled `ImageSet`.
17+ * the core's JPEG encoder, commit each variant locally and upload to
18+ * the client's servers, and return the assembled `ImageSet`.
2419 */
2520export async function processAndUploadImage (
2621 client : PolycentricClient ,
@@ -41,27 +36,14 @@ export async function processAndUploadImage(
4136
4237 const variants = await Promise . all (
4338 sizes . map ( async ( size ) => {
44- const jpeg = client . processImageToJpeg ( raw , size , size , mode ) ;
45- const { width , height } = computeOutputDims (
39+ const { bytes , width , height } = client . processImageToJpeg (
40+ raw ,
4641 size ,
4742 size ,
4843 mode ,
49- options . sourceWidth ,
50- options . sourceHeight ,
5144 ) ;
52- const image = v2 . Image . create ( {
53- blob : {
54- digest : {
55- type : v2 . ContentDigestType . SHA256 ,
56- value : sha256 ( jpeg ) ,
57- } ,
58- mimeType : 'image/jpeg' ,
59- size : BigInt ( jpeg . length ) ,
60- } ,
61- width,
62- height,
63- } ) ;
64- return { image, body : jpeg } ;
45+ const blob = await client . commitBlob ( bytes , 'image/jpeg' ) ;
46+ return { image : v2 . Image . create ( { blob, width, height } ) , body : bytes } ;
6547 } ) ,
6648 ) ;
6749
@@ -75,27 +57,3 @@ export async function processAndUploadImage(
7557
7658 return v2 . ImageSet . create ( { images : variants . map ( ( v ) => v . image ) } ) ;
7759}
78-
79- /**
80- * Match the `image` crate's `resize()`: scale both axes by
81- * `min(targetW/srcW, targetH/srcH)` and round. For fill mode the
82- * output is always exactly the requested bounds. For fit mode without
83- * known source dims, we fall back to the bounds (the client can re-
84- * derive aspect from the served image if needed).
85- */
86- function computeOutputDims (
87- targetW : number ,
88- targetH : number ,
89- mode : 'fill' | 'fit' ,
90- srcW ?: number ,
91- srcH ?: number ,
92- ) : { width : number ; height : number } {
93- if ( mode === 'fill' || ! srcW || ! srcH ) {
94- return { width : targetW , height : targetH } ;
95- }
96- const ratio = Math . min ( targetW / srcW , targetH / srcH ) ;
97- return {
98- width : Math . max ( 1 , Math . round ( srcW * ratio ) ) ,
99- height : Math . max ( 1 , Math . round ( srcH * ratio ) ) ,
100- } ;
101- }
0 commit comments