mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-11-03 16:23:54 +01:00
Convert FileUtils to kotlin
This commit is contained in:
parent
aa937ee6a1
commit
6fa1bcccbc
5 changed files with 168 additions and 190 deletions
|
|
@ -528,7 +528,9 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
|
||||||
final Bundle bundle = new Bundle();
|
final Bundle bundle = new Bundle();
|
||||||
try {
|
try {
|
||||||
bundle.putString("query",
|
bundle.putString("query",
|
||||||
FileUtils.readFromResource("/queries/radius_query_for_upload_wizard.rq"));
|
FileUtils.INSTANCE.readFromResource(
|
||||||
|
"/queries/radius_query_for_upload_wizard.rq")
|
||||||
|
);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Timber.e(e);
|
Timber.e(e);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,183 +0,0 @@
|
||||||
package fr.free.nrw.commons.upload;
|
|
||||||
|
|
||||||
import android.content.ContentResolver;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.webkit.MimeTypeMap;
|
|
||||||
|
|
||||||
import androidx.exifinterface.media.ExifInterface;
|
|
||||||
|
|
||||||
import fr.free.nrw.commons.location.LatLng;
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.math.BigInteger;
|
|
||||||
import java.security.MessageDigest;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
|
|
||||||
import java.util.Locale;
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
public class FileUtils {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get SHA1 of filePath from input stream
|
|
||||||
*/
|
|
||||||
static String getSHA1(InputStream is) {
|
|
||||||
|
|
||||||
MessageDigest digest;
|
|
||||||
try {
|
|
||||||
digest = MessageDigest.getInstance("SHA1");
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
|
||||||
Timber.e(e, "Exception while getting Digest");
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] buffer = new byte[8192];
|
|
||||||
int read;
|
|
||||||
try {
|
|
||||||
while ((read = is.read(buffer)) > 0) {
|
|
||||||
digest.update(buffer, 0, read);
|
|
||||||
}
|
|
||||||
byte[] md5sum = digest.digest();
|
|
||||||
BigInteger bigInt = new BigInteger(1, md5sum);
|
|
||||||
String output = bigInt.toString(16);
|
|
||||||
// Fill to 40 chars
|
|
||||||
output = String.format("%40s", output).replace(' ', '0');
|
|
||||||
Timber.i("File SHA1: %s", output);
|
|
||||||
|
|
||||||
return output;
|
|
||||||
} catch (IOException e) {
|
|
||||||
Timber.e(e, "IO Exception");
|
|
||||||
return "";
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
is.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
Timber.e(e, "Exception on closing MD5 input stream");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get Geolocation of filePath from input filePath path
|
|
||||||
*/
|
|
||||||
static String getGeolocationOfFile(String filePath, LatLng inAppPictureLocation) {
|
|
||||||
|
|
||||||
try {
|
|
||||||
ExifInterface exifInterface = new ExifInterface(filePath);
|
|
||||||
ImageCoordinates imageObj = new ImageCoordinates(exifInterface, inAppPictureLocation);
|
|
||||||
if (imageObj.getDecimalCoords() != null) { // If image has geolocation information in its EXIF
|
|
||||||
return imageObj.getDecimalCoords();
|
|
||||||
} else {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read and return the content of a resource filePath as string.
|
|
||||||
*
|
|
||||||
* @param fileName asset filePath's path (e.g. "/queries/radius_query_for_upload_wizard.rq")
|
|
||||||
* @return the content of the filePath
|
|
||||||
*/
|
|
||||||
public static String readFromResource(String fileName) throws IOException {
|
|
||||||
StringBuilder buffer = new StringBuilder();
|
|
||||||
BufferedReader reader = null;
|
|
||||||
try {
|
|
||||||
InputStream inputStream = FileUtils.class.getResourceAsStream(fileName);
|
|
||||||
if (inputStream == null) {
|
|
||||||
throw new FileNotFoundException(fileName);
|
|
||||||
}
|
|
||||||
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
|
|
||||||
String line;
|
|
||||||
while ((line = reader.readLine()) != null) {
|
|
||||||
buffer.append(line).append("\n");
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (reader != null) {
|
|
||||||
reader.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return buffer.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes files.
|
|
||||||
*
|
|
||||||
* @param file context
|
|
||||||
*/
|
|
||||||
public static boolean deleteFile(File file) {
|
|
||||||
boolean deletedAll = true;
|
|
||||||
if (file != null) {
|
|
||||||
if (file.isDirectory()) {
|
|
||||||
String[] children = file.list();
|
|
||||||
for (String child : children) {
|
|
||||||
deletedAll = deleteFile(new File(file, child)) && deletedAll;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
deletedAll = file.delete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return deletedAll;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getMimeType(Context context, Uri uri) {
|
|
||||||
String mimeType;
|
|
||||||
if (uri.getScheme()!=null && uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
|
|
||||||
ContentResolver cr = context.getContentResolver();
|
|
||||||
mimeType = cr.getType(uri);
|
|
||||||
} else {
|
|
||||||
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
|
|
||||||
.toString());
|
|
||||||
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
|
|
||||||
fileExtension.toLowerCase(Locale.getDefault()));
|
|
||||||
}
|
|
||||||
return mimeType;
|
|
||||||
}
|
|
||||||
|
|
||||||
static String getFileExt(String fileName) {
|
|
||||||
//Default filePath extension
|
|
||||||
String extension = ".jpg";
|
|
||||||
|
|
||||||
int i = fileName.lastIndexOf('.');
|
|
||||||
if (i > 0) {
|
|
||||||
extension = fileName.substring(i + 1);
|
|
||||||
}
|
|
||||||
return extension;
|
|
||||||
}
|
|
||||||
|
|
||||||
static FileInputStream getFileInputStream(String filePath) throws FileNotFoundException {
|
|
||||||
return new FileInputStream(filePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean recursivelyCreateDirs(String dirPath) {
|
|
||||||
File fileDir = new File(dirPath);
|
|
||||||
if (!fileDir.exists()) {
|
|
||||||
return fileDir.mkdirs();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if file exists in local dirs
|
|
||||||
*/
|
|
||||||
public static boolean fileExists(Uri localUri) {
|
|
||||||
try {
|
|
||||||
File file = new File(localUri.getPath());
|
|
||||||
return file.exists();
|
|
||||||
} catch (Exception e) {
|
|
||||||
Timber.d(e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
159
app/src/main/java/fr/free/nrw/commons/upload/FileUtils.kt
Normal file
159
app/src/main/java/fr/free/nrw/commons/upload/FileUtils.kt
Normal file
|
|
@ -0,0 +1,159 @@
|
||||||
|
package fr.free.nrw.commons.upload
|
||||||
|
|
||||||
|
import android.content.ContentResolver
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
|
import android.webkit.MimeTypeMap
|
||||||
|
import androidx.exifinterface.media.ExifInterface
|
||||||
|
import fr.free.nrw.commons.location.LatLng
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.io.BufferedReader
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileInputStream
|
||||||
|
import java.io.FileNotFoundException
|
||||||
|
import java.io.IOException
|
||||||
|
import java.io.InputStream
|
||||||
|
import java.io.InputStreamReader
|
||||||
|
import java.math.BigInteger
|
||||||
|
import java.security.MessageDigest
|
||||||
|
import java.security.NoSuchAlgorithmException
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
|
object FileUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get SHA1 of filePath from input stream
|
||||||
|
*/
|
||||||
|
fun getSHA1(stream: InputStream): String {
|
||||||
|
val digest: MessageDigest
|
||||||
|
try {
|
||||||
|
digest = MessageDigest.getInstance("SHA1")
|
||||||
|
} catch (e: NoSuchAlgorithmException) {
|
||||||
|
Timber.e(e, "Exception while getting Digest")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
val buffer = ByteArray(8192)
|
||||||
|
var read: Int
|
||||||
|
try {
|
||||||
|
while ((stream.read(buffer).also { read = it }) > 0) {
|
||||||
|
digest.update(buffer, 0, read)
|
||||||
|
}
|
||||||
|
val md5sum = digest.digest()
|
||||||
|
val bigInt = BigInteger(1, md5sum)
|
||||||
|
var output = bigInt.toString(16)
|
||||||
|
// Fill to 40 chars
|
||||||
|
output = String.format("%40s", output).replace(' ', '0')
|
||||||
|
Timber.i("File SHA1: %s", output)
|
||||||
|
|
||||||
|
return output
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Timber.e(e, "IO Exception")
|
||||||
|
return ""
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
stream.close()
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Timber.e(e, "Exception on closing MD5 input stream")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Geolocation of filePath from input filePath path
|
||||||
|
*/
|
||||||
|
fun getGeolocationOfFile(filePath: String, inAppPictureLocation: LatLng?): String? = try {
|
||||||
|
val exifInterface = ExifInterface(filePath)
|
||||||
|
val imageObj = ImageCoordinates(exifInterface, inAppPictureLocation)
|
||||||
|
if (imageObj.decimalCoords != null) { // If image has geolocation information in its EXIF
|
||||||
|
imageObj.decimalCoords
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Timber.e(e)
|
||||||
|
""
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read and return the content of a resource filePath as string.
|
||||||
|
*
|
||||||
|
* @param fileName asset filePath's path (e.g. "/queries/radius_query_for_upload_wizard.rq")
|
||||||
|
* @return the content of the filePath
|
||||||
|
*/
|
||||||
|
@Throws(IOException::class)
|
||||||
|
fun readFromResource(fileName: String) = buildString {
|
||||||
|
try {
|
||||||
|
val inputStream = FileUtils::class.java.getResourceAsStream(fileName) ?:
|
||||||
|
throw FileNotFoundException(fileName)
|
||||||
|
|
||||||
|
BufferedReader(InputStreamReader(inputStream, "UTF-8")).use { reader ->
|
||||||
|
var line: String?
|
||||||
|
while ((reader.readLine().also { line = it }) != null) {
|
||||||
|
append(line).append("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
Timber.e(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes files.
|
||||||
|
*
|
||||||
|
* @param file context
|
||||||
|
*/
|
||||||
|
fun deleteFile(file: File?): Boolean {
|
||||||
|
var deletedAll = true
|
||||||
|
if (file != null) {
|
||||||
|
if (file.isDirectory) {
|
||||||
|
val children = file.list()
|
||||||
|
for (child in children!!) {
|
||||||
|
deletedAll = deleteFile(File(file, child)) && deletedAll
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
deletedAll = file.delete()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return deletedAll
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getMimeType(context: Context, uri: Uri): String? {
|
||||||
|
val mimeType: String?
|
||||||
|
if (uri.scheme != null && uri.scheme == ContentResolver.SCHEME_CONTENT) {
|
||||||
|
val cr = context.contentResolver
|
||||||
|
mimeType = cr.getType(uri)
|
||||||
|
} else {
|
||||||
|
val fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri.toString())
|
||||||
|
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
|
||||||
|
fileExtension.lowercase(Locale.getDefault())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return mimeType
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getFileExt(fileName: String): String {
|
||||||
|
//Default filePath extension
|
||||||
|
var extension = ".jpg"
|
||||||
|
|
||||||
|
val i = fileName.lastIndexOf('.')
|
||||||
|
if (i > 0) {
|
||||||
|
extension = fileName.substring(i + 1)
|
||||||
|
}
|
||||||
|
return extension
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(FileNotFoundException::class)
|
||||||
|
fun getFileInputStream(filePath: String?): FileInputStream =
|
||||||
|
FileInputStream(filePath)
|
||||||
|
|
||||||
|
fun recursivelyCreateDirs(dirPath: String): Boolean {
|
||||||
|
val fileDir = File(dirPath)
|
||||||
|
if (!fileDir.exists()) {
|
||||||
|
return fileDir.mkdirs()
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -29,19 +29,19 @@ public class FileUtilsWrapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFileExt(String fileName) {
|
public String getFileExt(String fileName) {
|
||||||
return FileUtils.getFileExt(fileName);
|
return FileUtils.INSTANCE.getFileExt(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSHA1(InputStream is) {
|
public String getSHA1(InputStream is) {
|
||||||
return FileUtils.getSHA1(is);
|
return FileUtils.INSTANCE.getSHA1(is);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileInputStream getFileInputStream(String filePath) throws FileNotFoundException {
|
public FileInputStream getFileInputStream(String filePath) throws FileNotFoundException {
|
||||||
return FileUtils.getFileInputStream(filePath);
|
return FileUtils.INSTANCE.getFileInputStream(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getGeolocationOfFile(String filePath, LatLng inAppPictureLocation) {
|
public String getGeolocationOfFile(String filePath, LatLng inAppPictureLocation) {
|
||||||
return FileUtils.getGeolocationOfFile(filePath, inAppPictureLocation);
|
return FileUtils.INSTANCE.getGeolocationOfFile(filePath, inAppPictureLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMimeType(File file) {
|
public String getMimeType(File file) {
|
||||||
|
|
@ -49,7 +49,7 @@ public class FileUtilsWrapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMimeType(Uri uri) {
|
public String getMimeType(Uri uri) {
|
||||||
return FileUtils.getMimeType(context, uri);
|
return FileUtils.INSTANCE.getMimeType(context, uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -187,7 +187,7 @@ public class UploadModel {
|
||||||
public Observable<Contribution> buildContributions() {
|
public Observable<Contribution> buildContributions() {
|
||||||
return Observable.fromIterable(items).map(item ->
|
return Observable.fromIterable(items).map(item ->
|
||||||
{
|
{
|
||||||
String imageSHA1 = FileUtils.getSHA1(context.getContentResolver().openInputStream(item.getContentUri()));
|
String imageSHA1 = FileUtils.INSTANCE.getSHA1(context.getContentResolver().openInputStream(item.getContentUri()));
|
||||||
|
|
||||||
final Contribution contribution = new Contribution(
|
final Contribution contribution = new Contribution(
|
||||||
item, sessionManager, newListOf(selectedDepictions), newListOf(selectedCategories), imageSHA1);
|
item, sessionManager, newListOf(selectedDepictions), newListOf(selectedCategories), imageSHA1);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue