@@ -62,72 +62,58 @@ interface EditorResult {
6262 base64 ?: string
6363}
6464
65- const context = getContext ( this ) as common . ApplicationContext
66-
67- let newOptions : ImageCropData = {
68- offset : { x : 0 , y : 0 } ,
69- size : { width : 0 , height : 0 } ,
70- resizeMode : 'cover' ,
71- includeBase64 : false ,
72- }
73-
74- let resizeScaleSize : size = {
75- width : 0 ,
76- height : 0
77- }
78-
7965async function loadBase ( uri : string ) {
8066 return new Promise ( ( resolve ) => {
8167 let buf = buffer . alloc ( uri . length , uri )
8268 resolve ( buf . buffer )
8369 } )
8470}
71+
8572function loadHttp ( uri : string ) {
8673 return new Promise ( ( resolve , reject ) => {
8774 http . createHttp ( ) . request ( uri , {
8875 header : {
8976 'Content-Type' : 'application/octet-stream'
9077 }
9178 } ,
92- async ( error : BusinessError , data : http . HttpResponse ) => {
93- let code : http . ResponseCode | number = data . responseCode
94- if ( ResponseCode . ResponseCode . OK === code ) {
95- const imageData = data . result as ArrayBuffer
96- Logger . info ( "http.createHttp success" )
97- resolve ( imageData )
98- } else {
99- Logger . error ( "http.createHttp error is " + error )
100- }
101- } )
79+ async ( error : BusinessError , data : http . HttpResponse ) => {
80+ let code : http . ResponseCode | number = data . responseCode
81+ if ( ResponseCode . ResponseCode . OK === code ) {
82+ const imageData = data . result as ArrayBuffer
83+ Logger . info ( "http.createHttp success" )
84+ resolve ( imageData )
85+ } else {
86+ Logger . error ( "http.createHttp error is " + error )
87+ }
88+ } )
10289 } )
10390}
10491
105- async function getUriBuffer ( uri : string ) {
106- let imageBuffer = null
92+ async function imageEditor ( newOptions :ImageCropData , uri : string ) : Promise < EditorResult > {
93+ let imageData = null
94+ let openFile = null
10795 if ( uri . startsWith ( "data:" ) ) {
108- imageBuffer = await loadBase ( uri )
96+ imageData = await loadBase ( uri )
10997 } else if ( uri . startsWith ( 'http' ) ) {
110- imageBuffer = await loadHttp ( uri )
98+ imageData = await loadHttp ( uri )
11199 } else {
112- imageBuffer = uri
100+ openFile = fs . openSync ( uri , fs . OpenMode . READ_ONLY )
101+ imageData = openFile . fd
113102 }
114- return imageBuffer
115- }
116103
117- async function imageEditor ( imageData ) : Promise < EditorResult > {
118104 let imageSourceApi : image . ImageSource = image . createImageSource ( imageData )
119105 let getImageInfo : image . ImageInfo
120106 let options : Record < string , number | boolean > = {
121107 'editable' : true ,
122108 }
123- let editorPM = await imageSourceApi . createPixelMap ( options )
109+ let editorPM = await imageSourceApi . createPixelMap ( options )
124110
125111 editorPM . getImageInfo ( ) . then ( ( imageInfo : image . ImageInfo ) => {
126- if ( newOptions . offset . x + newOptions . size . width > imageInfo . size . width || newOptions . offset . y + newOptions . size . height > imageInfo . size . height ) {
112+ if ( newOptions . offset . x + newOptions . size . width > imageInfo . size . width || newOptions . offset . y + newOptions . size . height > imageInfo . size . height ) {
127113 Logger . error ( '[RNOH]:The cropped size exceeds the original size' )
128114 return
129115 }
130- } )
116+ } )
131117
132118 const x = newOptions . offset . x
133119 const y = newOptions . offset . y
@@ -136,24 +122,24 @@ async function imageEditor(imageData): Promise<EditorResult> {
136122 let region : image . Region = { x, y, size : { height, width} }
137123
138124 await editorPM . crop ( region ) . then ( ( ) => {
139- Logger . info ( 'imageEditor.Succeeded in crop.' ) ;
140- } ) . catch ( ( error : BusinessError ) => {
141- Logger . error ( 'imageEditor.Failed to crop.' ) ;
142- } )
125+ Logger . info ( 'imageEditor.Succeeded in crop.' )
126+ } ) . catch ( ( error : BusinessError ) => {
127+ Logger . error ( 'imageEditor.Failed to crop.' )
128+ } )
143129
144130 if ( newOptions . displaySize && newOptions . displaySize . width && newOptions . displaySize . height ) {
145-
146131 const cropSize = newOptions . size
147132 const displaySize = JSON . parse ( JSON . stringify ( newOptions . displaySize ) )
148133 const aspect = cropSize . width / cropSize . height
149134 const targetAspect = displaySize . width / displaySize . height
150135 if ( aspect === targetAspect ) newOptions . resizeMode = 'stretch'
151-
136+
152137 const xRatio = displaySize . width / cropSize . width
153138 const yRatio = displaySize . height / cropSize . height
154139 if ( newOptions . resizeMode === 'stretch' ) {
155140 await editorPM . scale ( xRatio , yRatio )
156141 } else if ( newOptions . resizeMode === 'cover' ) {
142+ let resizeScaleSize : size = displaySize
157143 if ( displaySize . width !== cropSize . width || displaySize . height !== cropSize . height ) {
158144 const ratio = Math . max ( xRatio , yRatio )
159145 await editorPM . scale ( ratio , ratio )
@@ -163,40 +149,42 @@ async function imageEditor(imageData): Promise<EditorResult> {
163149 } )
164150 }
165151
166- const targetRegion = await TargetRect ( )
152+ const targetRegion = await TargetRect ( newOptions , resizeScaleSize )
167153 await editorPM . crop ( targetRegion ) . then ( ( ) => {
168- Logger . info ( 'imageEditor.Succeeded in crop.' ) ;
154+ Logger . info ( 'imageEditor.Succeeded in crop.' )
169155 } ) . catch ( ( error : BusinessError ) => {
170- Logger . error ( 'imageEditor.Failed to crop.' ) ;
156+ Logger . error ( 'imageEditor.Failed to crop.' )
171157 } )
172-
173158 } else {
174- const { size } = await TargetRect ( )
159+ const { size } = await TargetRect ( newOptions )
175160 const xRatio = size . width / cropSize . width
176161 const yRatio = size . height / cropSize . height
177162 await editorPM . scale ( xRatio , yRatio )
178163 }
179-
180164 }
181165 await editorPM . getImageInfo ( ) . then ( ( imageInfo : image . ImageInfo ) => {
182- getImageInfo = imageInfo
183- } )
184-
166+ getImageInfo = imageInfo
167+ } )
168+ let context = getContext ( this ) as common . ApplicationContext
185169 let suffix = newOptions . format
186170 suffix = suffix === 'jpeg' || suffix === 'jpg' ? 'jpeg' : suffix
187171 const fileName = `ReactNative_cropped_image_${ new Date ( ) . getTime ( ) } .${ suffix === 'jpeg' ?'jpg' :suffix } `
188172 const path : string = `${ context . cacheDir } /${ fileName } `
189173 let packOpts : image . PackingOption = { format : `image/${ suffix } ` , quality : newOptions . quality || 90 }
190- let file = await fs . openSync ( path , fs . OpenMode . CREATE | fs . OpenMode . READ_WRITE ) ;
191174 let size = 0
192175 let base64Data = ''
193176
194177 const imagePackerApi :image . ImagePacker = image . createImagePacker ( )
195178 await imagePackerApi . packing ( editorPM , packOpts )
196179 . then ( async ( data : ArrayBuffer ) => {
197- let writeLen = await fs . writeSync ( file . fd , data )
198- size = writeLen
199- fs . closeSync ( file )
180+ let file = fs . openSync ( path , fs . OpenMode . CREATE | fs . OpenMode . READ_WRITE )
181+ try {
182+ size = fs . writeSync ( file . fd , data )
183+ } catch ( err ) {
184+ Logger . error ( 'file write failed' )
185+ } finally {
186+ fs . closeSync ( file )
187+ }
200188
201189 if ( newOptions . includeBase64 ) {
202190 let unit8Array : Uint8Array = new Uint8Array ( data )
@@ -205,14 +193,15 @@ async function imageEditor(imageData): Promise<EditorResult> {
205193 }
206194
207195 } ) . catch ( ( error : BusinessError ) => {
208- Logger . error ( 'packing failed.' ) ;
196+ Logger . error ( 'packing failed.' )
197+ } ) . finally ( ( ) => {
198+ openFile && fs . closeSync ( openFile )
209199 } )
210200
211201 editorPM . release ( )
212202 imageSourceApi . release ( )
213203 imagePackerApi . release ( )
214-
215- size = await fs . statSync ( path ) . size ;
204+ context = null
216205
217206 const result :EditorResult = {
218207 uri : `file://${ path } ` ,
@@ -226,10 +215,10 @@ async function imageEditor(imageData): Promise<EditorResult> {
226215 if ( newOptions . includeBase64 ) {
227216 result . base64 = base64Data
228217 }
229- return result ;
218+ return result
230219}
231220
232- async function TargetRect ( ) {
221+ async function TargetRect ( newOptions : ImageCropData , resizeScaleSize ?: size ) {
233222 const resizeMode = newOptions . resizeMode
234223 const cropSize = newOptions . size
235224 const displaySize = JSON . parse ( JSON . stringify ( newOptions . displaySize ) )
@@ -255,17 +244,18 @@ async function TargetRect() {
255244 targetSize . height = displaySize . height
256245 targetSize . width = Math . ceil ( targetSize . height * aspect )
257246 }
247+ targetRegion . size = targetSize
258248 } else if ( resizeMode === 'center' ) {
259249 if ( cropSize . height > displaySize . height ) {
260250 targetSize . width = displaySize . width
261251 targetSize . height = Math . ceil ( targetSize . width / aspect )
262- }
252+ }
263253 if ( cropSize . width > displaySize . width ) {
264254 targetSize . height = displaySize . height
265255 targetSize . width = Math . ceil ( targetSize . height * aspect )
266256 }
257+ targetRegion . size = targetSize
267258 }
268- targetRegion . size = targetSize
269259 return targetRegion
270260}
271261
@@ -278,18 +268,18 @@ export class ImageEditorModule extends TurboModule {
278268 quality = Math . floor ( options . quality * 100 )
279269 }
280270 if ( ! uri ) {
281- Logger . warn ( '[RNOH]:Please specify a URI' ) ;
271+ Logger . warn ( '[RNOH]:Please specify a URI' )
282272 return
283273 }
284274 if ( ! offset || ! size || ! ( 'x' in offset ) || ! ( 'y' in offset ) || ! size . width || ! size . height ) {
285- Logger . warn ( '[RNOH]:Please specify offset and size' ) ;
275+ Logger . warn ( '[RNOH]:Please specify offset and size' )
286276 return
287277 }
288278 if ( quality > 100 || quality < 0 ) {
289- Logger . warn ( '[RNOH]:quality must be a number between 0 and 1' ) ;
279+ Logger . warn ( '[RNOH]:quality must be a number between 0 and 1' )
290280 return
291281 }
292-
282+ let newOptions : ImageCropData = options
293283 newOptions . size = size
294284 newOptions . offset = offset
295285 newOptions . quality = quality
@@ -303,10 +293,7 @@ export class ImageEditorModule extends TurboModule {
303293 }
304294 if ( options . includeBase64 ) newOptions . includeBase64 = options . includeBase64
305295
306- const buffer = await getUriBuffer ( uri )
307- const fileUri : EditorResult = await imageEditor ( buffer )
308-
296+ const fileUri : EditorResult = await imageEditor ( newOptions , uri )
309297 return fileUri . uri
310-
311298 }
312- }
299+ }
0 commit comments