Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .eslintignore

This file was deleted.

71 changes: 56 additions & 15 deletions android/src/main/java/com/lodev09/exify/ExifyModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,65 @@ package com.lodev09.exify

import android.net.Uri
import androidx.exifinterface.media.ExifInterface
import com.lodev09.exify.ExifyUtils.formatTags
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.ReadableType
import com.facebook.react.util.RNLog
import com.lodev09.exify.ExifyUtils.formatTags
import java.io.IOException

private const val ERROR_TAG = "E_EXIFY_ERROR"

class ExifyModule(reactContext: ReactApplicationContext) :
NativeExifySpec(reactContext) {

class ExifyModule(
reactContext: ReactApplicationContext,
) : NativeExifySpec(reactContext) {
private val context = reactContext

override fun read(uri: String, promise: Promise) {
override fun read(
uri: String,
promise: Promise,
) {
val photoUri = Uri.parse(uri)
val scheme = photoUri.scheme

if (scheme == null) {
RNLog.w(context, "Exify: Invalid URI: $uri")
promise.reject(ERROR_TAG, "Invalid URI: $uri")
return
}

try {
context.contentResolver.openInputStream(photoUri)?.use {
val inputStream =
if (scheme == "http" || scheme == "https") {
java.net.URL(uri).openStream()
} else {
context.contentResolver.openInputStream(photoUri)
}

if (inputStream == null) {
RNLog.w(context, "Exify: Could not open URI: $uri")
promise.reject(ERROR_TAG, "Could not open URI: $uri")
return
}

inputStream.use {
val tags = formatTags(ExifInterface(it))
promise.resolve(tags)
}
} catch (e: Exception) {
promise.resolve(null)
e.printStackTrace()
RNLog.w(context, "Exify: ${e.message}")
promise.reject(ERROR_TAG, e.message, e)
}
}

@Throws(IOException::class)
override fun write(uri: String, tags: ReadableMap, promise: Promise) {
override fun write(
uri: String,
tags: ReadableMap,
promise: Promise,
) {
val photoUri = Uri.parse(uri)
val params = Arguments.createMap()

Expand All @@ -46,15 +74,28 @@ class ExifyModule(reactContext: ReactApplicationContext) :
val type = tags.getType(tag)

when (type) {
ReadableType.Boolean -> exif.setAttribute(tag, tags.getBoolean(tag).toString())
ReadableType.Number ->
ReadableType.Boolean -> {
exif.setAttribute(tag, tags.getBoolean(tag).toString())
}

ReadableType.Number -> {
when (valType) {
"double" -> exif.setAttribute(tag, tags.getDouble(tag).toBigDecimal().toPlainString())
else -> exif.setAttribute(tag, tags.getDouble(tag).toInt().toString())
}
ReadableType.String -> exif.setAttribute(tag, tags.getString(tag))
ReadableType.Array -> exif.setAttribute(tag, tags.getArray(tag).toString())
else -> exif.setAttribute(tag, tags.getString(tag))
}

ReadableType.String -> {
exif.setAttribute(tag, tags.getString(tag))
}

ReadableType.Array -> {
exif.setAttribute(tag, tags.getArray(tag).toString())
}

else -> {
exif.setAttribute(tag, tags.getString(tag))
}
}
}

Expand All @@ -64,7 +105,7 @@ class ExifyModule(reactContext: ReactApplicationContext) :
) {
exif.setLatLong(
tags.getDouble(ExifInterface.TAG_GPS_LATITUDE),
tags.getDouble(ExifInterface.TAG_GPS_LONGITUDE)
tags.getDouble(ExifInterface.TAG_GPS_LONGITUDE),
)
}

Expand Down
30 changes: 16 additions & 14 deletions android/src/main/java/com/lodev09/exify/ExifyPackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@ import com.facebook.react.module.model.ReactModuleInfoProvider
import java.util.HashMap

class ExifyPackage : BaseReactPackage() {
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
return if (name == ExifyModule.NAME) {
override fun getModule(
name: String,
reactContext: ReactApplicationContext,
): NativeModule? =
if (name == ExifyModule.NAME) {
ExifyModule(reactContext)
} else {
null
}
}

override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
return ReactModuleInfoProvider {
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider =
ReactModuleInfoProvider {
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
moduleInfos[ExifyModule.NAME] = ReactModuleInfo(
ExifyModule.NAME,
ExifyModule.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
false, // isCxxModule
true // isTurboModule
)
moduleInfos[ExifyModule.NAME] =
ReactModuleInfo(
ExifyModule.NAME,
ExifyModule.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
false, // isCxxModule
true, // isTurboModule
)
moduleInfos
}
}
}
Loading
Loading