diff options
author | iNPUTmice <daniel@gultsch.de> | 2014-08-06 18:36:33 +0200 |
---|---|---|
committer | iNPUTmice <daniel@gultsch.de> | 2014-08-06 18:36:33 +0200 |
commit | 2b4d9e8b60bdec97cbeedf070188efd6380e2332 (patch) | |
tree | ee65a70764696acb0eb906d2427dd203122b4a9b /src | |
parent | ba63727f50c192e63e171bedca92d7003981800c (diff) |
fixed problems with sha sum calculation on avatar write. delete avatars with non matching sha sum
Diffstat (limited to 'src')
3 files changed, 42 insertions, 20 deletions
diff --git a/src/eu/siacs/conversations/persistance/FileBackend.java b/src/eu/siacs/conversations/persistance/FileBackend.java index c371e039..03ea8753 100644 --- a/src/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/eu/siacs/conversations/persistance/FileBackend.java @@ -1,6 +1,5 @@ package eu.siacs.conversations.persistance; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; @@ -239,12 +238,18 @@ public class FileBackend { Base64OutputStream mBase64OutputSttream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT); MessageDigest digest = MessageDigest.getInstance("SHA-1"); DigestOutputStream mDigestOutputStream = new DigestOutputStream(mBase64OutputSttream, digest); - bm.compress(format, 75, mDigestOutputStream); + if (!bm.compress(format, 75, mDigestOutputStream)) { + return null; + } + mDigestOutputStream.flush(); + mDigestOutputStream.close(); avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest()); avatar.image = new String(mByteArrayOutputStream.toByteArray()); return avatar; } catch (NoSuchAlgorithmException e) { return null; + } catch (IOException e) { + return null; } } @@ -253,26 +258,38 @@ public class FileBackend { return file.exists(); } - public void save(Avatar avatar) { - File file = new File(getAvatarPath(context, avatar.getFilename())); + public boolean save(Avatar avatar) { + if (isAvatarCached(avatar)) { + return true; + } + String filename = getAvatarPath(context, avatar.getFilename()); + File file = new File(filename+".tmp"); file.getParentFile().mkdirs(); - Log.d("xmppService",file.getAbsolutePath()); try { file.createNewFile(); FileOutputStream mFileOutputStream = new FileOutputStream(file); MessageDigest digest = MessageDigest.getInstance("SHA-1"); + digest.reset(); DigestOutputStream mDigestOutputStream = new DigestOutputStream(mFileOutputStream, digest); mDigestOutputStream.write(avatar.getImageAsBytes()); mDigestOutputStream.flush(); mDigestOutputStream.close(); avatar.size = file.length(); + String sha1sum = CryptoHelper.bytesToHex(digest.digest()); + if (sha1sum.equals(avatar.sha1sum)) { + file.renameTo(new File(filename)); + return true; + } else { + Log.d("xmppService","sha1sum mismatch for "+avatar.owner); + file.delete(); + return false; + } } catch (FileNotFoundException e) { - + return false; } catch (IOException e) { - Log.d("xmppService",e.getMessage()); + return false; } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + return false; } } diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index 71f2cfc2..d94f20da 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -1201,7 +1201,10 @@ public class XmppConnectionService extends Service { } else if (format.equals(Bitmap.CompressFormat.PNG)) { avatar.type = "image/png"; } - getFileBackend().save(avatar); + if (!getFileBackend().save(avatar)) { + callback.error(R.string.error_saving_avatar, avatar); + return; + } IqPacket packet = this.mIqGenerator.publishAvatar(avatar); this.sendIqPacket(account, packet, new OnIqPacketReceived() { @@ -1232,6 +1235,7 @@ public class XmppConnectionService extends Service { } public void fetchAvatar(Account account, final Avatar avatar) { + Log.d(LOGTAG,account.getJid()+": retrieving avatar for "+avatar.owner); IqPacket packet = this.mIqGenerator.retrieveAvatar(avatar); sendIqPacket(account, packet, new OnIqPacketReceived() { @@ -1239,9 +1243,14 @@ public class XmppConnectionService extends Service { public void onIqPacketReceived(Account account, IqPacket result) { avatar.image = mIqParser.avatarData(result); if (avatar.image!=null) { - getFileBackend().save(avatar); - Contact contact = account.getRoster().getContact(avatar.owner); - contact.setAvatar(avatar.getFilename()); + if (getFileBackend().save(avatar)) { + if (account.getJid().equals(avatar.owner)) { + account.setAvatar(avatar.getFilename()); + } else { + Contact contact = account.getRoster().getContact(avatar.owner); + contact.setAvatar(avatar.getFilename()); + } + } } } }); diff --git a/src/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/eu/siacs/conversations/ui/adapter/MessageAdapter.java index c184e0e0..9aca8fab 100644 --- a/src/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -451,14 +451,10 @@ public class MessageAdapter extends ArrayAdapter<Message> { private HashMap<String, Bitmap> unknownBitmaps = new HashMap<String, Bitmap>(); public Bitmap get(Contact contact, Context context) { - if (contactBitmaps.containsKey(contact.getJid())) { - return contactBitmaps.get(contact.getJid()); - } else { - Bitmap bm = UIHelper.getContactPicture(contact, 48, context, - false); - contactBitmaps.put(contact.getJid(), bm); - return bm; + if (!contactBitmaps.containsKey(contact.getJid())) { + contactBitmaps.put(contact.getJid(), contact.getImage(48, context)); } + return contactBitmaps.get(contact.getJid()); } public Bitmap get(String name, Context context) { |