Skip to content

Commit

Permalink
#3408 Refactoring the FileProcessor and GPSExtractor classes - minor …
Browse files Browse the repository at this point in the history
…reformatting
  • Loading branch information
macgills committed Mar 19, 2020
1 parent 4a287d3 commit 95f2702
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions app/src/main/java/fr/free/nrw/commons/upload/FileProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ class FileProcessor @Inject constructor(
private val apiCall: CategoryApi
) {
private val compositeDisposable = CompositeDisposable()

fun cleanup() {
compositeDisposable.clear()
}

/**
* Processes filePath coordinates, either from EXIF data or user location
*/
fun processFileCoordinates(
similarImageInterface: SimilarImageInterface,
filePath: String?
): ImageCoordinates {
fun processFileCoordinates(similarImageInterface: SimilarImageInterface, filePath: String?)
: ImageCoordinates {
val exifInterface: ExifInterface? = try {
ExifInterface(filePath!!)
} catch (e: IOException) {
Expand All @@ -51,7 +50,7 @@ class FileProcessor @Inject constructor(
redactExifTags(exifInterface, getExifTagsToRedact())
Timber.d("Calling GPSExtractor")
val originalImageCoordinates = ImageCoordinates(exifInterface)
if (originalImageCoordinates.decimalCoords == null ) {
if (originalImageCoordinates.decimalCoords == null) {
//Find other photos taken around the same time which has gps coordinates
findOtherImages(
originalImageCoordinates,
Expand Down Expand Up @@ -83,10 +82,7 @@ class FileProcessor @Inject constructor(
* @param exifInterface ExifInterface object
* @param redactTags tags to be redacted
*/
private fun redactExifTags(
exifInterface: ExifInterface?,
redactTags: Set<String>
) {
private fun redactExifTags(exifInterface: ExifInterface?, redactTags: Set<String>) {
compositeDisposable.add(
Observable.fromIterable(redactTags)
.flatMap { Observable.fromArray(*FileMetadataUtils.getTagsFromPref(it)) }
Expand Down Expand Up @@ -139,7 +135,7 @@ class FileProcessor @Inject constructor(
.listFiles()
.asSequence()
.filter { it.lastModified() in timeOfCreationRange }
.map { Pair(it, readimageCoordinates(it)) }
.map { Pair(it, readImageCoordinates(it)) }
.firstOrNull { it.second?.decimalCoords != null }
?.let { fileCoordinatesPair ->
similarImageInterface.showSimilarImageFragment(
Expand All @@ -151,17 +147,18 @@ class FileProcessor @Inject constructor(
}
}

private fun readimageCoordinates(file: File) = try {
ImageCoordinates(contentResolver.openInputStream(Uri.fromFile(file)))
} catch (e: IOException) {
Timber.e(e)
private fun readImageCoordinates(file: File) =
try {
ImageCoordinates(file.absolutePath)
} catch (ex: IOException) {
Timber.e(ex)
null
ImageCoordinates(contentResolver.openInputStream(Uri.fromFile(file)))
} catch (e: IOException) {
Timber.e(e)
try {
ImageCoordinates(file.absolutePath)
} catch (ex: IOException) {
Timber.e(ex)
null
}
}
}

/**
* Initiates retrieval of image coordinates or user coordinates, and caching of coordinates. Then
Expand Down

0 comments on commit 95f2702

Please sign in to comment.