+
+ public File getAttachmentFile(long attachmentId) {
+ return new File(attachmentsPath + "/" + attachmentId);
+ }
+
+ private File retrieveAttachment(TextSecureAttachmentPointer pointer) throws IOException, InvalidMessageException {
+ final TextSecureMessageReceiver messageReceiver = new TextSecureMessageReceiver(URL, TRUST_STORE, username, password, signalingKey, USER_AGENT);
+
+ File tmpFile = File.createTempFile("ts_attach_" + pointer.getId(), ".tmp");
+ InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile);
+
+ new File(attachmentsPath).mkdirs();
+ File outputFile = getAttachmentFile(pointer.getId());
+ OutputStream output = null;
+ try {
+ output = new FileOutputStream(outputFile);
+ byte[] buffer = new byte[4096];
+ int read;
+
+ while ((read = input.read(buffer)) != -1) {
+ output.write(buffer, 0, read);
+ }
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ return null;
+ } finally {
+ if (output != null) {
+ output.close();
+ output = null;
+ }
+ if (!tmpFile.delete()) {
+ System.err.println("Failed to delete temp file: " + tmpFile);
+ }
+ }
+ if (pointer.getPreview().isPresent()) {
+ File previewFile = new File(outputFile + ".preview");
+ try {
+ output = new FileOutputStream(previewFile);
+ byte[] preview = pointer.getPreview().get();
+ output.write(preview, 0, preview.length);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ return null;
+ } finally {
+ if (output != null) {
+ output.close();
+ }
+ }
+ }
+ return outputFile;
+ }
+
+ public String canonicalizeNumber(String number) throws InvalidNumberException {
+ String localNumber = username;
+ return PhoneNumberFormatter.formatNumber(number, localNumber);
+ }
+
+ private TextSecureAddress getPushAddress(String number) throws InvalidNumberException {
+ String e164number = canonicalizeNumber(number);
+ return new TextSecureAddress(e164number);
+ }
+
+ public GroupInfo getGroupInfo(byte[] groupId) {
+ return groupStore.getGroup(groupId);
+ }
+
+ public void setGroupInfo(GroupInfo group) {
+ groupStore.updateGroup(group);
+ }
+
+ public String getUsername() {
+ return username;
+ }