Skip to content

Commit

Permalink
Move BlobProvider storage out of cache and into internal storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Feb 11, 2021
1 parent 37d9718 commit 80f3504
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 15 deletions.
Expand Up @@ -31,6 +31,7 @@
import org.thoughtcrime.securesms.migrations.AvatarIdRemovalMigrationJob;
import org.thoughtcrime.securesms.migrations.AvatarMigrationJob;
import org.thoughtcrime.securesms.migrations.BackupNotificationMigrationJob;
import org.thoughtcrime.securesms.migrations.BlobStorageLocationMigrationJob;
import org.thoughtcrime.securesms.migrations.CachedAttachmentsMigrationJob;
import org.thoughtcrime.securesms.migrations.DatabaseMigrationJob;
import org.thoughtcrime.securesms.migrations.DirectoryRefreshMigrationJob;
Expand Down Expand Up @@ -149,6 +150,7 @@ public static Map<String, Job.Factory> getJobFactories(@NonNull Application appl
put(AvatarIdRemovalMigrationJob.KEY, new AvatarIdRemovalMigrationJob.Factory());
put(AvatarMigrationJob.KEY, new AvatarMigrationJob.Factory());
put(BackupNotificationMigrationJob.KEY, new BackupNotificationMigrationJob.Factory());
put(BlobStorageLocationMigrationJob.KEY, new BlobStorageLocationMigrationJob.Factory());
put(CachedAttachmentsMigrationJob.KEY, new CachedAttachmentsMigrationJob.Factory());
put(DatabaseMigrationJob.KEY, new DatabaseMigrationJob.Factory());
put(DirectoryRefreshMigrationJob.KEY, new DirectoryRefreshMigrationJob.Factory());
Expand Down
Expand Up @@ -40,7 +40,7 @@ public class ApplicationMigrations {

private static final int LEGACY_CANONICAL_VERSION = 455;

public static final int CURRENT_VERSION = 26;
public static final int CURRENT_VERSION = 27;

private static final class Version {
static final int LEGACY = 1;
Expand Down Expand Up @@ -69,6 +69,7 @@ private static final class Version {
static final int GV1_MIGRATION = 24;
static final int USER_NOTIFICATION = 25;
static final int DAY_BY_DAY_STICKERS = 26;
static final int BLOB_LOCATION = 27;
}

/**
Expand Down Expand Up @@ -291,6 +292,10 @@ private static LinkedHashMap<Integer, MigrationJob> getMigrationJobs(@NonNull Co
jobs.put(Version.DAY_BY_DAY_STICKERS, new StickerDayByDayMigrationJob());
}

if (lastSeenVersion < Version.BLOB_LOCATION) {
jobs.put(Version.BLOB_LOCATION, new BlobStorageLocationMigrationJob());
}

return jobs;
}

Expand Down
@@ -0,0 +1,77 @@
package org.thoughtcrime.securesms.migrations;

import android.content.Context;

import androidx.annotation.NonNull;

import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.jobmanager.Data;
import org.thoughtcrime.securesms.jobmanager.Job;

import java.io.File;

/**
* We moved files stored by {@link org.thoughtcrime.securesms.providers.BlobProvider} from the cache
* into internal storage, so we gotta move any existing multi-session files.
*/
public class BlobStorageLocationMigrationJob extends MigrationJob {

private static final String TAG = Log.tag(BlobStorageLocationMigrationJob.class);

public static final String KEY = "BlobStorageLocationMigrationJob";

BlobStorageLocationMigrationJob() {
this(new Job.Parameters.Builder().build());
}

private BlobStorageLocationMigrationJob(@NonNull Parameters parameters) {
super(parameters);
}

@Override
boolean isUiBlocking() {
return false;
}

@Override
void performMigration() {
File oldDirectory = new File(context.getCacheDir(), "multi_session_blobs");

File[] oldFiles = oldDirectory.listFiles();

if (oldFiles == null) {
Log.i(TAG, "No files to move.");
return;
}

Log.i(TAG, "Preparing to move " + oldFiles.length + " files.");

File newDirectory = context.getDir("multi_session_blobs", Context.MODE_PRIVATE);

for (File oldFile : oldFiles) {
if (oldFile.renameTo(new File(newDirectory, oldFile.getName()))) {
Log.i(TAG, "Successfully moved file: " + oldFile.getName());
} else {
Log.w(TAG, "Failed to move file! " + oldFile.getAbsolutePath());
}
}
}

@Override
boolean shouldRetry(@NonNull Exception e) {
return false;
}

@Override
public @NonNull String getFactoryKey() {
return KEY;
}

public static class Factory implements Job.Factory<BlobStorageLocationMigrationJob> {

@Override
public @NonNull BlobStorageLocationMigrationJob create(@NonNull Parameters parameters, @NonNull Data data) {
return new BlobStorageLocationMigrationJob(parameters);
}
}
}
Expand Up @@ -141,7 +141,7 @@ public BlobBuilder forData(@NonNull InputStream data, long fileSize) {
} else {
String id = uri.getPathSegments().get(ID_PATH_SEGMENT);
String directory = getDirectory(storageType);
File file = new File(getOrCreateCacheDirectory(context, directory), buildFileName(id));
File file = new File(getOrCreateDirectory(context, directory), buildFileName(id));

return getFileRepresentation.apply(file);
}
Expand All @@ -163,6 +163,8 @@ public synchronized void delete(@NonNull Context context, @NonNull Uri uri) {
return;
}

Log.d(TAG, "Deleting " + uri);

try {
StorageType storageType = StorageType.decode(uri.getPathSegments().get(STORAGE_TYPE_PATH_SEGMENT));

Expand All @@ -171,9 +173,11 @@ public synchronized void delete(@NonNull Context context, @NonNull Uri uri) {
} else {
String id = uri.getPathSegments().get(ID_PATH_SEGMENT);
String directory = getDirectory(storageType);
File file = new File(getOrCreateCacheDirectory(context, directory), buildFileName(id));
File file = new File(getOrCreateDirectory(context, directory), buildFileName(id));

if (!file.delete()) {
if (file.delete()) {
Log.d(TAG, "Successfully deleted " + uri);
} else {
throw new IOException("File wasn't deleted.");
}
}
Expand All @@ -186,9 +190,13 @@ public synchronized void delete(@NonNull Context context, @NonNull Uri uri) {
* Indicates a new app session has started, allowing old single-session blobs to be deleted.
*/
public synchronized void onSessionStart(@NonNull Context context) {
File directory = getOrCreateCacheDirectory(context, SINGLE_SESSION_DIRECTORY);
File directory = getOrCreateDirectory(context, SINGLE_SESSION_DIRECTORY);
for (File file : directory.listFiles()) {
file.delete();
if (file.delete()) {
Log.d(TAG, "Deleted single-session file: " + file.getName());
} else {
Log.w(TAG, "Failed to delete single-session file! " + file.getName());
}
}
}

Expand Down Expand Up @@ -266,7 +274,7 @@ public static boolean isAuthority(@NonNull Uri uri) {
{
AttachmentSecret attachmentSecret = AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret();
String directory = getDirectory(blobSpec.getStorageType());
File outputFile = new File(getOrCreateCacheDirectory(context, directory), buildFileName(blobSpec.id));
File outputFile = new File(getOrCreateDirectory(context, directory), buildFileName(blobSpec.id));
OutputStream outputStream = ModernEncryptingPartOutputStream.createFor(attachmentSecret, outputFile, true).second;

SignalExecutors.UNBOUNDED.execute(() -> {
Expand Down Expand Up @@ -310,13 +318,8 @@ public static boolean isAuthority(@NonNull Uri uri) {
.build();
}

private static File getOrCreateCacheDirectory(@NonNull Context context, @NonNull String directory) {
File file = new File(context.getCacheDir(), directory);
if (!file.exists()) {
file.mkdir();
}

return file;
private static File getOrCreateDirectory(@NonNull Context context, @NonNull String directory) {
return context.getDir(directory, Context.MODE_PRIVATE);
}

public class BlobBuilder {
Expand Down
Expand Up @@ -104,7 +104,8 @@ void getResolved(@NonNull List<Uri> uris, @NonNull Callback<Optional<ShareData>>
.forData(stream, size)
.withMimeType(mimeType)
.withFileName(fileName)
.createForMultipleSessionsOnDisk(context);
.createForSingleSessionOnDisk(context);
// TODO Convert to multi-session after file drafts are fixed.
}

return ShareData.forIntentData(blobUri, mimeType, true, isMmsSupported(context, mimeType, size));
Expand Down

0 comments on commit 80f3504

Please sign in to comment.