DownloadableFile.java

 1package eu.siacs.conversations.entities;
 2
 3import java.io.File;
 4
 5import eu.siacs.conversations.utils.MimeUtils;
 6
 7public class DownloadableFile extends File {
 8
 9	private static final long serialVersionUID = 2247012619505115863L;
10
11	private long expectedSize = 0;
12	private byte[] sha1sum;
13	private byte[] aeskey;
14	private byte[] iv;
15
16	public DownloadableFile(String path) {
17		super(path);
18	}
19
20	public long getSize() {
21		return super.length();
22	}
23
24	public long getExpectedSize() {
25		return this.expectedSize;
26	}
27
28	public String getMimeType() {
29		String path = this.getAbsolutePath();
30		int start = path.lastIndexOf('.') + 1;
31		if (start < path.length()) {
32			String mime = MimeUtils.guessMimeTypeFromExtension(path.substring(start));
33			return mime == null ? "" : mime;
34		} else {
35			return "";
36		}
37	}
38
39	public void setExpectedSize(long size) {
40		this.expectedSize = size;
41	}
42
43	public byte[] getSha1Sum() {
44		return this.sha1sum;
45	}
46
47	public void setSha1Sum(byte[] sum) {
48		this.sha1sum = sum;
49	}
50
51	public void setKeyAndIv(byte[] keyIvCombo) {
52		// originally, we used a 16 byte IV, then found for aes-gcm a 12 byte IV is ideal
53		// this code supports reading either length, with sending 12 byte IV to be done in future
54		if (keyIvCombo.length == 48) {
55			this.aeskey = new byte[32];
56			this.iv = new byte[16];
57			System.arraycopy(keyIvCombo, 0, this.iv, 0, 16);
58			System.arraycopy(keyIvCombo, 16, this.aeskey, 0, 32);
59		} else if (keyIvCombo.length == 44) {
60			this.aeskey = new byte[32];
61			this.iv = new byte[12];
62			System.arraycopy(keyIvCombo, 0, this.iv, 0, 12);
63			System.arraycopy(keyIvCombo, 12, this.aeskey, 0, 32);
64		} else if (keyIvCombo.length >= 32) {
65			this.aeskey = new byte[32];
66			this.iv = new byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0xf };
67			System.arraycopy(keyIvCombo, 0, aeskey, 0, 32);
68		}
69	}
70
71	public void setKey(byte[] key) {
72		this.aeskey = key;
73	}
74
75	public void setIv(byte[] iv) {
76		this.iv = iv;
77	}
78
79	public byte[] getKey() {
80		return this.aeskey;
81	}
82
83	public byte[] getIv() {
84		return this.iv;
85	}
86}