From 34fcdda53fa8ae1174909b62860534d2d874eb72 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Thu, 29 Sep 2016 11:57:16 +0200 Subject: Implements FS#235: Deletion of remote files uploaded via httpupload --- .../conversationsplus/entities/Message.java | 170 ++++----------------- 1 file changed, 30 insertions(+), 140 deletions(-) (limited to 'src/main/java/de/thedevstack/conversationsplus/entities/Message.java') diff --git a/src/main/java/de/thedevstack/conversationsplus/entities/Message.java b/src/main/java/de/thedevstack/conversationsplus/entities/Message.java index f081e82a..a81ba404 100644 --- a/src/main/java/de/thedevstack/conversationsplus/entities/Message.java +++ b/src/main/java/de/thedevstack/conversationsplus/entities/Message.java @@ -7,6 +7,7 @@ import java.net.MalformedURLException; import java.net.URL; import de.thedevstack.conversationsplus.crypto.axolotl.XmppAxolotlSession; +import de.thedevstack.conversationsplus.enums.FileStatus; import de.thedevstack.conversationsplus.utils.FileUtils; import de.thedevstack.conversationsplus.utils.MimeUtils; import de.thedevstack.conversationsplus.xmpp.jid.InvalidJidException; @@ -27,9 +28,6 @@ public class Message extends AbstractEntity { public static final int STATUS_SEND_RECEIVED = 7; public static final int STATUS_SEND_DISPLAYED = 8; - public static final int STATUS_REMOTE_FILE_DELETE_FAILED = 101; - public static final int STATUS_REMOTE_FILE_DELETED = 102; - public static final int ENCRYPTION_NONE = 0; public static final int ENCRYPTION_PGP = 1; public static final int ENCRYPTION_OTR = 2; @@ -87,6 +85,7 @@ public class Message extends AbstractEntity { private Decision mTreatAsDownloadAble = Decision.NOT_DECIDED; private boolean httpUploaded; + private FileParams fileParams; private Message() { @@ -338,6 +337,9 @@ public class Message extends AbstractEntity { public void setType(int type) { this.type = type; + if (null != this.fileParams && (type == Message.TYPE_FILE || type == Message.TYPE_IMAGE)) { + this.setFileParams(new FileParams()); + } } public boolean isCarbon() { @@ -371,17 +373,8 @@ public class Message extends AbstractEntity { || message.getBody() == null || message.getCounterpart() == null) { return false; } else { - String body, otherBody; - if (this.hasFileOnRemoteHost()) { - body = this.getFileParams().url.toString(); - } else { - body = this.getBody(); - } - if (message.hasFileOnRemoteHost()) { - otherBody = message.getFileParams().url.toString(); - } else { - otherBody = message.getBody(); - } + String body = this.getBody(); + String otherBody = message.getBody(); if (message.getRemoteMsgId() != null && this.getRemoteMsgId() != null) { return (message.getRemoteMsgId().equals(this.getRemoteMsgId()) @@ -496,27 +489,10 @@ public class Message extends AbstractEntity { } private String extractRelevantExtension(String path) { - if (path == null || path.isEmpty()) { - return null; - } - - String filename = path.substring(path.lastIndexOf('/') + 1).toLowerCase(); - - final String lastPart = FileUtils.getLastExtension(filename); - - if (!lastPart.isEmpty()) { - // we want the real file extension, not the crypto one - final String secondToLastPart = FileUtils.getSecondToLastExtension(filename); - if (!secondToLastPart.isEmpty() && Transferable.VALID_CRYPTO_EXTENSIONS.contains(lastPart)) { - return secondToLastPart; - } else { - return lastPart; - } - } - return null; + return FileUtils.getRelevantExtension(path); } - public String getMimeType() { + public String getMimeType() { // TODO: Move to fileparams if (relativeFilePath != null) { int start = relativeFilePath.lastIndexOf('.') + 1; if (start < relativeFilePath.length()) { @@ -595,103 +571,6 @@ public class Message extends AbstractEntity { } } - public FileParams getFileParams() { - FileParams params = getLegacyFileParams(); - if (params != null) { - return params; - } - params = new FileParams(); - if (this.transferable != null) { - params.size = this.transferable.getFileSize(); - } - if (this.getBody() == null) { - return params; - } - String parts[] = this.getBody().split("\\|"); - switch (parts.length) { - case 1: - try { - params.size = Long.parseLong(parts[0]); - } catch (NumberFormatException e) { - try { - params.url = new URL(parts[0]); - } catch (MalformedURLException e1) { - params.url = null; - } - } - break; - case 2: - case 4: - try { - params.url = new URL(parts[0]); - } catch (MalformedURLException e1) { - params.url = null; - } - try { - params.size = Long.parseLong(parts[1]); - } catch (NumberFormatException e) { - params.size = 0; - } - try { - params.width = Integer.parseInt(parts[2]); - } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { - params.width = 0; - } - try { - params.height = Integer.parseInt(parts[3]); - } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { - params.height = 0; - } - break; - case 3: - try { - params.size = Long.parseLong(parts[0]); - } catch (NumberFormatException e) { - params.size = 0; - } - try { - params.width = Integer.parseInt(parts[1]); - } catch (NumberFormatException e) { - params.width = 0; - } - try { - params.height = Integer.parseInt(parts[2]); - } catch (NumberFormatException e) { - params.height = 0; - } - break; - } - return params; - } - - public FileParams getLegacyFileParams() { - FileParams params = new FileParams(); - if (this.getBody() == null) { - return params; - } - String parts[] = this.getBody().split(","); - if (parts.length == 3) { - try { - params.size = Long.parseLong(parts[0]); - } catch (NumberFormatException e) { - return null; - } - try { - params.width = Integer.parseInt(parts[1]); - } catch (NumberFormatException e) { - return null; - } - try { - params.height = Integer.parseInt(parts[2]); - } catch (NumberFormatException e) { - return null; - } - return params; - } else { - return null; - } - } - public void untie() { this.mNextMessage = null; this.mPreviousMessage = null; @@ -701,19 +580,22 @@ public class Message extends AbstractEntity { return type == TYPE_FILE || type == TYPE_IMAGE; } - public boolean hasFileOnRemoteHost() { - return isFileOrImage() && getFileParams().url != null; - } + public boolean hasFileAttached() { + return isFileOrImage() || isHttpUploaded() || (null != fileParams && null != fileParams.getPath()); + } - public boolean needsUploading() { - return isFileOrImage() && getFileParams().url == null; + /* + @TODO better + */ + public boolean hasFileOnRemoteHost() { + return hasFileAttached() && null != getFileParams() && getFileParams().isRemoteAvailable(); } - public class FileParams { - public URL url; - public long size = 0; - public int width = 0; - public int height = 0; + /* + @TODO better + */ + public boolean needsUploading() { + return hasFileAttached() && getFileParams().getFileStatus() == FileStatus.NEEDS_UPLOAD; } public void setFingerprint(String fingerprint) { @@ -768,6 +650,14 @@ public class Message extends AbstractEntity { this.httpUploaded = httpUploaded; } + public FileParams getFileParams() { + return this.fileParams; + } + + public void setFileParams(FileParams params) { + this.fileParams = params; + } + private static int getCleanedEncryption(int encryption) { if (encryption == ENCRYPTION_DECRYPTED || encryption == ENCRYPTION_DECRYPTION_FAILED) { return ENCRYPTION_PGP; -- cgit v1.2.3