mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-11-02 15:53:55 +01:00
Convert ReadFBMD to kotlin
This commit is contained in:
parent
a8740c4df8
commit
79b51944a0
2 changed files with 45 additions and 48 deletions
|
|
@ -1,48 +0,0 @@
|
|||
package fr.free.nrw.commons.upload;
|
||||
|
||||
import fr.free.nrw.commons.utils.ImageUtils;
|
||||
import io.reactivex.Single;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
/**
|
||||
* We want to discourage users from uploading images to Commons that were taken from Facebook. This
|
||||
* attempts to detect whether an image was downloaded from Facebook by heuristically searching for
|
||||
* metadata that is specific to images that come from Facebook.
|
||||
*/
|
||||
@Singleton
|
||||
public class ReadFBMD {
|
||||
|
||||
@Inject
|
||||
public ReadFBMD() {
|
||||
}
|
||||
|
||||
public Single<Integer> processMetadata(String path) {
|
||||
return Single.fromCallable(() -> {
|
||||
try {
|
||||
int psBlockOffset;
|
||||
int fbmdOffset;
|
||||
|
||||
try (FileInputStream fs = new FileInputStream(path)) {
|
||||
byte[] bytes = new byte[4096];
|
||||
fs.read(bytes);
|
||||
fs.close();
|
||||
String fileStr = new String(bytes);
|
||||
psBlockOffset = fileStr.indexOf("8BIM");
|
||||
fbmdOffset = fileStr.indexOf("FBMD");
|
||||
}
|
||||
|
||||
if (psBlockOffset > 0 && fbmdOffset > 0
|
||||
&& fbmdOffset > psBlockOffset && fbmdOffset - psBlockOffset < 0x80) {
|
||||
return ImageUtils.FILE_FBMD;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ImageUtils.IMAGE_OK;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
45
app/src/main/java/fr/free/nrw/commons/upload/ReadFBMD.kt
Normal file
45
app/src/main/java/fr/free/nrw/commons/upload/ReadFBMD.kt
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package fr.free.nrw.commons.upload
|
||||
|
||||
import fr.free.nrw.commons.utils.ImageUtils.FILE_FBMD
|
||||
import fr.free.nrw.commons.utils.ImageUtils.IMAGE_OK
|
||||
import io.reactivex.Single
|
||||
import timber.log.Timber
|
||||
import java.io.FileInputStream
|
||||
import java.io.IOException
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* We want to discourage users from uploading images to Commons that were taken from Facebook. This
|
||||
* attempts to detect whether an image was downloaded from Facebook by heuristically searching for
|
||||
* metadata that is specific to images that come from Facebook.
|
||||
*/
|
||||
@Singleton
|
||||
class ReadFBMD @Inject constructor() {
|
||||
fun processMetadata(path: String?): Single<Int> = Single.fromCallable {
|
||||
var result = IMAGE_OK
|
||||
try {
|
||||
var psBlockOffset: Int
|
||||
var fbmdOffset: Int
|
||||
|
||||
FileInputStream(path).use { fs ->
|
||||
val bytes = ByteArray(4096)
|
||||
fs.read(bytes)
|
||||
with(String(bytes)) {
|
||||
psBlockOffset = indexOf("8BIM")
|
||||
fbmdOffset = indexOf("FBMD")
|
||||
}
|
||||
}
|
||||
|
||||
result = if (psBlockOffset > 0 && fbmdOffset > 0 &&
|
||||
fbmdOffset > psBlockOffset &&
|
||||
fbmdOffset - psBlockOffset < 0x80
|
||||
) FILE_FBMD else IMAGE_OK
|
||||
|
||||
} catch (e: IOException) {
|
||||
Timber.e(e)
|
||||
}
|
||||
return@fromCallable result
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue