mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-23 18:15:22 +00:00
Refactor storage management to have a centralized,
clearer way to get the Signal output directories Closes #6476 // FREEBIE
This commit is contained in:
parent
757ccd0c36
commit
dd3cef5ec2
@ -1,121 +0,0 @@
|
||||
/**
|
||||
* Copyright (C) 2011 Whisper Systems
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.thoughtcrime.securesms.database;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
public class EncryptedBackupExporter {
|
||||
|
||||
public static void exportToSd(Context context) throws NoExternalStorageException, IOException {
|
||||
verifyExternalStorageForExport();
|
||||
exportDirectory(context, "");
|
||||
}
|
||||
|
||||
public static void importFromSd(Context context) throws NoExternalStorageException, IOException {
|
||||
verifyExternalStorageForImport();
|
||||
importDirectory(context, "");
|
||||
}
|
||||
|
||||
private static String getExportDirectoryPath() {
|
||||
File sdDirectory = Environment.getExternalStorageDirectory();
|
||||
return sdDirectory.getAbsolutePath() + File.separator + "TextSecureExport";
|
||||
}
|
||||
|
||||
private static void verifyExternalStorageForExport() throws NoExternalStorageException {
|
||||
if (!Environment.getExternalStorageDirectory().canWrite())
|
||||
throw new NoExternalStorageException();
|
||||
|
||||
String exportDirectoryPath = getExportDirectoryPath();
|
||||
File exportDirectory = new File(exportDirectoryPath);
|
||||
|
||||
if (!exportDirectory.exists())
|
||||
exportDirectory.mkdir();
|
||||
}
|
||||
|
||||
private static void verifyExternalStorageForImport() throws NoExternalStorageException {
|
||||
if (!Environment.getExternalStorageDirectory().canRead() ||
|
||||
!(new File(getExportDirectoryPath()).exists()))
|
||||
throw new NoExternalStorageException();
|
||||
}
|
||||
|
||||
private static void migrateFile(File from, File to) {
|
||||
try {
|
||||
if (from.exists()) {
|
||||
FileChannel source = new FileInputStream(from).getChannel();
|
||||
FileChannel destination = new FileOutputStream(to).getChannel();
|
||||
|
||||
destination.transferFrom(source, 0, source.size());
|
||||
source.close();
|
||||
destination.close();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
Log.w("EncryptedBackupExporter", ioe);
|
||||
}
|
||||
}
|
||||
|
||||
private static void exportDirectory(Context context, String directoryName) throws IOException {
|
||||
File directory = new File(context.getFilesDir().getParent() + File.separatorChar + directoryName);
|
||||
File exportDirectory = new File(getExportDirectoryPath() + File.separatorChar + directoryName);
|
||||
|
||||
if (directory.exists()) {
|
||||
exportDirectory.mkdirs();
|
||||
|
||||
File[] contents = directory.listFiles();
|
||||
|
||||
for (int i=0;i<contents.length;i++) {
|
||||
File localFile = contents[i];
|
||||
|
||||
if (localFile.isFile()) {
|
||||
File exportedFile = new File(exportDirectory.getAbsolutePath() + File.separator + localFile.getName());
|
||||
migrateFile(localFile, exportedFile);
|
||||
} else {
|
||||
exportDirectory(context, directoryName + File.separator + localFile.getName());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Log.w("EncryptedBackupExporter", "Could not find directory: " + directory.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
private static void importDirectory(Context context, String directoryName) throws IOException {
|
||||
File directory = new File(getExportDirectoryPath() + File.separator + directoryName);
|
||||
File importDirectory = new File(context.getFilesDir().getParent() + File.separator + directoryName);
|
||||
|
||||
if (directory.exists() && directory.isDirectory()) {
|
||||
importDirectory.mkdirs();
|
||||
|
||||
File[] contents = directory.listFiles();
|
||||
|
||||
for (File exportedFile : contents) {
|
||||
if (exportedFile.isFile()) {
|
||||
File localFile = new File(importDirectory.getAbsolutePath() + File.separator + exportedFile.getName());
|
||||
migrateFile(exportedFile, localFile);
|
||||
} else if (exportedFile.isDirectory()) {
|
||||
importDirectory(context, directoryName + File.separator + exportedFile.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -2,10 +2,10 @@ package org.thoughtcrime.securesms.database;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
||||
import org.thoughtcrime.securesms.util.StorageUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -17,21 +17,15 @@ public class PlaintextBackupExporter {
|
||||
public static void exportPlaintextToSd(Context context, MasterSecret masterSecret)
|
||||
throws NoExternalStorageException, IOException
|
||||
{
|
||||
verifyExternalStorageForPlaintextExport();
|
||||
exportPlaintext(context, masterSecret);
|
||||
}
|
||||
|
||||
private static void verifyExternalStorageForPlaintextExport() throws NoExternalStorageException {
|
||||
if (!Environment.getExternalStorageDirectory().canWrite())
|
||||
throw new NoExternalStorageException();
|
||||
}
|
||||
|
||||
public static File getPlaintextExportFile() {
|
||||
return new File(Environment.getExternalStorageDirectory(), FILENAME);
|
||||
public static File getPlaintextExportFile() throws NoExternalStorageException {
|
||||
return new File(StorageUtil.getBackupDir(), FILENAME);
|
||||
}
|
||||
|
||||
private static void exportPlaintext(Context context, MasterSecret masterSecret)
|
||||
throws IOException
|
||||
throws NoExternalStorageException, IOException
|
||||
{
|
||||
int count = DatabaseFactory.getSmsDatabase(context).getMessageCount();
|
||||
XmlBackup.Writer writer = new XmlBackup.Writer(getPlaintextExportFile().getAbsolutePath(), count);
|
||||
|
@ -17,34 +17,10 @@ import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
public class PlaintextBackupImporter {
|
||||
|
||||
public static void importPlaintextFromSd(Context context, MasterSecret masterSecret)
|
||||
throws NoExternalStorageException, IOException
|
||||
{
|
||||
Log.w("PlaintextBackupImporter", "Importing plaintext...");
|
||||
verifyExternalStorageForPlaintextImport();
|
||||
importPlaintext(context, masterSecret);
|
||||
}
|
||||
|
||||
private static void verifyExternalStorageForPlaintextImport() throws NoExternalStorageException {
|
||||
if (!Environment.getExternalStorageDirectory().canRead() || !getPlaintextExportFile().exists())
|
||||
throw new NoExternalStorageException();
|
||||
}
|
||||
|
||||
private static File getPlaintextExportFile() {
|
||||
File backup = PlaintextBackupExporter.getPlaintextExportFile();
|
||||
File oldBackup = new File(Environment.getExternalStorageDirectory(), "TextSecurePlaintextBackup.xml");
|
||||
|
||||
if (!backup.exists() && oldBackup.exists()) {
|
||||
return oldBackup;
|
||||
}
|
||||
return backup;
|
||||
}
|
||||
|
||||
private static void importPlaintext(Context context, MasterSecret masterSecret)
|
||||
throws IOException
|
||||
{
|
||||
Log.w("PlaintextBackupImporter", "importPlaintext()");
|
||||
SmsDatabase db = DatabaseFactory.getSmsDatabase(context);
|
||||
@ -54,7 +30,7 @@ public class PlaintextBackupImporter {
|
||||
ThreadDatabase threads = DatabaseFactory.getThreadDatabase(context);
|
||||
XmlBackup backup = new XmlBackup(getPlaintextExportFile().getAbsolutePath());
|
||||
MasterCipher masterCipher = new MasterCipher(masterSecret);
|
||||
Set<Long> modifiedThreads = new HashSet<Long>();
|
||||
Set<Long> modifiedThreads = new HashSet<>();
|
||||
XmlBackup.XmlBackupItem item;
|
||||
|
||||
while ((item = backup.getNext()) != null) {
|
||||
@ -78,7 +54,7 @@ public class PlaintextBackupImporter {
|
||||
addTranslatedTypeToStatement(statement, 8, item.getType());
|
||||
addNullToStatement(statement, 9);
|
||||
addStringToStatement(statement, 10, item.getSubject());
|
||||
addEncryptedStingToStatement(masterCipher, statement, 11, item.getBody());
|
||||
addEncryptedStringToStatement(masterCipher, statement, 11, item.getBody());
|
||||
addStringToStatement(statement, 12, item.getServiceCenter());
|
||||
addLongToStatement(statement, 13, threadId);
|
||||
modifiedThreads.add(threadId);
|
||||
@ -98,7 +74,14 @@ public class PlaintextBackupImporter {
|
||||
}
|
||||
}
|
||||
|
||||
private static void addEncryptedStingToStatement(MasterCipher masterCipher, SQLiteStatement statement, int index, String value) {
|
||||
private static File getPlaintextExportFile() throws NoExternalStorageException {
|
||||
File backup = PlaintextBackupExporter.getPlaintextExportFile();
|
||||
File oldBackup = new File(Environment.getExternalStorageDirectory(), "TextSecurePlaintextBackup.xml");
|
||||
|
||||
return !backup.exists() && oldBackup.exists() ? oldBackup : backup;
|
||||
}
|
||||
|
||||
private static void addEncryptedStringToStatement(MasterCipher masterCipher, SQLiteStatement statement, int index, String value) {
|
||||
if (value == null || value.equals("null")) {
|
||||
statement.bindNull(index);
|
||||
} else {
|
||||
@ -130,6 +113,4 @@ public class PlaintextBackupImporter {
|
||||
ourType == MmsSmsColumns.Types.BASE_SENT_TYPE ||
|
||||
ourType == MmsSmsColumns.Types.BASE_SENT_FAILED_TYPE;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import android.content.DialogInterface.OnClickListener;
|
||||
import android.content.Intent;
|
||||
import android.media.MediaScannerConnection;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.design.widget.Snackbar;
|
||||
@ -17,6 +16,7 @@ import android.widget.Toast;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.database.NoExternalStorageException;
|
||||
import org.thoughtcrime.securesms.mms.PartAuthority;
|
||||
import org.thoughtcrime.securesms.util.task.ProgressDialogAsyncTask;
|
||||
import org.whispersystems.libsignal.util.Pair;
|
||||
@ -32,9 +32,9 @@ import java.text.SimpleDateFormat;
|
||||
public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTask.Attachment, Void, Pair<Integer, File>> {
|
||||
private static final String TAG = SaveAttachmentTask.class.getSimpleName();
|
||||
|
||||
private static final int SUCCESS = 0;
|
||||
private static final int FAILURE = 1;
|
||||
private static final int WRITE_ACCESS_FAILURE = 2;
|
||||
protected static final int SUCCESS = 0;
|
||||
protected static final int FAILURE = 1;
|
||||
protected static final int WRITE_ACCESS_FAILURE = 2;
|
||||
|
||||
private final WeakReference<Context> contextReference;
|
||||
private final WeakReference<MasterSecret> masterSecretReference;
|
||||
@ -67,7 +67,7 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
||||
MasterSecret masterSecret = masterSecretReference.get();
|
||||
File directory = null;
|
||||
|
||||
if (!Environment.getExternalStorageDirectory().canWrite()) {
|
||||
if (!StorageUtil.canWriteInSignalStorageDir()) {
|
||||
return new Pair<>(WRITE_ACCESS_FAILURE, null);
|
||||
}
|
||||
|
||||
@ -84,17 +84,23 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
||||
|
||||
if (attachments.length > 1) return new Pair<>(SUCCESS, null);
|
||||
else return new Pair<>(SUCCESS, directory);
|
||||
} catch (IOException ioe) {
|
||||
} catch (NoExternalStorageException|IOException ioe) {
|
||||
Log.w(TAG, ioe);
|
||||
return new Pair<>(FAILURE, null);
|
||||
}
|
||||
}
|
||||
|
||||
private @Nullable File saveAttachment(Context context, MasterSecret masterSecret, Attachment attachment)
|
||||
throws IOException
|
||||
throws NoExternalStorageException, IOException
|
||||
{
|
||||
String contentType = MediaUtil.getCorrectedMimeType(attachment.contentType);
|
||||
File mediaFile = constructOutputFile(attachment.fileName, contentType, attachment.date);
|
||||
String fileName = attachment.fileName;
|
||||
|
||||
if (fileName == null) fileName = generateOutputFileName(contentType, attachment.date);
|
||||
fileName = sanitizeOutputFileName(fileName);
|
||||
|
||||
File outputDirectory = createOutputDirectoryFromContentType(contentType);
|
||||
File mediaFile = createOutputFile(outputDirectory, fileName);
|
||||
InputStream inputStream = PartAuthority.getAttachmentStream(context, masterSecret, attachment.uri);
|
||||
|
||||
if (inputStream == null) {
|
||||
@ -110,6 +116,73 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
||||
return mediaFile.getParentFile();
|
||||
}
|
||||
|
||||
private File createOutputDirectoryFromContentType(@NonNull String contentType)
|
||||
throws NoExternalStorageException
|
||||
{
|
||||
File outputDirectory;
|
||||
|
||||
if (contentType.startsWith("video/")) {
|
||||
outputDirectory = StorageUtil.getVideoDir();
|
||||
} else if (contentType.startsWith("audio/")) {
|
||||
outputDirectory = StorageUtil.getAudioDir();
|
||||
} else if (contentType.startsWith("image/")) {
|
||||
outputDirectory = StorageUtil.getImageDir();
|
||||
} else {
|
||||
outputDirectory = StorageUtil.getDownloadDir();
|
||||
}
|
||||
|
||||
if (!outputDirectory.mkdirs()) Log.w(TAG, "mkdirs() returned false, attempting to continue");
|
||||
return outputDirectory;
|
||||
}
|
||||
|
||||
private String generateOutputFileName(@NonNull String contentType, long timestamp) {
|
||||
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
||||
String extension = mimeTypeMap.getExtensionFromMimeType(contentType);
|
||||
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
|
||||
String base = "signal-" + dateFormatter.format(timestamp);
|
||||
|
||||
if (extension == null) extension = "attach";
|
||||
|
||||
return base + "." + extension;
|
||||
}
|
||||
|
||||
private String sanitizeOutputFileName(@NonNull String fileName) {
|
||||
return new File(fileName).getName();
|
||||
}
|
||||
|
||||
private File createOutputFile(@NonNull File outputDirectory, @NonNull String fileName)
|
||||
throws IOException
|
||||
{
|
||||
String[] fileParts = getFileNameParts(fileName);
|
||||
String base = fileParts[0];
|
||||
String extension = fileParts[1];
|
||||
|
||||
File outputFile = new File(outputDirectory, base + "." + extension);
|
||||
|
||||
int i = 0;
|
||||
while (outputFile.exists()) {
|
||||
outputFile = new File(outputDirectory, base + "-" + (++i) + "." + extension);
|
||||
}
|
||||
|
||||
if (outputFile.isHidden()) {
|
||||
throw new IOException("Specified name would not be visible");
|
||||
}
|
||||
|
||||
return outputFile;
|
||||
}
|
||||
|
||||
private String[] getFileNameParts(String fileName) {
|
||||
String[] result = new String[2];
|
||||
String[] tokens = fileName.split("\\.(?=[^\\.]+$)");
|
||||
|
||||
result[0] = tokens[0];
|
||||
|
||||
if (tokens.length > 1) result[1] = tokens[1];
|
||||
else result[1] = "";
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(final Pair<Integer, File> result) {
|
||||
super.onPostExecute(result);
|
||||
@ -152,64 +225,6 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
|
||||
}
|
||||
}
|
||||
|
||||
private File constructOutputFile(@Nullable String fileName, String contentType, long timestamp)
|
||||
throws IOException
|
||||
{
|
||||
File sdCard = Environment.getExternalStorageDirectory();
|
||||
File outputDirectory;
|
||||
|
||||
if (contentType.startsWith("video/")) {
|
||||
outputDirectory = new File(sdCard.getAbsoluteFile() + File.separator + Environment.DIRECTORY_MOVIES);
|
||||
} else if (contentType.startsWith("audio/")) {
|
||||
outputDirectory = new File(sdCard.getAbsolutePath() + File.separator + Environment.DIRECTORY_MUSIC);
|
||||
} else if (contentType.startsWith("image/")) {
|
||||
outputDirectory = new File(sdCard.getAbsolutePath() + File.separator + Environment.DIRECTORY_PICTURES);
|
||||
} else {
|
||||
outputDirectory = new File(sdCard.getAbsolutePath() + File.separator + Environment.DIRECTORY_DOWNLOADS);
|
||||
}
|
||||
|
||||
if (!outputDirectory.mkdirs()) Log.w(TAG, "mkdirs() returned false, attempting to continue");
|
||||
|
||||
if (fileName == null) {
|
||||
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
||||
String extension = mimeTypeMap.getExtensionFromMimeType(contentType);
|
||||
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
|
||||
String base = "signal-" + dateFormatter.format(timestamp);
|
||||
|
||||
if (extension == null) extension = "attach";
|
||||
|
||||
fileName = base + "." + extension;
|
||||
}
|
||||
|
||||
fileName = new File(fileName).getName();
|
||||
|
||||
int i = 0;
|
||||
File file = new File(outputDirectory, fileName);
|
||||
|
||||
while (file.exists()) {
|
||||
String[] fileParts = getFileNameParts(fileName);
|
||||
file = new File(outputDirectory, fileParts[0] + "-" + (++i) + "." + fileParts[1]);
|
||||
}
|
||||
|
||||
if (file.isHidden()) {
|
||||
throw new IOException("Specified name would not be visible");
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
private String[] getFileNameParts(String fileName) {
|
||||
String[] result = new String[2];
|
||||
String[] tokens = fileName.split("\\.(?=[^\\.]+$)");
|
||||
|
||||
result[0] = tokens[0];
|
||||
|
||||
if (tokens.length > 1) result[1] = tokens[1];
|
||||
else result[1] = "";
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static class Attachment {
|
||||
public Uri uri;
|
||||
public String fileName;
|
||||
|
52
src/org/thoughtcrime/securesms/util/StorageUtil.java
Normal file
52
src/org/thoughtcrime/securesms/util/StorageUtil.java
Normal file
@ -0,0 +1,52 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.os.Environment;
|
||||
|
||||
import org.thoughtcrime.securesms.database.NoExternalStorageException;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class StorageUtil
|
||||
{
|
||||
private static File getSignalStorageDir() throws NoExternalStorageException {
|
||||
final File storage = Environment.getExternalStorageDirectory();
|
||||
|
||||
if (!storage.canWrite()) {
|
||||
throw new NoExternalStorageException();
|
||||
}
|
||||
|
||||
return storage;
|
||||
}
|
||||
|
||||
public static boolean canWriteInSignalStorageDir() {
|
||||
File storage;
|
||||
|
||||
try {
|
||||
storage = getSignalStorageDir();
|
||||
} catch (NoExternalStorageException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return storage.canWrite();
|
||||
}
|
||||
|
||||
public static File getBackupDir() throws NoExternalStorageException {
|
||||
return getSignalStorageDir();
|
||||
}
|
||||
|
||||
public static File getVideoDir() throws NoExternalStorageException {
|
||||
return new File(getSignalStorageDir(), Environment.DIRECTORY_MOVIES);
|
||||
}
|
||||
|
||||
public static File getAudioDir() throws NoExternalStorageException {
|
||||
return new File(getSignalStorageDir(), Environment.DIRECTORY_MUSIC);
|
||||
}
|
||||
|
||||
public static File getImageDir() throws NoExternalStorageException {
|
||||
return new File(getSignalStorageDir(), Environment.DIRECTORY_PICTURES);
|
||||
}
|
||||
|
||||
public static File getDownloadDir() throws NoExternalStorageException {
|
||||
return new File(getSignalStorageDir(), Environment.DIRECTORY_DOWNLOADS);
|
||||
}
|
||||
}
|
@ -0,0 +1,242 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.view.View;
|
||||
|
||||
import org.thoughtcrime.securesms.TextSecureTestCase;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.database.NoExternalStorageException;
|
||||
import org.whispersystems.libsignal.util.Pair;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class SaveAttachmentTaskTest extends TextSecureTestCase
|
||||
{
|
||||
private static final long TEST_TIMESTAMP = 585001320000L;
|
||||
|
||||
private TestSaveAttachmentTask saveAttachmentTask;
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
super.setUp();
|
||||
saveAttachmentTask = createTestSaveAttachmentTask();
|
||||
}
|
||||
|
||||
private TestSaveAttachmentTask createTestSaveAttachmentTask() {
|
||||
return new TestSaveAttachmentTask(getInstrumentation().getTargetContext(), null, null);
|
||||
}
|
||||
|
||||
public void testDoInBackground_emptyImageAttachmentWithFileNameIsCorrectlySaved()
|
||||
throws IOException, NoExternalStorageException
|
||||
{
|
||||
final String name = "testImageThatShouldNotAlreadyExist";
|
||||
final String extension = "png";
|
||||
final String outputFileName = name + "." + extension;
|
||||
final String contentType = "image/png";
|
||||
final File outputDir = StorageUtil.getImageDir();
|
||||
final File expectedOutputFile = generateOutputFileForKnownFilename(name, extension, outputDir);
|
||||
|
||||
verifyAttachmentSavedCorrectly(outputFileName, contentType, outputDir, expectedOutputFile);
|
||||
|
||||
assertTrue(expectedOutputFile.delete());
|
||||
}
|
||||
|
||||
public void testDoInBackground_emptyImageAttachmentWithFileNameIsCorrectlySavedWithIndex()
|
||||
throws IOException, NoExternalStorageException
|
||||
{
|
||||
final String name = "testImageThatShouldNotAlreadyExist";
|
||||
final String extension = "png";
|
||||
final String outputFileName = name + "." + extension;
|
||||
final String contentType = "image/png";
|
||||
final File outputDir = StorageUtil.getImageDir();
|
||||
final ArrayList<File> testFiles = populateWithTestFiles(name, extension, outputDir);
|
||||
final File expectedOutputFile = generateOutputFileForKnownFilename(name, extension, outputDir);
|
||||
testFiles.add(expectedOutputFile);
|
||||
|
||||
verifyAttachmentSavedCorrectly(outputFileName, contentType, outputDir, expectedOutputFile);
|
||||
|
||||
for (File tmpFile : testFiles) {
|
||||
assertTrue(tmpFile.delete());
|
||||
}
|
||||
}
|
||||
|
||||
public void testDoInBackground_emptyImageAttachmentWithoutFileNameIsCorrectlySaved()
|
||||
throws IOException, NoExternalStorageException
|
||||
{
|
||||
final String extension = "png";
|
||||
final String contentType = "image/png";
|
||||
final File outputDir = StorageUtil.getImageDir();
|
||||
final File expectedOutputFile = generateOutputFileForUnknownFilename(extension, TEST_TIMESTAMP, outputDir);
|
||||
|
||||
verifyAttachmentSavedCorrectly(null, contentType, outputDir, expectedOutputFile);
|
||||
|
||||
assertTrue(expectedOutputFile.delete());
|
||||
}
|
||||
|
||||
public void testDoInBackground_emptyImageAttachmentWithoutFileNameNorExtensionIsCorrectlySaved()
|
||||
throws IOException, NoExternalStorageException
|
||||
{
|
||||
final String extension = "attach";
|
||||
final String contentType = "image/";
|
||||
final File outputDir = StorageUtil.getImageDir();
|
||||
final File expectedOutputFile = generateOutputFileForUnknownFilename(extension, TEST_TIMESTAMP, outputDir);
|
||||
|
||||
verifyAttachmentSavedCorrectly(null, contentType, outputDir, expectedOutputFile);
|
||||
|
||||
assertTrue(expectedOutputFile.delete());
|
||||
}
|
||||
|
||||
public void testDoInBackground_emptyAudioAttachmentWithFileNameIsCorrectlySaved()
|
||||
throws IOException, NoExternalStorageException
|
||||
{
|
||||
final String name = "testAudioThatShouldNotAlreadyExist";
|
||||
final String extension = "mp3";
|
||||
final String outputFileName = name + "." + extension;
|
||||
final String contentType = "audio/";
|
||||
final File outputDir = StorageUtil.getAudioDir();
|
||||
final File expectedOutputFile = generateOutputFileForKnownFilename(name, extension, outputDir);
|
||||
|
||||
verifyAttachmentSavedCorrectly(outputFileName, contentType, outputDir, expectedOutputFile);
|
||||
|
||||
assertTrue(expectedOutputFile.delete());
|
||||
}
|
||||
|
||||
public void testDoInBackground_emptyVideoAttachmentWithFileNameIsCorrectlySaved()
|
||||
throws IOException, NoExternalStorageException
|
||||
{
|
||||
final String name = "testVideoThatShouldNotAlreadyExist";
|
||||
final String extension = "mp4";
|
||||
final String outputFileName = name + "." + extension;
|
||||
final String contentType = "video/";
|
||||
final File outputDir = StorageUtil.getVideoDir();
|
||||
final File expectedOutputFile = generateOutputFileForKnownFilename(name, extension, outputDir);
|
||||
|
||||
verifyAttachmentSavedCorrectly(outputFileName, contentType, outputDir, expectedOutputFile);
|
||||
|
||||
assertTrue(expectedOutputFile.delete());
|
||||
}
|
||||
|
||||
public void testDoInBackground_emptyUnknownAttachmentWithFileNameIsCorrectlySaved()
|
||||
throws IOException, NoExternalStorageException
|
||||
{
|
||||
final String name = "testFileThatShouldNotAlreadyExist";
|
||||
final String extension = "rand";
|
||||
final String outputFileName = name + "." + extension;
|
||||
final String contentType = "somethingweird/";
|
||||
final File outputDir = StorageUtil.getDownloadDir();
|
||||
final File expectedOutputFile = generateOutputFileForKnownFilename(name, extension, outputDir);
|
||||
|
||||
verifyAttachmentSavedCorrectly(outputFileName, contentType, outputDir, expectedOutputFile);
|
||||
|
||||
assertTrue(expectedOutputFile.delete());
|
||||
}
|
||||
|
||||
private ArrayList<File> populateWithTestFiles(String name, String extension, final File outputDir)
|
||||
throws IOException
|
||||
{
|
||||
ArrayList<File> testFiles = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
File tmpFile = generateOutputFileForKnownFilename(name, extension, outputDir);
|
||||
if (tmpFile.createNewFile()) {
|
||||
testFiles.add(tmpFile);
|
||||
}
|
||||
}
|
||||
|
||||
return testFiles;
|
||||
}
|
||||
|
||||
private File generateOutputFileForKnownFilename(String name,
|
||||
String extension,
|
||||
final File outputDir)
|
||||
{
|
||||
final String outputFileName = guessOutputFileNameIndex(name, extension, outputDir);
|
||||
final File outputFile = new File(outputDir, outputFileName);
|
||||
|
||||
assertFalse(outputFile.exists());
|
||||
return outputFile;
|
||||
}
|
||||
|
||||
private String guessOutputFileNameIndex(String name, String extension, final File outputDir) {
|
||||
final File outputFile = new File(outputDir, name + "." + extension);
|
||||
|
||||
if (outputFile.exists()) {
|
||||
String newName;
|
||||
|
||||
if (name.charAt(name.length() - 2) == '-') {
|
||||
int newIndex = Integer.parseInt("" + name.charAt(name.length() - 1)) + 1;
|
||||
newName = name.substring(0, name.length() - 1) + newIndex;
|
||||
} else {
|
||||
newName = name + "-1";
|
||||
}
|
||||
|
||||
return guessOutputFileNameIndex(newName, extension, outputDir);
|
||||
} else {
|
||||
return name + "." + extension;
|
||||
}
|
||||
}
|
||||
|
||||
private void verifyAttachmentSavedCorrectly(String outputFileName,
|
||||
String contentType,
|
||||
final File outputDir,
|
||||
final File expectedOutputFile)
|
||||
throws IOException
|
||||
{
|
||||
final File testFile = createEmptyTempFile("testFile", "ext");
|
||||
final SaveAttachmentTask.Attachment attachment
|
||||
= new SaveAttachmentTask.Attachment(Uri.fromFile(testFile),
|
||||
contentType,
|
||||
TEST_TIMESTAMP,
|
||||
outputFileName);
|
||||
|
||||
Pair<Integer, File> result = saveAttachmentTask.doInBackground(attachment);
|
||||
|
||||
assertTrue(result.first() == SaveAttachmentTask.SUCCESS);
|
||||
assertEquals(result.second().getAbsolutePath(), outputDir.getAbsolutePath());
|
||||
assertTrue(expectedOutputFile.exists());
|
||||
}
|
||||
|
||||
private File createEmptyTempFile(String fileName, String extension) throws IOException
|
||||
{
|
||||
String fullName = fileName + "." + extension;
|
||||
File file = new File(getInstrumentation().getTargetContext().getCacheDir(), fullName);
|
||||
|
||||
if (file.exists()) {
|
||||
file = createEmptyTempFile(fileName + "-" + System.currentTimeMillis(), extension);
|
||||
} else {
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
private File generateOutputFileForUnknownFilename(String extension,
|
||||
long date,
|
||||
final File outputDir)
|
||||
{
|
||||
if (extension == null) extension = "attach";
|
||||
|
||||
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
|
||||
String base = "signal-" + dateFormatter.format(date);
|
||||
|
||||
final String outputFileName = guessOutputFileNameIndex(base, extension, outputDir);
|
||||
final File outputFile = new File(outputDir, outputFileName);
|
||||
|
||||
assertFalse(outputFile.exists());
|
||||
return outputFile;
|
||||
}
|
||||
|
||||
private class TestSaveAttachmentTask extends SaveAttachmentTask {
|
||||
private TestSaveAttachmentTask(Context context, MasterSecret masterSecret, View view)
|
||||
{
|
||||
super(context, masterSecret, view);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user