Address possible issues with AvatarMigrationJob.
This commit is contained in:
parent
f9f9ae68f5
commit
59d03cbeb2
2 changed files with 22 additions and 6 deletions
|
@ -6,13 +6,16 @@ import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||||
import org.thoughtcrime.securesms.jobmanager.Data;
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
||||||
import org.thoughtcrime.securesms.jobmanager.Job;
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
||||||
import org.thoughtcrime.securesms.logging.Log;
|
import org.thoughtcrime.securesms.logging.Log;
|
||||||
|
import org.thoughtcrime.securesms.phonenumbers.NumberUtil;
|
||||||
import org.thoughtcrime.securesms.profiles.AvatarHelper;
|
import org.thoughtcrime.securesms.profiles.AvatarHelper;
|
||||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||||
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
||||||
import org.thoughtcrime.securesms.util.Util;
|
import org.thoughtcrime.securesms.util.Util;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Previously, we used a recipient's address as the filename for their avatar. We want to use
|
* Previously, we used a recipient's address as the filename for their avatar. We want to use
|
||||||
|
@ -24,6 +27,8 @@ public class AvatarMigrationJob extends MigrationJob {
|
||||||
|
|
||||||
private static final String TAG = Log.tag(AvatarMigrationJob.class);
|
private static final String TAG = Log.tag(AvatarMigrationJob.class);
|
||||||
|
|
||||||
|
private static final Pattern NUMBER_PATTERN = Pattern.compile("^[0-9\\-+]+$");
|
||||||
|
|
||||||
AvatarMigrationJob() {
|
AvatarMigrationJob() {
|
||||||
this(new Parameters.Builder().build());
|
this(new Parameters.Builder().build());
|
||||||
}
|
}
|
||||||
|
@ -47,22 +52,29 @@ public class AvatarMigrationJob extends MigrationJob {
|
||||||
File oldDirectory = new File(context.getFilesDir(), "avatars");
|
File oldDirectory = new File(context.getFilesDir(), "avatars");
|
||||||
File[] files = oldDirectory.listFiles();
|
File[] files = oldDirectory.listFiles();
|
||||||
|
|
||||||
|
if (files == null) {
|
||||||
|
Log.w(TAG, "Unable to read directory, and therefore unable to migrate any avatars.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Log.i(TAG, "Preparing to move " + files.length + " avatars.");
|
Log.i(TAG, "Preparing to move " + files.length + " avatars.");
|
||||||
|
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
try {
|
try {
|
||||||
|
if (isValidFileName(file.getName())) {
|
||||||
Recipient recipient = Recipient.external(context, file.getName());
|
Recipient recipient = Recipient.external(context, file.getName());
|
||||||
byte[] data = Util.readFully(new FileInputStream(file));
|
byte[] data = Util.readFully(new FileInputStream(file));
|
||||||
|
|
||||||
AvatarHelper.setAvatar(context, recipient.getId(), data);
|
AvatarHelper.setAvatar(context, recipient.getId(), data);
|
||||||
|
} else {
|
||||||
|
Log.w(TAG, "Invalid file name! Can't migrate this file. It'll just get deleted.");
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.w(TAG, "Failed to copy avatar file. Skipping it.", e);
|
Log.w(TAG, "Failed to copy avatar file. Skipping it.", e);
|
||||||
} finally {
|
} finally {
|
||||||
file.delete();
|
file.delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
oldDirectory.delete();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -70,6 +82,10 @@ public class AvatarMigrationJob extends MigrationJob {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isValidFileName(@NonNull String name) {
|
||||||
|
return NUMBER_PATTERN.matcher(name).matches() || GroupUtil.isEncodedGroup(name) || NumberUtil.isValidEmail(name);
|
||||||
|
}
|
||||||
|
|
||||||
public static class Factory implements Job.Factory<AvatarMigrationJob> {
|
public static class Factory implements Job.Factory<AvatarMigrationJob> {
|
||||||
@Override
|
@Override
|
||||||
public @NonNull AvatarMigrationJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
public @NonNull AvatarMigrationJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
||||||
|
|
|
@ -41,7 +41,7 @@ abstract class MigrationJob extends Job {
|
||||||
return Result.success();
|
return Result.success();
|
||||||
} catch (RuntimeException e) {
|
} catch (RuntimeException e) {
|
||||||
Log.w(TAG, JobLogger.format(this, "Encountered a runtime exception."), e);
|
Log.w(TAG, JobLogger.format(this, "Encountered a runtime exception."), e);
|
||||||
throw e;
|
throw new FailedMigrationError(e);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (shouldRetry(e)) {
|
if (shouldRetry(e)) {
|
||||||
Log.w(TAG, JobLogger.format(this, "Encountered a retryable exception."), e);
|
Log.w(TAG, JobLogger.format(this, "Encountered a retryable exception."), e);
|
||||||
|
|
Loading…
Add table
Reference in a new issue