mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-11-04 00:33:55 +01:00 
			
		
		
		
	Convert EXIFReader to kotlin
This commit is contained in:
		
							parent
							
								
									7ff3e5f62c
								
							
						
					
					
						commit
						aa937ee6a1
					
				
					 2 changed files with 31 additions and 36 deletions
				
			
		| 
						 | 
					@ -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);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
							
								
								
									
										31
									
								
								app/src/main/java/fr/free/nrw/commons/upload/EXIFReader.kt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								app/src/main/java/fr/free/nrw/commons/upload/EXIFReader.kt
									
										
									
									
									
										Normal 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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue