Skip to content

Commit 8f0a619

Browse files
domdomeggmaskaravivek
authored andcommitted
Remove utils.FileUtils (commons-app#2118)
1 parent 378a298 commit 8f0a619

File tree

2 files changed

+68
-107
lines changed

2 files changed

+68
-107
lines changed

app/src/main/java/fr/free/nrw/commons/utils/ContributionUtils.java

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
import android.net.Uri;
55

66
import java.io.File;
7+
import java.io.FileNotFoundException;
8+
import java.io.FileOutputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.io.OutputStream;
712
import java.util.Random;
813

914
import timber.log.Timber;
@@ -29,9 +34,9 @@ public static Uri saveFileBeingUploadedTemporarily(Context context, Uri URIfromC
2934
// TODO add exceptions for Google Drive URİ is needed
3035
Uri result = null;
3136

32-
if (FileUtils.checkIfDirectoryExists(TEMP_EXTERNAL_DIRECTORY)) {
37+
if (checkIfDirectoryExists(TEMP_EXTERNAL_DIRECTORY)) {
3338
String destinationFilename = decideTempDestinationFileName();
34-
result = FileUtils.saveFileFromURI(context, URIfromContentProvider, destinationFilename);
39+
result = saveFileFromURI(context, URIfromContentProvider, destinationFilename);
3540
} else { // If directory doesn't exist, create it and recursive call current method to check again
3641

3742
File file = new File(TEMP_EXTERNAL_DIRECTORY);
@@ -53,29 +58,25 @@ public static void removeTemporaryFile(Uri tempFileUri) {
5358
//TODO: do I have to notify file system about deletion?
5459
File tempFile = new File(tempFileUri.getPath());
5560
if (tempFile.exists()) {
56-
boolean isDeleted= tempFile.delete();
61+
boolean isDeleted = tempFile.delete();
5762
Timber.e("removeTemporaryFile() parameters: URI tempFileUri %s, deleted status %b", tempFileUri, isDeleted);
5863
}
5964
}
6065

6166
private static String decideTempDestinationFileName() {
6267
int i = 0;
63-
while (true) {
64-
if (new File(TEMP_EXTERNAL_DIRECTORY +File.separatorChar+i+"_tmp").exists()) {
65-
// This file is in use, try enother file
66-
i++;
67-
} else {
68-
// Use time stamp for file name, so that two temporary file never has same file name
69-
// to prevent previous file reference bug
70-
Long tsLong = System.currentTimeMillis()/1000;
71-
String ts = tsLong.toString();
72-
73-
// For multiple uploads, time randomisation should be combined with another random
74-
// parameter, since they created at same time
75-
int multipleUploadRandomParameter = new Random().nextInt(100);
76-
return TEMP_EXTERNAL_DIRECTORY +File.separatorChar+ts+multipleUploadRandomParameter+"_tmp";
77-
}
68+
while (new File(TEMP_EXTERNAL_DIRECTORY + File.separatorChar + i + "_tmp").exists()) {
69+
i++;
7870
}
71+
72+
// Use time stamp for file name, so that two temporary file never has same file name
73+
// to prevent previous file reference bug
74+
String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
75+
76+
// For multiple uploads, time randomisation should be combined with another random
77+
// parameter, since they created at same time
78+
int multipleUploadRandomParameter = new Random().nextInt(100);
79+
return TEMP_EXTERNAL_DIRECTORY + File.separatorChar + timeStamp + multipleUploadRandomParameter + "_tmp";
7980
}
8081

8182
public static void emptyTemporaryDirectory() {
@@ -91,4 +92,53 @@ public static void emptyTemporaryDirectory() {
9192
}
9293
}
9394
}
95+
96+
/**
97+
* Saves file from source URI to destination.
98+
* @param sourceUri Uri which points to file to be saved
99+
* @param destinationFilename where file will be located at
100+
* @return Uri points to file saved
101+
*/
102+
private static Uri saveFileFromURI(Context context, Uri sourceUri, String destinationFilename) {
103+
File file = new File(destinationFilename);
104+
if (file.exists()) {
105+
file.delete();
106+
}
107+
108+
InputStream in = null;
109+
OutputStream out = null;
110+
try {
111+
in = context.getContentResolver().openInputStream(sourceUri);
112+
out = new FileOutputStream(new File(destinationFilename));
113+
114+
byte[] buf = new byte[1024];
115+
int len;
116+
while ((len=in.read(buf)) > 0) {
117+
out.write(buf, 0, len);
118+
}
119+
} catch (FileNotFoundException e) {
120+
e.printStackTrace();
121+
} catch (IOException e) {
122+
e.printStackTrace();
123+
} finally {
124+
try {
125+
if (out != null) out.close();
126+
if (in != null) in.close();
127+
} catch (IOException e) {
128+
e.printStackTrace();
129+
}
130+
}
131+
132+
return Uri.parse("file://" + destinationFilename);
133+
}
134+
135+
/**
136+
* Checks if directory exists
137+
* @param pathToCheck path of directory to check
138+
* @return true if directory exists, false otherwise
139+
*/
140+
private static boolean checkIfDirectoryExists(String pathToCheck) {
141+
File dir = new File(pathToCheck);
142+
return dir.exists() && dir.isDirectory();
143+
}
94144
}

app/src/main/java/fr/free/nrw/commons/utils/FileUtils.java

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)