@@ -2,37 +2,65 @@ package com.lodev09.exify
22
33import android.net.Uri
44import androidx.exifinterface.media.ExifInterface
5- import com.lodev09.exify.ExifyUtils.formatTags
65import com.facebook.react.bridge.Arguments
76import com.facebook.react.bridge.Promise
87import com.facebook.react.bridge.ReactApplicationContext
98import com.facebook.react.bridge.ReadableMap
109import com.facebook.react.bridge.ReadableType
10+ import com.facebook.react.util.RNLog
11+ import com.lodev09.exify.ExifyUtils.formatTags
1112import java.io.IOException
1213
1314private const val ERROR_TAG = " E_EXIFY_ERROR"
1415
15- class ExifyModule (reactContext : ReactApplicationContext ) :
16- NativeExifySpec ( reactContext) {
17-
16+ class ExifyModule (
17+ reactContext : ReactApplicationContext ,
18+ ) : NativeExifySpec(reactContext) {
1819 private val context = reactContext
1920
20- override fun read (uri : String , promise : Promise ) {
21+ override fun read (
22+ uri : String ,
23+ promise : Promise ,
24+ ) {
2125 val photoUri = Uri .parse(uri)
26+ val scheme = photoUri.scheme
27+
28+ if (scheme == null ) {
29+ RNLog .w(context, " Exify: Invalid URI: $uri " )
30+ promise.reject(ERROR_TAG , " Invalid URI: $uri " )
31+ return
32+ }
2233
2334 try {
24- context.contentResolver.openInputStream(photoUri)?.use {
35+ val inputStream =
36+ if (scheme == " http" || scheme == " https" ) {
37+ java.net.URL (uri).openStream()
38+ } else {
39+ context.contentResolver.openInputStream(photoUri)
40+ }
41+
42+ if (inputStream == null ) {
43+ RNLog .w(context, " Exify: Could not open URI: $uri " )
44+ promise.reject(ERROR_TAG , " Could not open URI: $uri " )
45+ return
46+ }
47+
48+ inputStream.use {
2549 val tags = formatTags(ExifInterface (it))
2650 promise.resolve(tags)
2751 }
2852 } catch (e: Exception ) {
29- promise.resolve( null )
30- e.printStackTrace( )
53+ RNLog .w(context, " Exify: ${e.message} " )
54+ promise.reject( ERROR_TAG , e.message, e )
3155 }
3256 }
3357
3458 @Throws(IOException ::class )
35- override fun write (uri : String , tags : ReadableMap , promise : Promise ) {
59+ override fun write (
60+ uri : String ,
61+ tags : ReadableMap ,
62+ promise : Promise ,
63+ ) {
3664 val photoUri = Uri .parse(uri)
3765 val params = Arguments .createMap()
3866
@@ -46,15 +74,28 @@ class ExifyModule(reactContext: ReactApplicationContext) :
4674 val type = tags.getType(tag)
4775
4876 when (type) {
49- ReadableType .Boolean -> exif.setAttribute(tag, tags.getBoolean(tag).toString())
50- ReadableType .Number ->
77+ ReadableType .Boolean -> {
78+ exif.setAttribute(tag, tags.getBoolean(tag).toString())
79+ }
80+
81+ ReadableType .Number -> {
5182 when (valType) {
5283 " double" -> exif.setAttribute(tag, tags.getDouble(tag).toBigDecimal().toPlainString())
5384 else -> exif.setAttribute(tag, tags.getDouble(tag).toInt().toString())
5485 }
55- ReadableType .String -> exif.setAttribute(tag, tags.getString(tag))
56- ReadableType .Array -> exif.setAttribute(tag, tags.getArray(tag).toString())
57- else -> exif.setAttribute(tag, tags.getString(tag))
86+ }
87+
88+ ReadableType .String -> {
89+ exif.setAttribute(tag, tags.getString(tag))
90+ }
91+
92+ ReadableType .Array -> {
93+ exif.setAttribute(tag, tags.getArray(tag).toString())
94+ }
95+
96+ else -> {
97+ exif.setAttribute(tag, tags.getString(tag))
98+ }
5899 }
59100 }
60101
@@ -64,7 +105,7 @@ class ExifyModule(reactContext: ReactApplicationContext) :
64105 ) {
65106 exif.setLatLong(
66107 tags.getDouble(ExifInterface .TAG_GPS_LATITUDE ),
67- tags.getDouble(ExifInterface .TAG_GPS_LONGITUDE )
108+ tags.getDouble(ExifInterface .TAG_GPS_LONGITUDE ),
68109 )
69110 }
70111
0 commit comments