Convert EXIFReader to kotlin

This commit is contained in:
Paul Hawke 2024-12-09 13:51:48 -06:00
parent 7ff3e5f62c
commit aa937ee6a1
2 changed files with 31 additions and 36 deletions

View file

@ -1,36 +0,0 @@
package fr.free.nrw.commons.upload;
import androidx.exifinterface.media.ExifInterface;
import javax.inject.Inject;
import javax.inject.Singleton;
import fr.free.nrw.commons.utils.ImageUtils;
import io.reactivex.Single;
/**
* We try to minimize uploads from the Commons app that might be copyright violations.
* If an image does not have any Exif metadata, then it was likely downloaded from the internet,
* and is probably not an original work by the user. We detect these kinds of images by looking
* for the presence of some basic Exif metadata.
*/
@Singleton
public class EXIFReader {
@Inject
public EXIFReader() {
}
public Single<Integer> processMetadata(String path) {
try {
ExifInterface exif = new ExifInterface(path);
if (exif.getAttribute(ExifInterface.TAG_MAKE) != null
|| exif.getAttribute(ExifInterface.TAG_DATETIME) != null) {
return Single.just(ImageUtils.IMAGE_OK);
}
} catch (Exception e) {
return Single.just(ImageUtils.FILE_NO_EXIF);
}
return Single.just(ImageUtils.FILE_NO_EXIF);
}
}

View file

@ -0,0 +1,31 @@
package fr.free.nrw.commons.upload
import androidx.exifinterface.media.ExifInterface
import androidx.exifinterface.media.ExifInterface.TAG_DATETIME
import androidx.exifinterface.media.ExifInterface.TAG_MAKE
import fr.free.nrw.commons.utils.ImageUtils.FILE_NO_EXIF
import fr.free.nrw.commons.utils.ImageUtils.IMAGE_OK
import io.reactivex.Single
import javax.inject.Inject
import javax.inject.Singleton
/**
* We try to minimize uploads from the Commons app that might be copyright violations.
* If an image does not have any Exif metadata, then it was likely downloaded from the internet,
* and is probably not an original work by the user. We detect these kinds of images by looking
* for the presence of some basic Exif metadata.
*/
@Singleton
class EXIFReader @Inject constructor() {
fun processMetadata(path: String): Single<Int> = Single.just(
try {
if (ExifInterface(path).hasMakeOrDate) IMAGE_OK else FILE_NO_EXIF
} catch (e: Exception) {
FILE_NO_EXIF
}
)
private val ExifInterface.hasMakeOrDate get() =
getAttribute(TAG_MAKE) != null || getAttribute(TAG_DATETIME) != null
}