mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 04:13:53 +01:00
Remove utils.FileUtils (#2118)
This commit is contained in:
parent
378a298e32
commit
8f0a619a6d
2 changed files with 68 additions and 107 deletions
|
|
@ -4,6 +4,11 @@ import android.content.Context;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
@ -29,9 +34,9 @@ public class ContributionUtils {
|
||||||
// TODO add exceptions for Google Drive URİ is needed
|
// TODO add exceptions for Google Drive URİ is needed
|
||||||
Uri result = null;
|
Uri result = null;
|
||||||
|
|
||||||
if (FileUtils.checkIfDirectoryExists(TEMP_EXTERNAL_DIRECTORY)) {
|
if (checkIfDirectoryExists(TEMP_EXTERNAL_DIRECTORY)) {
|
||||||
String destinationFilename = decideTempDestinationFileName();
|
String destinationFilename = decideTempDestinationFileName();
|
||||||
result = FileUtils.saveFileFromURI(context, URIfromContentProvider, destinationFilename);
|
result = saveFileFromURI(context, URIfromContentProvider, destinationFilename);
|
||||||
} else { // If directory doesn't exist, create it and recursive call current method to check again
|
} else { // If directory doesn't exist, create it and recursive call current method to check again
|
||||||
|
|
||||||
File file = new File(TEMP_EXTERNAL_DIRECTORY);
|
File file = new File(TEMP_EXTERNAL_DIRECTORY);
|
||||||
|
|
@ -53,29 +58,25 @@ public class ContributionUtils {
|
||||||
//TODO: do I have to notify file system about deletion?
|
//TODO: do I have to notify file system about deletion?
|
||||||
File tempFile = new File(tempFileUri.getPath());
|
File tempFile = new File(tempFileUri.getPath());
|
||||||
if (tempFile.exists()) {
|
if (tempFile.exists()) {
|
||||||
boolean isDeleted= tempFile.delete();
|
boolean isDeleted = tempFile.delete();
|
||||||
Timber.e("removeTemporaryFile() parameters: URI tempFileUri %s, deleted status %b", tempFileUri, isDeleted);
|
Timber.e("removeTemporaryFile() parameters: URI tempFileUri %s, deleted status %b", tempFileUri, isDeleted);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String decideTempDestinationFileName() {
|
private static String decideTempDestinationFileName() {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (true) {
|
while (new File(TEMP_EXTERNAL_DIRECTORY + File.separatorChar + i + "_tmp").exists()) {
|
||||||
if (new File(TEMP_EXTERNAL_DIRECTORY +File.separatorChar+i+"_tmp").exists()) {
|
i++;
|
||||||
// This file is in use, try enother file
|
|
||||||
i++;
|
|
||||||
} else {
|
|
||||||
// Use time stamp for file name, so that two temporary file never has same file name
|
|
||||||
// to prevent previous file reference bug
|
|
||||||
Long tsLong = System.currentTimeMillis()/1000;
|
|
||||||
String ts = tsLong.toString();
|
|
||||||
|
|
||||||
// For multiple uploads, time randomisation should be combined with another random
|
|
||||||
// parameter, since they created at same time
|
|
||||||
int multipleUploadRandomParameter = new Random().nextInt(100);
|
|
||||||
return TEMP_EXTERNAL_DIRECTORY +File.separatorChar+ts+multipleUploadRandomParameter+"_tmp";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use time stamp for file name, so that two temporary file never has same file name
|
||||||
|
// to prevent previous file reference bug
|
||||||
|
String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
|
||||||
|
|
||||||
|
// For multiple uploads, time randomisation should be combined with another random
|
||||||
|
// parameter, since they created at same time
|
||||||
|
int multipleUploadRandomParameter = new Random().nextInt(100);
|
||||||
|
return TEMP_EXTERNAL_DIRECTORY + File.separatorChar + timeStamp + multipleUploadRandomParameter + "_tmp";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void emptyTemporaryDirectory() {
|
public static void emptyTemporaryDirectory() {
|
||||||
|
|
@ -91,4 +92,53 @@ public class ContributionUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves file from source URI to destination.
|
||||||
|
* @param sourceUri Uri which points to file to be saved
|
||||||
|
* @param destinationFilename where file will be located at
|
||||||
|
* @return Uri points to file saved
|
||||||
|
*/
|
||||||
|
private static Uri saveFileFromURI(Context context, Uri sourceUri, String destinationFilename) {
|
||||||
|
File file = new File(destinationFilename);
|
||||||
|
if (file.exists()) {
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
InputStream in = null;
|
||||||
|
OutputStream out = null;
|
||||||
|
try {
|
||||||
|
in = context.getContentResolver().openInputStream(sourceUri);
|
||||||
|
out = new FileOutputStream(new File(destinationFilename));
|
||||||
|
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int len;
|
||||||
|
while ((len=in.read(buf)) > 0) {
|
||||||
|
out.write(buf, 0, len);
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (out != null) out.close();
|
||||||
|
if (in != null) in.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Uri.parse("file://" + destinationFilename);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if directory exists
|
||||||
|
* @param pathToCheck path of directory to check
|
||||||
|
* @return true if directory exists, false otherwise
|
||||||
|
*/
|
||||||
|
private static boolean checkIfDirectoryExists(String pathToCheck) {
|
||||||
|
File dir = new File(pathToCheck);
|
||||||
|
return dir.exists() && dir.isDirectory();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,89 +0,0 @@
|
||||||
package fr.free.nrw.commons.utils;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.net.Uri;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created for file operations
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class FileUtils {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Saves file from source URI to destination.
|
|
||||||
* @param sourceUri Uri which points to file to be saved
|
|
||||||
* @param destinationFilename where file will be located at
|
|
||||||
* @return Uri points to file saved
|
|
||||||
*/
|
|
||||||
public static Uri saveFileFromURI(Context context, Uri sourceUri, String destinationFilename) {
|
|
||||||
File file = new File(destinationFilename);
|
|
||||||
if (file.exists()) {
|
|
||||||
file.delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
InputStream in = null;
|
|
||||||
OutputStream out = null;
|
|
||||||
try {
|
|
||||||
in = context.getContentResolver().openInputStream(sourceUri);
|
|
||||||
out = new FileOutputStream(new File(destinationFilename));
|
|
||||||
|
|
||||||
byte[] buf = new byte[1024];
|
|
||||||
int len;
|
|
||||||
while((len=in.read(buf))>0){
|
|
||||||
out.write(buf,0,len);
|
|
||||||
}
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
if(out != null) {
|
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
if(in != null) {
|
|
||||||
in.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Uri.parse("file://" + destinationFilename);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if directory exists
|
|
||||||
* @param pathToCheck path of directory to check
|
|
||||||
* @return true if directory exists, false otherwise
|
|
||||||
*/
|
|
||||||
public static boolean checkIfDirectoryExists(String pathToCheck) {
|
|
||||||
File director = new File(pathToCheck);
|
|
||||||
if (director.exists() && director.isDirectory()) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates new directory.
|
|
||||||
* @param pathToCreateAt where directory will be created at
|
|
||||||
* @return true if directory is created, false if an error occured, or already exists.
|
|
||||||
*/
|
|
||||||
public static boolean createDirectory(String pathToCreateAt) {
|
|
||||||
File directory = new File(pathToCreateAt);
|
|
||||||
if (!directory.exists()) {
|
|
||||||
return directory.mkdirs(); //true if directory is created
|
|
||||||
} else {
|
|
||||||
return false; //false if file already exists
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue