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 {
mUrl = CryptoHelper.toHttpsUrl(new URL(message.getBody().split("\n")[0]));
}
String[] parts = mUrl.getPath().toLowerCase().split("\\.");
String lastPart = parts.length >= 1 ? parts[parts.length - 1] : null;
String secondToLast = parts.length >= 2 ? parts[parts.length - 2] : null;
if ("pgp".equals(lastPart) || "gpg".equals(lastPart)) {
final AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(mUrl.getPath());
if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
this.message.setEncryption(Message.ENCRYPTION_PGP);
} else if (message.getEncryption() != Message.ENCRYPTION_OTR
&& message.getEncryption() != Message.ENCRYPTION_AXOLOTL) {
this.message.setEncryption(Message.ENCRYPTION_NONE);
}
String extension;
if (VALID_CRYPTO_EXTENSIONS.contains(lastPart)) {
extension = secondToLast;
final String ext;
if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
ext = extension.secondary;
} 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);
final String reference = mUrl.getRef();
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);
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());
}
Element fileSize = fileOffer.findChild("size");
Element fileNameElement = fileOffer.findChild("name");
if (fileNameElement != null) {
String[] filename = fileNameElement.getContent()
.toLowerCase(Locale.US).toLowerCase().split("\\.");
String extension = filename[filename.length - 1];
if (VALID_IMAGE_EXTENSIONS.contains(extension)) {
final String path = fileOffer.findChildContent("name");
if (path != null) {
AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(path);
if (VALID_IMAGE_EXTENSIONS.contains(extension.main)) {
message.setType(Message.TYPE_IMAGE);
message.setRelativeFilePath(message.getUuid() + "." + extension);
} else if (VALID_CRYPTO_EXTENSIONS.contains(
filename[filename.length - 1])) {
if (filename.length == 3) {
extension = filename[filename.length - 2];
if (VALID_IMAGE_EXTENSIONS.contains(extension)) {
message.setType(Message.TYPE_IMAGE);
message.setRelativeFilePath(message.getUuid() + "." + extension);
} else {
message.setType(Message.TYPE_FILE);
}
message.setEncryption(Message.ENCRYPTION_PGP);
message.setRelativeFilePath(message.getUuid() + "." + extension.main);
} else if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) {
if (VALID_IMAGE_EXTENSIONS.contains(extension.secondary)) {
message.setType(Message.TYPE_IMAGE);
message.setRelativeFilePath(message.getUuid() + "." + extension.main);
} else {
message.setType(Message.TYPE_FILE);
message.setRelativeFilePath(message.getUuid() + (extension.secondary != null ? ("." + extension.secondary) : ""));
}
message.setEncryption(Message.ENCRYPTION_PGP);
} else {
message.setType(Message.TYPE_FILE);
message.setRelativeFilePath(message.getUuid() + (extension.main != null ? ("." + extension.main) : ""));
}
if (message.getType() == Message.TYPE_FILE) {
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());
long size = parseLong(fileSize, 0);
message.setBody(Long.toString(size));
conversation.add(message);
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() {
JinglePacket packet = this.bootstrapPacket("session-initiate");
Content content = new Content(this.contentCreator, this.contentName);