kopia lustrzana https://github.com/ryukoposting/Signal-Android
Check if the content uri already exists and rename the file until it's valid to insert.
Fixes #10159fork-5.53.8
rodzic
2954c31b5f
commit
2cca6a5afb
|
@ -4,9 +4,11 @@ import android.content.ContentResolver;
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface.OnClickListener;
|
import android.content.DialogInterface.OnClickListener;
|
||||||
|
import android.database.Cursor;
|
||||||
import android.media.MediaScannerConnection;
|
import android.media.MediaScannerConnection;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
import android.os.Environment;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.webkit.MimeTypeMap;
|
import android.webkit.MimeTypeMap;
|
||||||
|
@ -97,7 +99,7 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
||||||
fileName = sanitizeOutputFileName(fileName);
|
fileName = sanitizeOutputFileName(fileName);
|
||||||
|
|
||||||
Uri outputUri = getMediaStoreContentUriForType(contentType);
|
Uri outputUri = getMediaStoreContentUriForType(contentType);
|
||||||
Uri mediaUri = createOutputUri(outputUri, fileName);
|
Uri mediaUri = createOutputUri(outputUri, contentType, fileName);
|
||||||
|
|
||||||
try (InputStream inputStream = PartAuthority.getAttachmentStream(context, attachment.uri)) {
|
try (InputStream inputStream = PartAuthority.getAttachmentStream(context, attachment.uri)) {
|
||||||
|
|
||||||
|
@ -138,6 +140,20 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getExternalPathToFileForType(String contentType) {
|
||||||
|
File storage;
|
||||||
|
if (contentType.startsWith("video/")) {
|
||||||
|
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
|
||||||
|
} else if (contentType.startsWith("audio/")) {
|
||||||
|
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
|
||||||
|
} else if (contentType.startsWith("image/")) {
|
||||||
|
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
||||||
|
} else {
|
||||||
|
storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
|
||||||
|
}
|
||||||
|
return storage.getAbsolutePath();
|
||||||
|
}
|
||||||
|
|
||||||
private String generateOutputFileName(@NonNull String contentType, long timestamp) {
|
private String generateOutputFileName(@NonNull String contentType, long timestamp) {
|
||||||
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
||||||
String extension = mimeTypeMap.getExtensionFromMimeType(contentType);
|
String extension = mimeTypeMap.getExtensionFromMimeType(contentType);
|
||||||
|
@ -153,7 +169,9 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
||||||
return new File(fileName).getName();
|
return new File(fileName).getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Uri createOutputUri(@NonNull Uri outputUri, @NonNull String fileName) throws IOException {
|
private Uri createOutputUri(@NonNull Uri outputUri, @NonNull String contentType, @NonNull String fileName)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
String[] fileParts = getFileNameParts(fileName);
|
String[] fileParts = getFileNameParts(fileName);
|
||||||
String base = fileParts[0];
|
String base = fileParts[0];
|
||||||
String extension = fileParts[1];
|
String extension = fileParts[1];
|
||||||
|
@ -167,9 +185,7 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT > 28) {
|
if (Build.VERSION.SDK_INT > 28) {
|
||||||
contentValues.put(MediaStore.MediaColumns.IS_PENDING, 1);
|
contentValues.put(MediaStore.MediaColumns.IS_PENDING, 1);
|
||||||
}
|
} else if (Objects.equals(outputUri.getScheme(), ContentResolver.SCHEME_FILE)) {
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT <= 28 && Objects.equals(outputUri.getScheme(), ContentResolver.SCHEME_FILE)) {
|
|
||||||
File outputDirectory = new File(outputUri.getPath());
|
File outputDirectory = new File(outputUri.getPath());
|
||||||
File outputFile = new File(outputDirectory, base + "." + extension);
|
File outputFile = new File(outputDirectory, base + "." + extension);
|
||||||
|
|
||||||
|
@ -183,11 +199,35 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
||||||
}
|
}
|
||||||
|
|
||||||
return Uri.fromFile(outputFile);
|
return Uri.fromFile(outputFile);
|
||||||
|
} else {
|
||||||
|
String outputFileName = fileName;
|
||||||
|
String dataPath = String.format("%s/%s", getExternalPathToFileForType(contentType), outputFileName);
|
||||||
|
int i = 0;
|
||||||
|
while (pathTaken(outputUri, dataPath)) {
|
||||||
|
Log.d(TAG, "The content exists. Rename and check again.");
|
||||||
|
outputFileName = base + "-" + (++i) + "." + extension;
|
||||||
|
dataPath = String.format("%s/%s", getExternalPathToFileForType(contentType), outputFileName);
|
||||||
|
}
|
||||||
|
contentValues.put(MediaStore.MediaColumns.DATA, dataPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
return getContext().getContentResolver().insert(outputUri, contentValues);
|
return getContext().getContentResolver().insert(outputUri, contentValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean pathTaken(@NonNull Uri outputUri, @NonNull String dataPath) throws IOException {
|
||||||
|
try (Cursor cursor = getContext().getContentResolver().query(outputUri,
|
||||||
|
new String[] { MediaStore.MediaColumns.DATA },
|
||||||
|
MediaStore.MediaColumns.DATA + " = ?",
|
||||||
|
new String[] { dataPath },
|
||||||
|
null))
|
||||||
|
{
|
||||||
|
if (cursor == null) {
|
||||||
|
throw new IOException("Something is wrong with the filename to save");
|
||||||
|
}
|
||||||
|
return cursor.moveToFirst();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private String[] getFileNameParts(String fileName) {
|
private String[] getFileNameParts(String fileName) {
|
||||||
String[] result = new String[2];
|
String[] result = new String[2];
|
||||||
String[] tokens = fileName.split("\\.(?=[^\\.]+$)");
|
String[] tokens = fileName.split("\\.(?=[^\\.]+$)");
|
||||||
|
|
|
@ -130,7 +130,7 @@ public class StorageUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @NonNull Uri getAudioUri() {
|
public static @NonNull Uri getAudioUri() {
|
||||||
if (Build.VERSION.SDK_INT < 29) {
|
if (Build.VERSION.SDK_INT < 21) {
|
||||||
return getLegacyUri(Environment.DIRECTORY_MUSIC);
|
return getLegacyUri(Environment.DIRECTORY_MUSIC);
|
||||||
} else {
|
} else {
|
||||||
return MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
return MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
|
||||||
|
|
Ładowanie…
Reference in New Issue