refactored filename and extension parsing

This commit is contained in:
Daniel Gultsch 2019-08-16 14:09:42 +02:00
parent c039ffcf35
commit 0770914edf
3 changed files with 53 additions and 39 deletions

View file

@ -77,22 +77,20 @@ public class HttpDownloadConnection implements Transferable {
} else { } else {
mUrl = CryptoHelper.toHttpsUrl(new URL(message.getBody().split("\n")[0])); mUrl = CryptoHelper.toHttpsUrl(new URL(message.getBody().split("\n")[0]));
} }
String[] parts = mUrl.getPath().toLowerCase().split("\\."); final AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(mUrl.getPath());
String lastPart = parts.length >= 1 ? parts[parts.length - 1] : null; if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
String secondToLast = parts.length >= 2 ? parts[parts.length - 2] : null;
if ("pgp".equals(lastPart) || "gpg".equals(lastPart)) {
this.message.setEncryption(Message.ENCRYPTION_PGP); this.message.setEncryption(Message.ENCRYPTION_PGP);
} else if (message.getEncryption() != Message.ENCRYPTION_OTR } else if (message.getEncryption() != Message.ENCRYPTION_OTR
&& message.getEncryption() != Message.ENCRYPTION_AXOLOTL) { && message.getEncryption() != Message.ENCRYPTION_AXOLOTL) {
this.message.setEncryption(Message.ENCRYPTION_NONE); this.message.setEncryption(Message.ENCRYPTION_NONE);
} }
String extension; final String ext;
if (VALID_CRYPTO_EXTENSIONS.contains(lastPart)) { if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
extension = secondToLast; ext = extension.secondary;
} else { } else {
extension = lastPart; ext = extension.main;
} }
message.setRelativeFilePath(message.getUuid() + (extension != null ? ("." + extension) : "")); message.setRelativeFilePath(message.getUuid() + (ext != null ? ("." + ext) : ""));
this.file = mXmppConnectionService.getFileBackend().getFile(message, false); this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
final String reference = mUrl.getRef(); final String reference = mUrl.getRef();
if (reference != null && AesGcmURLStreamHandler.IV_KEY.matcher(reference).matches()) { if (reference != null && AesGcmURLStreamHandler.IV_KEY.matcher(reference).matches()) {

View file

@ -109,4 +109,23 @@ public class AbstractConnectionManager {
PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE); PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name); return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
} }
public static class Extension {
public final String main;
public final String secondary;
private Extension(String main, String secondary) {
this.main = main;
this.secondary = secondary;
}
public static Extension of(String path) {
final int pos = path.lastIndexOf('/');
final String filename = path.substring(pos + 1).toLowerCase();
final String[] parts = filename.split("\\.");
final String main = parts.length >= 2 ? parts[parts.length - 1] : null;
final String secondary = parts.length >= 3 ? parts[parts.length - 2] : null;
return new Extension(main, secondary);
}
}
} }

View file

@ -414,41 +414,26 @@ public class JingleConnection implements Transferable {
this.mXmppAxolotlMessage = XmppAxolotlMessage.fromElement(encrypted, packet.getFrom().asBareJid()); this.mXmppAxolotlMessage = XmppAxolotlMessage.fromElement(encrypted, packet.getFrom().asBareJid());
} }
Element fileSize = fileOffer.findChild("size"); Element fileSize = fileOffer.findChild("size");
Element fileNameElement = fileOffer.findChild("name"); final String path = fileOffer.findChildContent("name");
if (fileNameElement != null) { if (path != null) {
String[] filename = fileNameElement.getContent() AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(path);
.toLowerCase(Locale.US).toLowerCase().split("\\."); if (VALID_IMAGE_EXTENSIONS.contains(extension.main)) {
String extension = filename[filename.length - 1];
if (VALID_IMAGE_EXTENSIONS.contains(extension)) {
message.setType(Message.TYPE_IMAGE); message.setType(Message.TYPE_IMAGE);
message.setRelativeFilePath(message.getUuid() + "." + extension); message.setRelativeFilePath(message.getUuid() + "." + extension.main);
} else if (VALID_CRYPTO_EXTENSIONS.contains( } else if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
filename[filename.length - 1])) { if (VALID_IMAGE_EXTENSIONS.contains(extension.secondary)) {
if (filename.length == 3) { message.setType(Message.TYPE_IMAGE);
extension = filename[filename.length - 2]; message.setRelativeFilePath(message.getUuid() + "." + extension.main);
if (VALID_IMAGE_EXTENSIONS.contains(extension)) { } else {
message.setType(Message.TYPE_IMAGE); message.setType(Message.TYPE_FILE);
message.setRelativeFilePath(message.getUuid() + "." + extension); message.setRelativeFilePath(message.getUuid() + (extension.secondary != null ? ("." + extension.secondary) : ""));
} else {
message.setType(Message.TYPE_FILE);
}
message.setEncryption(Message.ENCRYPTION_PGP);
} }
message.setEncryption(Message.ENCRYPTION_PGP);
} else { } else {
message.setType(Message.TYPE_FILE); message.setType(Message.TYPE_FILE);
message.setRelativeFilePath(message.getUuid() + (extension.main != null ? ("." + extension.main) : ""));
} }
if (message.getType() == Message.TYPE_FILE) { long size = parseLong(fileSize, 0);
String suffix = "";
if (!fileNameElement.getContent().isEmpty()) {
String parts[] = fileNameElement.getContent().split("/");
suffix = parts[parts.length - 1];
if (message.getEncryption() == Message.ENCRYPTION_PGP && (suffix.endsWith(".pgp") || suffix.endsWith(".gpg"))) {
suffix = suffix.substring(0, suffix.length() - 4);
}
}
message.setRelativeFilePath(message.getUuid() + "_" + suffix);
}
long size = Long.parseLong(fileSize.getContent());
message.setBody(Long.toString(size)); message.setBody(Long.toString(size));
conversation.add(message); conversation.add(message);
mJingleConnectionManager.updateConversationUi(true); mJingleConnectionManager.updateConversationUi(true);
@ -493,6 +478,18 @@ public class JingleConnection implements Transferable {
} }
} }
private static long parseLong(final Element element, final long l) {
final String input = element == null ? null : element.getContent();
if (input == null) {
return l;
}
try {
return Long.parseLong(input);
} catch (Exception e) {
return l;
}
}
private void sendInitRequest() { private void sendInitRequest() {
JinglePacket packet = this.bootstrapPacket("session-initiate"); JinglePacket packet = this.bootstrapPacket("session-initiate");
Content content = new Content(this.contentCreator, this.contentName); Content content = new Content(this.contentCreator, this.contentName);