Refactor storage management to have a centralized,
clearer way to get the Signal output directories Closes #6476 // FREEBIEpull/1/head
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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