diff --git a/app/src/main/java/fr/free/nrw/commons/utils/ContributionUtils.java b/app/src/main/java/fr/free/nrw/commons/utils/ContributionUtils.java index 35c30a8f7..eb7cec957 100644 --- a/app/src/main/java/fr/free/nrw/commons/utils/ContributionUtils.java +++ b/app/src/main/java/fr/free/nrw/commons/utils/ContributionUtils.java @@ -4,6 +4,11 @@ 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; import java.util.Random; import timber.log.Timber; @@ -29,9 +34,9 @@ public class ContributionUtils { // TODO add exceptions for Google Drive URİ is needed Uri result = null; - if (FileUtils.checkIfDirectoryExists(TEMP_EXTERNAL_DIRECTORY)) { + if (checkIfDirectoryExists(TEMP_EXTERNAL_DIRECTORY)) { 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 File file = new File(TEMP_EXTERNAL_DIRECTORY); @@ -53,29 +58,25 @@ public class ContributionUtils { //TODO: do I have to notify file system about deletion? File tempFile = new File(tempFileUri.getPath()); if (tempFile.exists()) { - boolean isDeleted= tempFile.delete(); + boolean isDeleted = tempFile.delete(); Timber.e("removeTemporaryFile() parameters: URI tempFileUri %s, deleted status %b", tempFileUri, isDeleted); } } private static String decideTempDestinationFileName() { int i = 0; - while (true) { - if (new File(TEMP_EXTERNAL_DIRECTORY +File.separatorChar+i+"_tmp").exists()) { - // 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"; - } + while (new File(TEMP_EXTERNAL_DIRECTORY + File.separatorChar + i + "_tmp").exists()) { + i++; } + + // 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() { @@ -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(); + } } diff --git a/app/src/main/java/fr/free/nrw/commons/utils/FileUtils.java b/app/src/main/java/fr/free/nrw/commons/utils/FileUtils.java deleted file mode 100644 index 26ab5b2ca..000000000 --- a/app/src/main/java/fr/free/nrw/commons/utils/FileUtils.java +++ /dev/null @@ -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 - } - } -}