fixed avatar republish missing the mime type

This commit is contained in:
Daniel Gultsch 2017-01-12 12:20:10 +01:00
parent f0c3b31a42
commit 2c1d3ef968
3 changed files with 14 additions and 15 deletions

View File

@ -23,7 +23,6 @@ import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.jid.Jid;

View File

@ -555,6 +555,7 @@ public class FileBackend {
File file = new File(getAvatarPath(hash));
FileInputStream is = null;
try {
avatar.size = file.length();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
@ -574,6 +575,7 @@ public class FileBackend {
avatar.image = new String(mByteArrayOutputStream.toByteArray());
avatar.height = options.outHeight;
avatar.width = options.outWidth;
avatar.type = options.outMimeType;
return avatar;
} catch (IOException e) {
return null;
@ -593,6 +595,7 @@ public class FileBackend {
File file;
if (isAvatarCached(avatar)) {
file = new File(getAvatarPath(avatar.getFilename()));
avatar.size = file.length();
} else {
String filename = getAvatarPath(avatar.getFilename());
file = new File(filename + ".tmp");
@ -604,7 +607,8 @@ public class FileBackend {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.reset();
DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
mDigestOutputStream.write(avatar.getImageAsBytes());
final byte[] bytes = avatar.getImageAsBytes();
mDigestOutputStream.write(bytes);
mDigestOutputStream.flush();
mDigestOutputStream.close();
String sha1sum = CryptoHelper.bytesToHex(digest.digest());
@ -615,13 +619,13 @@ public class FileBackend {
file.delete();
return false;
}
avatar.size = bytes.length;
} catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
return false;
} finally {
close(os);
}
}
avatar.size = file.length();
return true;
}
@ -691,7 +695,7 @@ public class FileBackend {
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
if (source != null && !source.isRecycled()) {
if (source.isRecycled()) {
source.recycle();
}
return dest;

View File

@ -2639,14 +2639,13 @@ public class XmppConnectionService extends Service {
}
public void publishAvatar(Account account, final Avatar avatar, final UiCallback<Avatar> callback) {
final IqPacket packet = this.mIqGenerator.publishAvatar(avatar);
IqPacket packet = this.mIqGenerator.publishAvatar(avatar);
this.sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket result) {
if (result.getType() == IqPacket.TYPE.RESULT) {
final IqPacket packet = XmppConnectionService.this.mIqGenerator
.publishAvatarMetadata(avatar);
final IqPacket packet = XmppConnectionService.this.mIqGenerator.publishAvatarMetadata(avatar);
sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket result) {
@ -2655,25 +2654,22 @@ public class XmppConnectionService extends Service {
getAvatarService().clear(account);
databaseBackend.updateAccount(account);
}
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": published avatar "+(avatar.size/1024)+"KiB");
if (callback != null) {
callback.success(avatar);
} else {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": published avatar");
}
} else {
if (callback != null) {
callback.error(
R.string.error_publish_avatar_server_reject,
avatar);
callback.error(R.string.error_publish_avatar_server_reject,avatar);
}
}
}
});
} else {
Element error = result.findChild("error");
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": server rejected avatar "+(avatar.size/1024)+"KiB "+(error!=null?error.toString():""));
if (callback != null) {
callback.error(
R.string.error_publish_avatar_server_reject,
avatar);
callback.error(R.string.error_publish_avatar_server_reject, avatar);
}
}
}