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 String sha1sum;
13	private byte[] aeskey;
14
15	private byte[] iv = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
16			0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0xf };
17
18	public DownloadableFile(String path) {
19		super(path);
20	}
21
22	public long getSize() {
23		return super.length();
24	}
25
26	public long getExpectedSize() {
27		if (this.aeskey != null) {
28			if (this.expectedSize == 0) {
29				return 0;
30			} else {
31				return (this.expectedSize / 16 + 1) * 16;
32			}
33		} else {
34			return this.expectedSize;
35		}
36	}
37
38	public String getMimeType() {
39		String path = this.getAbsolutePath();
40		int start = path.lastIndexOf('.') + 1;
41		if (start < path.length()) {
42			String mime = MimeUtils.guessMimeTypeFromExtension(path.substring(start));
43			return mime == null ? "" : mime;
44		} else {
45			return "";
46		}
47	}
48
49	public void setExpectedSize(long size) {
50		this.expectedSize = size;
51	}
52
53	public String getSha1Sum() {
54		return this.sha1sum;
55	}
56
57	public void setSha1Sum(String sum) {
58		this.sha1sum = sum;
59	}
60
61	public void setKey(byte[] key) {
62		if (key.length == 48) {
63			byte[] secretKey = new byte[32];
64			byte[] iv = new byte[16];
65			System.arraycopy(key, 0, iv, 0, 16);
66			System.arraycopy(key, 16, secretKey, 0, 32);
67			this.aeskey = secretKey;
68			this.iv = iv;
69		} else if (key.length >= 32) {
70			byte[] secretKey = new byte[32];
71			System.arraycopy(key, 0, secretKey, 0, 32);
72			this.aeskey = secretKey;
73		} else if (key.length >= 16) {
74			byte[] secretKey = new byte[16];
75			System.arraycopy(key, 0, secretKey, 0, 16);
76			this.aeskey = secretKey;
77		}
78	}
79
80	public byte[] getKey() {
81		return this.aeskey;
82	}
83
84	public byte[] getIv() {
85		return this.iv;
86	}
87}