2019-09-23 17:37:01 +02:00
|
|
|
package org.thoughtcrime.securesms.migrations;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
2020-03-26 15:00:17 +01:00
|
|
|
import org.thoughtcrime.securesms.groups.GroupId;
|
2019-09-23 17:37:01 +02:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2019-10-18 17:07:49 +02:00
|
|
|
import org.thoughtcrime.securesms.phonenumbers.NumberUtil;
|
2019-09-23 17:37:01 +02:00
|
|
|
import org.thoughtcrime.securesms.profiles.AvatarHelper;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
|
2020-03-26 20:38:27 +01:00
|
|
|
import java.io.ByteArrayInputStream;
|
2019-09-23 17:37:01 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
2019-10-18 17:07:49 +02:00
|
|
|
import java.util.regex.Pattern;
|
2019-09-23 17:37:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Previously, we used a recipient's address as the filename for their avatar. We want to use
|
|
|
|
* recipientId's instead in preparation for UUIDs.
|
|
|
|
*/
|
|
|
|
public class AvatarMigrationJob extends MigrationJob {
|
|
|
|
|
|
|
|
public static final String KEY = "AvatarMigrationJob";
|
|
|
|
|
|
|
|
private static final String TAG = Log.tag(AvatarMigrationJob.class);
|
|
|
|
|
2019-10-18 17:07:49 +02:00
|
|
|
private static final Pattern NUMBER_PATTERN = Pattern.compile("^[0-9\\-+]+$");
|
|
|
|
|
2019-09-23 17:37:01 +02:00
|
|
|
AvatarMigrationJob() {
|
|
|
|
this(new Parameters.Builder().build());
|
|
|
|
}
|
|
|
|
|
|
|
|
private AvatarMigrationJob(@NonNull Parameters parameters) {
|
|
|
|
super(parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isUiBlocking() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void performMigration() {
|
|
|
|
File oldDirectory = new File(context.getFilesDir(), "avatars");
|
|
|
|
File[] files = oldDirectory.listFiles();
|
|
|
|
|
2019-10-18 17:07:49 +02:00
|
|
|
if (files == null) {
|
|
|
|
Log.w(TAG, "Unable to read directory, and therefore unable to migrate any avatars.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:37:01 +02:00
|
|
|
Log.i(TAG, "Preparing to move " + files.length + " avatars.");
|
|
|
|
|
|
|
|
for (File file : files) {
|
|
|
|
try {
|
2019-10-18 17:07:49 +02:00
|
|
|
if (isValidFileName(file.getName())) {
|
|
|
|
Recipient recipient = Recipient.external(context, file.getName());
|
|
|
|
byte[] data = Util.readFully(new FileInputStream(file));
|
|
|
|
|
2020-03-26 20:38:27 +01:00
|
|
|
AvatarHelper.setAvatar(context, recipient.getId(), new ByteArrayInputStream(data));
|
2019-10-18 17:07:49 +02:00
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Invalid file name! Can't migrate this file. It'll just get deleted.");
|
|
|
|
}
|
2019-09-23 17:37:01 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, "Failed to copy avatar file. Skipping it.", e);
|
|
|
|
} finally {
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
boolean shouldRetry(@NonNull Exception e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-18 17:07:49 +02:00
|
|
|
private static boolean isValidFileName(@NonNull String name) {
|
2020-03-26 15:00:17 +01:00
|
|
|
return NUMBER_PATTERN.matcher(name).matches() || GroupId.isEncodedGroup(name) || NumberUtil.isValidEmail(name);
|
2019-10-18 17:07:49 +02:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:37:01 +02:00
|
|
|
public static class Factory implements Job.Factory<AvatarMigrationJob> {
|
|
|
|
@Override
|
|
|
|
public @NonNull AvatarMigrationJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
|
|
|
return new AvatarMigrationJob(parameters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|