From a09378b08f12c6c331f5eac65120f406e11dcaad Mon Sep 17 00:00:00 2001 From: Ben Kwa Date: Wed, 22 Apr 2015 15:50:14 -0700 Subject: Sanitize display names, keep extensions intact. Use the newly factored FileUtils sanitize the requested display names to be valid FAT filenames, and also allow any extension that maps to the requested MIME type. BUG=20157955 Change-Id: Ic37863a3362a941d81632bd4a7562dae40053652 --- .../android/providers/downloads/DownloadStorageProvider.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'src/com/android/providers') diff --git a/src/com/android/providers/downloads/DownloadStorageProvider.java b/src/com/android/providers/downloads/DownloadStorageProvider.java index 80d78551..1b5dc844 100644 --- a/src/com/android/providers/downloads/DownloadStorageProvider.java +++ b/src/com/android/providers/downloads/DownloadStorageProvider.java @@ -29,6 +29,7 @@ import android.net.Uri; import android.os.Binder; import android.os.CancellationSignal; import android.os.Environment; +import android.os.FileUtils; import android.os.ParcelFileDescriptor; import android.provider.DocumentsContract; import android.provider.DocumentsContract.Document; @@ -105,6 +106,8 @@ public class DownloadStorageProvider extends DocumentsProvider { @Override public String createDocument(String docId, String mimeType, String displayName) throws FileNotFoundException { + displayName = FileUtils.buildValidFatFilename(displayName); + if (Document.MIME_TYPE_DIR.equals(mimeType)) { throw new FileNotFoundException("Directory creation not supported"); } @@ -116,14 +119,7 @@ public class DownloadStorageProvider extends DocumentsProvider { // Delegate to real provider final long token = Binder.clearCallingIdentity(); try { - displayName = removeExtension(mimeType, displayName); - File file = new File(parent, addExtension(mimeType, displayName)); - - // If conflicting file, try adding counter suffix - int n = 0; - while (file.exists() && n++ < 32) { - file = new File(parent, addExtension(mimeType, displayName + " (" + n + ")")); - } + final File file = FileUtils.buildUniqueFile(parent, mimeType, displayName); try { if (!file.createNewFile()) { -- cgit v1.2.3