Revert "Revert "Fixed avatar caching for very long email addresses""

This reverts commit 796fd4d7d3.
This commit is contained in:
M66B 2022-12-30 14:22:17 +01:00
parent a47da021e2
commit 576fac5ffa
1 changed files with 21 additions and 8 deletions

View File

@ -256,6 +256,16 @@ public class ContactInfo {
ContactInfo info = new ContactInfo();
info.email = address.getAddress();
// Maximum file name length: 255
// Maximum email address length: 320 (<local part = 64> @ <domain part = 255>)
final String ekey;
if (TextUtils.isEmpty(info.email))
ekey = null;
else
ekey = (info.email.length() > 255
? info.email.substring(0, 255)
: info.email).toLowerCase(Locale.ROOT);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean avatars = prefs.getBoolean("avatars", true);
boolean prefer_contact = prefs.getBoolean("prefer_contact", false);
@ -331,7 +341,6 @@ public class ContactInfo {
}
final String domain = d.toLowerCase(Locale.ROOT);
final String email = info.email.toLowerCase(Locale.ROOT);
File dir = Helper.ensureExists(new File(context.getFilesDir(), "favicons"));
@ -339,12 +348,12 @@ public class ContactInfo {
// check cache
File[] files = null;
if (gravatars) {
File f = new File(dir, email + ".gravatar");
File f = new File(dir, ekey + ".gravatar");
if (f.exists())
files = new File[]{f};
}
if (files == null && libravatars) {
File f = new File(dir, email + ".libravatar");
File f = new File(dir, ekey + ".libravatar");
if (f.exists())
files = new File[]{f};
}
@ -384,10 +393,13 @@ public class ContactInfo {
}
}));
String email = info.email.toLowerCase(Locale.ROOT);
if (gravatars)
futures.add(Helper.getDownloadTaskExecutor().submit(Avatar.getGravatar(email, scaleToPixels, context)));
futures.add(Helper.getDownloadTaskExecutor()
.submit(Avatar.getGravatar(email, scaleToPixels, context)));
if (libravatars)
futures.add(Helper.getDownloadTaskExecutor().submit(Avatar.getLibravatar(email, scaleToPixels, context)));
futures.add(Helper.getDownloadTaskExecutor()
.submit(Avatar.getLibravatar(email, scaleToPixels, context)));
if (favicons) {
String host = domain;
@ -466,7 +478,7 @@ public class ContactInfo {
// Add to cache
File output = new File(dir,
(info.isEmailBased() ? email : domain) +
(info.isEmailBased() ? ekey : domain) +
"." + info.type +
(info.verified ? "_verified" : ""));
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(output))) {
@ -507,11 +519,12 @@ public class ContactInfo {
File[] files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String name) {
return name.startsWith(info.email);
return name.startsWith(ekey);
}
});
if (files != null && files.length == 1) {
Log.i("Generated from cache=" + files[0].getName());
files[0].setLastModified(new Date().getTime());
info.bitmap = BitmapFactory.decodeFile(files[0].getAbsolutePath());
info.type = Helper.getExtension(files[0].getName());
} else {
@ -528,7 +541,7 @@ public class ContactInfo {
}
// Add to cache
File output = new File(dir, info.email + "." + info.type);
File output = new File(dir, ekey + "." + info.type);
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(output))) {
info.bitmap.compress(Bitmap.CompressFormat.PNG, 90, os);
} catch (IOException ex) {