Skip to content

Commit 9d3a1fc

Browse files
authored
fix(android): support HTTP URLs in read (#20)
* chore: lint and format fixes * fix(android): support HTTP URLs in read (#5) * feat(example): add URL input for testing remote exif read * fix: reject with errors instead of resolving null
1 parent 6a87e7c commit 9d3a1fc

13 files changed

Lines changed: 618 additions & 255 deletions

File tree

.eslintignore

Lines changed: 0 additions & 7 deletions
This file was deleted.

android/src/main/java/com/lodev09/exify/ExifyModule.kt

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,65 @@ package com.lodev09.exify
22

33
import android.net.Uri
44
import androidx.exifinterface.media.ExifInterface
5-
import com.lodev09.exify.ExifyUtils.formatTags
65
import com.facebook.react.bridge.Arguments
76
import com.facebook.react.bridge.Promise
87
import com.facebook.react.bridge.ReactApplicationContext
98
import com.facebook.react.bridge.ReadableMap
109
import com.facebook.react.bridge.ReadableType
10+
import com.facebook.react.util.RNLog
11+
import com.lodev09.exify.ExifyUtils.formatTags
1112
import java.io.IOException
1213

1314
private 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

android/src/main/java/com/lodev09/exify/ExifyPackage.kt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,28 @@ import com.facebook.react.module.model.ReactModuleInfoProvider
88
import java.util.HashMap
99

1010
class ExifyPackage : BaseReactPackage() {
11-
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
12-
return if (name == ExifyModule.NAME) {
11+
override fun getModule(
12+
name: String,
13+
reactContext: ReactApplicationContext,
14+
): NativeModule? =
15+
if (name == ExifyModule.NAME) {
1316
ExifyModule(reactContext)
1417
} else {
1518
null
1619
}
17-
}
1820

19-
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
20-
return ReactModuleInfoProvider {
21+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider =
22+
ReactModuleInfoProvider {
2123
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
22-
moduleInfos[ExifyModule.NAME] = ReactModuleInfo(
23-
ExifyModule.NAME,
24-
ExifyModule.NAME,
25-
false, // canOverrideExistingModule
26-
false, // needsEagerInit
27-
false, // isCxxModule
28-
true // isTurboModule
29-
)
24+
moduleInfos[ExifyModule.NAME] =
25+
ReactModuleInfo(
26+
ExifyModule.NAME,
27+
ExifyModule.NAME,
28+
false, // canOverrideExistingModule
29+
false, // needsEagerInit
30+
false, // isCxxModule
31+
true, // isTurboModule
32+
)
3033
moduleInfos
3134
}
32-
}
3335
}

0 commit comments

Comments
 (0)