mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-11-01 23:33:54 +01:00
Convert FileUtilsWrapper to kotlin
This commit is contained in:
parent
6fa1bcccbc
commit
e68341efb2
3 changed files with 86 additions and 95 deletions
|
|
@ -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<File> 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<File> 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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<File> {
|
||||||
|
if (file == null) return emptyList()
|
||||||
|
|
||||||
|
val buffer = ByteArray(chunkSize)
|
||||||
|
|
||||||
|
FileInputStream(file).use { fis ->
|
||||||
|
BufferedInputStream(fis).use { bis ->
|
||||||
|
val buffers: MutableList<File> = 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -69,7 +69,7 @@ class UploadClient
|
||||||
|
|
||||||
val file = contribution.localUriPath
|
val file = contribution.localUriPath
|
||||||
val fileChunks = fileUtilsWrapper.getFileChunks(file, chunkSize)
|
val fileChunks = fileUtilsWrapper.getFileChunks(file, chunkSize)
|
||||||
val mediaType = fileUtilsWrapper.getMimeType(file).toMediaTypeOrNull()
|
val mediaType = fileUtilsWrapper.getMimeType(file)?.toMediaTypeOrNull()
|
||||||
|
|
||||||
val chunkInfo = AtomicReference<ChunkInfo?>()
|
val chunkInfo = AtomicReference<ChunkInfo?>()
|
||||||
if (isStashValid(contribution)) {
|
if (isStashValid(contribution)) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue