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 javax.crypto.spec.SecretKeySpec;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;

View file

@ -555,6 +555,7 @@ public class FileBackend {
File file = new File(getAvatarPath(hash)); File file = new File(getAvatarPath(hash));
FileInputStream is = null; FileInputStream is = null;
try { try {
avatar.size = file.length();
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options); BitmapFactory.decodeFile(file.getAbsolutePath(), options);
@ -574,6 +575,7 @@ public class FileBackend {
avatar.image = new String(mByteArrayOutputStream.toByteArray()); avatar.image = new String(mByteArrayOutputStream.toByteArray());
avatar.height = options.outHeight; avatar.height = options.outHeight;
avatar.width = options.outWidth; avatar.width = options.outWidth;
avatar.type = options.outMimeType;
return avatar; return avatar;
} catch (IOException e) { } catch (IOException e) {
return null; return null;
@ -593,6 +595,7 @@ public class FileBackend {
File file; File file;
if (isAvatarCached(avatar)) { if (isAvatarCached(avatar)) {
file = new File(getAvatarPath(avatar.getFilename())); file = new File(getAvatarPath(avatar.getFilename()));
avatar.size = file.length();
} else { } else {
String filename = getAvatarPath(avatar.getFilename()); String filename = getAvatarPath(avatar.getFilename());
file = new File(filename + ".tmp"); file = new File(filename + ".tmp");
@ -604,7 +607,8 @@ public class FileBackend {
MessageDigest digest = MessageDigest.getInstance("SHA-1"); MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.reset(); digest.reset();
DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest); DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
mDigestOutputStream.write(avatar.getImageAsBytes()); final byte[] bytes = avatar.getImageAsBytes();
mDigestOutputStream.write(bytes);
mDigestOutputStream.flush(); mDigestOutputStream.flush();
mDigestOutputStream.close(); mDigestOutputStream.close();
String sha1sum = CryptoHelper.bytesToHex(digest.digest()); String sha1sum = CryptoHelper.bytesToHex(digest.digest());
@ -615,13 +619,13 @@ public class FileBackend {
file.delete(); file.delete();
return false; return false;
} }
avatar.size = bytes.length;
} catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) { } catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
return false; return false;
} finally { } finally {
close(os); close(os);
} }
} }
avatar.size = file.length();
return true; return true;
} }
@ -691,7 +695,7 @@ public class FileBackend {
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888); Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(dest); Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null); canvas.drawBitmap(source, null, targetRect, null);
if (source != null && !source.isRecycled()) { if (source.isRecycled()) {
source.recycle(); source.recycle();
} }
return dest; 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) { 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() { this.sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket result) { public void onIqPacketReceived(Account account, IqPacket result) {
if (result.getType() == IqPacket.TYPE.RESULT) { if (result.getType() == IqPacket.TYPE.RESULT) {
final IqPacket packet = XmppConnectionService.this.mIqGenerator final IqPacket packet = XmppConnectionService.this.mIqGenerator.publishAvatarMetadata(avatar);
.publishAvatarMetadata(avatar);
sendIqPacket(account, packet, new OnIqPacketReceived() { sendIqPacket(account, packet, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket result) { public void onIqPacketReceived(Account account, IqPacket result) {
@ -2655,25 +2654,22 @@ public class XmppConnectionService extends Service {
getAvatarService().clear(account); getAvatarService().clear(account);
databaseBackend.updateAccount(account); databaseBackend.updateAccount(account);
} }
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": published avatar "+(avatar.size/1024)+"KiB");
if (callback != null) { if (callback != null) {
callback.success(avatar); callback.success(avatar);
} else {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": published avatar");
} }
} else { } else {
if (callback != null) { if (callback != null) {
callback.error( callback.error(R.string.error_publish_avatar_server_reject,avatar);
R.string.error_publish_avatar_server_reject,
avatar);
} }
} }
} }
}); });
} else { } 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) { if (callback != null) {
callback.error( callback.error(R.string.error_publish_avatar_server_reject, avatar);
R.string.error_publish_avatar_server_reject,
avatar);
} }
} }
} }