From aa937ee6a1100a85f03e77123ae77dab2123b47b Mon Sep 17 00:00:00 2001 From: Paul Hawke Date: Mon, 9 Dec 2024 13:51:48 -0600 Subject: [PATCH] Convert EXIFReader to kotlin --- .../free/nrw/commons/upload/EXIFReader.java | 36 ------------------- .../fr/free/nrw/commons/upload/EXIFReader.kt | 31 ++++++++++++++++ 2 files changed, 31 insertions(+), 36 deletions(-) delete mode 100644 app/src/main/java/fr/free/nrw/commons/upload/EXIFReader.java create mode 100644 app/src/main/java/fr/free/nrw/commons/upload/EXIFReader.kt diff --git a/app/src/main/java/fr/free/nrw/commons/upload/EXIFReader.java b/app/src/main/java/fr/free/nrw/commons/upload/EXIFReader.java deleted file mode 100644 index 0dd13acea..000000000 --- a/app/src/main/java/fr/free/nrw/commons/upload/EXIFReader.java +++ /dev/null @@ -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 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); - } -} - diff --git a/app/src/main/java/fr/free/nrw/commons/upload/EXIFReader.kt b/app/src/main/java/fr/free/nrw/commons/upload/EXIFReader.kt new file mode 100644 index 000000000..f97052065 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/upload/EXIFReader.kt @@ -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 = 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 +} +