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 System.arraycopy(keyIvCombo, 0, aeskey, 0, 32);
67 }
68 }
69
70 public void setKey(byte[] key) {
71 this.aeskey = key;
72 }
73
74 public void setIv(byte[] iv) {
75 this.iv = iv;
76 }
77
78 public byte[] getKey() {
79 return this.aeskey;
80 }
81
82 public byte[] getIv() {
83 return this.iv;
84 }
85}