Avatar.java

  1package eu.siacs.conversations.xmpp.pep;
  2
  3import android.util.Base64;
  4
  5import java.security.NoSuchAlgorithmException;
  6
  7import io.ipfs.cid.Cid;
  8
  9import eu.siacs.conversations.utils.CryptoHelper;
 10import eu.siacs.conversations.xml.Element;
 11import eu.siacs.conversations.xmpp.Jid;
 12
 13public class Avatar {
 14
 15	public enum Origin { PEP, VCARD }
 16
 17    public String type;
 18	public String sha1sum;
 19	public String image;
 20	public int height;
 21	public int width;
 22	public long size;
 23	public Jid owner;
 24	public Origin origin = Origin.PEP; //default to maintain compat
 25
 26	public byte[] getImageAsBytes() {
 27		return Base64.decode(image, Base64.DEFAULT);
 28	}
 29
 30	public String getFilename() {
 31		return sha1sum;
 32	}
 33
 34	public static Avatar parseMetadata(Element items) {
 35		Element item = items.findChild("item");
 36		if (item == null) {
 37			return null;
 38		}
 39		Element metadata = item.findChild("metadata");
 40		if (metadata == null) {
 41			return null;
 42		}
 43		String primaryId = item.getAttribute("id");
 44		if (primaryId == null) {
 45			return null;
 46		}
 47		for (Element child : metadata.getChildren()) {
 48			if (child.getName().equals("info")
 49					&& primaryId.equals(child.getAttribute("id"))) {
 50				Avatar avatar = new Avatar();
 51				String height = child.getAttribute("height");
 52				String width = child.getAttribute("width");
 53				String size = child.getAttribute("bytes");
 54				try {
 55					if (height != null) {
 56						avatar.height = Integer.parseInt(height);
 57					}
 58					if (width != null) {
 59						avatar.width = Integer.parseInt(width);
 60					}
 61					if (size != null) {
 62						avatar.size = Long.parseLong(size);
 63					}
 64				} catch (NumberFormatException e) {
 65					return null;
 66				}
 67				avatar.type = child.getAttribute("type");
 68				String hash = child.getAttribute("id");
 69				if (!isValidSHA1(hash)) {
 70					return null;
 71				}
 72				avatar.sha1sum = hash;
 73				avatar.origin = Origin.PEP;
 74				return avatar;
 75			}
 76		}
 77		return null;
 78	}
 79
 80	@Override
 81	public boolean equals(Object object) {
 82		if (object != null && object instanceof Avatar) {
 83			Avatar other = (Avatar) object;
 84			return other.getFilename().equals(this.getFilename());
 85		} else {
 86			return false;
 87		}
 88	}
 89
 90	public Cid cid() {
 91		if (sha1sum == null) return null;
 92
 93		try {
 94			return CryptoHelper.cid(CryptoHelper.hexToBytes(sha1sum), "sha-1");
 95		} catch (final NoSuchAlgorithmException e) {
 96			return null;
 97		}
 98	}
 99
100	public static Avatar parsePresence(Element x) {
101		String hash = x == null ? null : x.findChildContent("photo");
102		if (hash == null) {
103			return null;
104		}
105		if (!isValidSHA1(hash)) {
106			return null;
107		}
108		Avatar avatar = new Avatar();
109		avatar.sha1sum = hash;
110		avatar.origin = Origin.VCARD;
111		return avatar;
112	}
113
114	private static boolean isValidSHA1(String s) {
115		return s != null && s.matches("[a-fA-F0-9]{40}");
116	}
117}