Clear filename

This commit is contained in:
tom79 2019-04-16 18:32:38 +02:00
parent 9ed45ad3c7
commit 03ac93b9c7
2 changed files with 30 additions and 2 deletions

View File

@ -66,6 +66,7 @@ import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Attachment;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.helper.FileNameCleaner;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnDownloadInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAttachmentInterface;
@ -605,6 +606,7 @@ public class HttpsConnection {
fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1,
downloadUrl.length());
}
fileName = FileNameCleaner.cleanFileName(fileName);
// opens input stream from the HTTP connection
InputStream inputStream = httpsURLConnection.getInputStream();
File saveDir = context.getCacheDir();
@ -692,6 +694,7 @@ public class HttpsConnection {
fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1,
downloadUrl.length());
}
fileName = FileNameCleaner.cleanFileName(fileName);
// opens input stream from the HTTP connection
InputStream inputStream = httpURLConnection.getInputStream();
File saveDir = context.getCacheDir();
@ -815,6 +818,7 @@ public class HttpsConnection {
@SuppressWarnings("SameParameterValue")
private void patchImage(String urlConnection, int timeout, imageType it, InputStream image, String fileName, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
fileName = FileNameCleaner.cleanFileName(fileName);
String twoHyphens = "--";
String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
String lineEnd = "\r\n";
@ -1201,8 +1205,8 @@ public class HttpsConnection {
* @param inputStream InputStream of the file to upload
* @param listener - OnRetrieveAttachmentInterface: listener to send information about attachment once uploaded.
*/
public void upload(final InputStream inputStream, String fileName, String tokenUsed, final OnRetrieveAttachmentInterface listener) {
public void upload(final InputStream inputStream, String fname, String tokenUsed, final OnRetrieveAttachmentInterface listener) {
final String fileName = FileNameCleaner.cleanFileName(fname);
if( Helper.getLiveInstanceWithProtocol(context).startsWith("https://")) {
new Thread(new Runnable() {
@Override

View File

@ -0,0 +1,24 @@
package fr.gouv.etalab.mastodon.helper;
import java.util.Arrays;
/**
* Work from https://stackoverflow.com/a/5626340
*/
public class FileNameCleaner {
private final static int[] illegalChars = {34, 60, 62, 124, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 58, 42, 63, 92, 47};
static {
Arrays.sort(illegalChars);
}
public static String cleanFileName(String badFileName) {
StringBuilder cleanName = new StringBuilder();
for (int i = 0; i < badFileName.length(); i++) {
int c = (int)badFileName.charAt(i);
if (Arrays.binarySearch(illegalChars, c) < 0) {
cleanName.append((char)c);
}
}
return cleanName.toString();
}
}