From e68341efb2f009c50cc08f7882956b9a43f10546 Mon Sep 17 00:00:00 2001 From: Paul Hawke Date: Tue, 10 Dec 2024 21:06:01 -0600 Subject: [PATCH] Convert FileUtilsWrapper to kotlin --- .../nrw/commons/upload/FileUtilsWrapper.java | 94 ------------------- .../nrw/commons/upload/FileUtilsWrapper.kt | 85 +++++++++++++++++ .../free/nrw/commons/upload/UploadClient.kt | 2 +- 3 files changed, 86 insertions(+), 95 deletions(-) delete mode 100644 app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.java create mode 100644 app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.kt diff --git a/app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.java b/app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.java deleted file mode 100644 index a2ca62917..000000000 --- a/app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.java +++ /dev/null @@ -1,94 +0,0 @@ -package fr.free.nrw.commons.upload; - -import android.content.Context; -import android.net.Uri; -import fr.free.nrw.commons.location.LatLng; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import javax.inject.Inject; -import javax.inject.Singleton; -import timber.log.Timber; - -@Singleton -public class FileUtilsWrapper { - - private final Context context; - - @Inject - public FileUtilsWrapper(final Context context) { - this.context = context; - } - - public String getFileExt(String fileName) { - return FileUtils.INSTANCE.getFileExt(fileName); - } - - public String getSHA1(InputStream is) { - return FileUtils.INSTANCE.getSHA1(is); - } - - public FileInputStream getFileInputStream(String filePath) throws FileNotFoundException { - return FileUtils.INSTANCE.getFileInputStream(filePath); - } - - public String getGeolocationOfFile(String filePath, LatLng inAppPictureLocation) { - return FileUtils.INSTANCE.getGeolocationOfFile(filePath, inAppPictureLocation); - } - - public String getMimeType(File file) { - return getMimeType(Uri.parse(file.getPath())); - } - - public String getMimeType(Uri uri) { - return FileUtils.INSTANCE.getMimeType(context, uri); - } - - /** - * Takes a file as input and returns an Observable of files with the specified chunk size - */ - public List getFileChunks(File file, final int chunkSize) - throws IOException { - final byte[] buffer = new byte[chunkSize]; - - //try-with-resources to ensure closing stream - try (final FileInputStream fis = new FileInputStream(file); - final BufferedInputStream bis = new BufferedInputStream(fis)) { - final List buffers = new ArrayList<>(); - int size; - while ((size = bis.read(buffer)) > 0) { - buffers.add(writeToFile(Arrays.copyOf(buffer, size), file.getName(), - getFileExt(file.getName()))); - } - return buffers; - } - } - - /** - * Create a temp file containing the passed byte data. - */ - private File writeToFile(final byte[] data, final String fileName, - String fileExtension) - throws IOException { - final File file = File.createTempFile(fileName, fileExtension, context.getCacheDir()); - try { - if (!file.exists()) { - file.createNewFile(); - } - final FileOutputStream fos = new FileOutputStream(file); - fos.write(data); - fos.close(); - } catch (final Exception throwable) { - Timber.e(throwable, "Failed to create file"); - } - return file; - } -} diff --git a/app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.kt b/app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.kt new file mode 100644 index 000000000..aa1f8aed6 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/upload/FileUtilsWrapper.kt @@ -0,0 +1,85 @@ +package fr.free.nrw.commons.upload + +import android.content.Context +import android.net.Uri +import fr.free.nrw.commons.location.LatLng +import fr.free.nrw.commons.upload.FileUtils.getMimeType +import timber.log.Timber +import java.io.BufferedInputStream +import java.io.File +import java.io.FileInputStream +import java.io.FileNotFoundException +import java.io.FileOutputStream +import java.io.IOException +import java.io.InputStream +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class FileUtilsWrapper @Inject constructor(private val context: Context) { + fun getSHA1(stream: InputStream?): String = + stream?.let { FileUtils.getSHA1(it) } ?: "" + + @Throws(FileNotFoundException::class) + fun getFileInputStream(filePath: String?): FileInputStream = + FileUtils.getFileInputStream(filePath) + + fun getGeolocationOfFile(filePath: String, inAppPictureLocation: LatLng?): String? = + FileUtils.getGeolocationOfFile(filePath, inAppPictureLocation) + + fun getMimeType(file: File?): String? = + getMimeType(Uri.parse(file?.path)) + + fun getMimeType(uri: Uri): String? = + getMimeType(context, uri) + + /** + * Takes a file as input and returns an Observable of files with the specified chunk size + */ + @Throws(IOException::class) + fun getFileChunks(file: File?, chunkSize: Int): List { + if (file == null) return emptyList() + + val buffer = ByteArray(chunkSize) + + FileInputStream(file).use { fis -> + BufferedInputStream(fis).use { bis -> + val buffers: MutableList = ArrayList() + var size: Int + while ((bis.read(buffer).also { size = it }) > 0) { + buffers.add( + writeToFile( + buffer.copyOf(size), + file.name ?: "", + getFileExt(file.name) + ) + ) + } + return buffers + } + } + } + + private fun getFileExt(fileName: String): String = + FileUtils.getFileExt(fileName) + + /** + * Create a temp file containing the passed byte data. + */ + @Throws(IOException::class) + private fun writeToFile(data: ByteArray, fileName: String, fileExtension: String): File { + val file = File.createTempFile(fileName, fileExtension, context.cacheDir) + try { + if (!file.exists()) { + file.createNewFile() + } + + FileOutputStream(file).use { fos -> + fos.write(data) + } + } catch (throwable: Exception) { + Timber.e(throwable, "Failed to create file") + } + return file + } +} diff --git a/app/src/main/java/fr/free/nrw/commons/upload/UploadClient.kt b/app/src/main/java/fr/free/nrw/commons/upload/UploadClient.kt index 41b363f17..754ae05dd 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/UploadClient.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/UploadClient.kt @@ -69,7 +69,7 @@ class UploadClient val file = contribution.localUriPath val fileChunks = fileUtilsWrapper.getFileChunks(file, chunkSize) - val mediaType = fileUtilsWrapper.getMimeType(file).toMediaTypeOrNull() + val mediaType = fileUtilsWrapper.getMimeType(file)?.toMediaTypeOrNull() val chunkInfo = AtomicReference() if (isStashValid(contribution)) {