create avatar path before saving avatar

This commit is contained in:
Daniel Gultsch 2018-04-18 12:41:16 +02:00
parent 146096f00a
commit 5272421678
1 changed files with 17 additions and 4 deletions

View File

@ -665,10 +665,14 @@ public class FileBackend {
avatar.size = file.length(); avatar.size = file.length();
} else { } else {
file = new File(mXmppConnectionService.getCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString()); file = new File(mXmppConnectionService.getCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString());
file.getParentFile().mkdirs(); if (file.getParentFile().mkdirs()) {
Log.d(Config.LOGTAG,"created cache directory");
}
OutputStream os = null; OutputStream os = null;
try { try {
file.createNewFile(); if (!file.createNewFile()) {
Log.d(Config.LOGTAG,"unable to create temporary file "+file.getAbsolutePath());
}
os = new FileOutputStream(file); os = new FileOutputStream(file);
MessageDigest digest = MessageDigest.getInstance("SHA-1"); MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.reset(); digest.reset();
@ -679,11 +683,20 @@ public class FileBackend {
mDigestOutputStream.close(); mDigestOutputStream.close();
String sha1sum = CryptoHelper.bytesToHex(digest.digest()); String sha1sum = CryptoHelper.bytesToHex(digest.digest());
if (sha1sum.equals(avatar.sha1sum)) { if (sha1sum.equals(avatar.sha1sum)) {
File outputFile = new File(getAvatarPath(avatar.getFilename()));
if (outputFile.getParentFile().mkdirs()) {
Log.d(Config.LOGTAG,"created avatar directory");
}
String filename = getAvatarPath(avatar.getFilename()); String filename = getAvatarPath(avatar.getFilename());
file.renameTo(new File(filename)); if (!file.renameTo(new File(filename))) {
Log.d(Config.LOGTAG,"unable to rename "+file.getAbsolutePath()+" to "+outputFile);
return false;
}
} else { } else {
Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner); Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
file.delete(); if (!file.delete()) {
Log.d(Config.LOGTAG,"unable to delete temporary file");
}
return false; return false;
} }
avatar.size = bytes.length; avatar.size = bytes.length;