From 85ed2a942792df5f95ff7d4fea6172ad15865b5b Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Sat, 19 Jan 2019 18:23:29 +0100 Subject: [PATCH] fix PathUtil.getFileExtension - dot in the path must be not as ext --- .../src/com/intellij/util/PathUtil.java | 20 ++----------------- .../src/org/jetbrains/io/FileResponses.kt | 2 +- .../src/com/intellij/util/PathUtilRt.java | 8 ++++---- .../com/intellij/util/PathUtilTest.java | 12 +++++++---- 4 files changed, 15 insertions(+), 27 deletions(-) diff --git a/platform/core-api/src/com/intellij/util/PathUtil.java b/platform/core-api/src/com/intellij/util/PathUtil.java index 3047131583f1..94a60b6fa019 100644 --- a/platform/core-api/src/com/intellij/util/PathUtil.java +++ b/platform/core-api/src/com/intellij/util/PathUtil.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.util; import com.intellij.openapi.application.PathManager; @@ -73,9 +59,7 @@ public class PathUtil { @Nullable public static String getFileExtension(@NotNull String name) { - int index = name.lastIndexOf('.'); - if (index < 0) return null; - return name.substring(index + 1); + return PathUtilRt.getFileExtension(name); } @NotNull diff --git a/platform/platform-impl/src/org/jetbrains/io/FileResponses.kt b/platform/platform-impl/src/org/jetbrains/io/FileResponses.kt index 15a2979dc9ba..3b7c4af87957 100644 --- a/platform/platform-impl/src/org/jetbrains/io/FileResponses.kt +++ b/platform/platform-impl/src/org/jetbrains/io/FileResponses.kt @@ -23,7 +23,7 @@ fun flushChunkedResponse(channel: Channel, isKeepAlive: Boolean) { object FileResponses { fun getContentType(path: String): String { - return fileExtToMimeType.getOrDefault(PathUtilRt.getFileExt(path), "application/octet-stream") + return PathUtilRt.getFileExtension(path)?.let { fileExtToMimeType.get(it) } ?: "application/octet-stream" } @JvmOverloads diff --git a/platform/util-rt/src/com/intellij/util/PathUtilRt.java b/platform/util-rt/src/com/intellij/util/PathUtilRt.java index 87c1b9318dc0..02472e6a9b84 100644 --- a/platform/util-rt/src/com/intellij/util/PathUtilRt.java +++ b/platform/util-rt/src/com/intellij/util/PathUtilRt.java @@ -26,16 +26,16 @@ public class PathUtilRt { return path.substring(getLastIndexOfPathSeparator(path, end) + 1, end); } - @NotNull - public static String getFileExt(@Nullable String path) { + @Nullable + public static String getFileExtension(@Nullable String path) { if (StringUtilRt.isEmpty(path)) { - return ""; + return null; } int end = getEnd(path); int start = getLastIndexOfPathSeparator(path, end) + 1; int index = StringUtilRt.lastIndexOf(path, '.', Math.max(start, 0), end); - return index < 0 ? "" : path.substring(index + 1, end); + return index < 0 ? null : path.substring(index + 1, end); } private static int getEnd(@NotNull String path) { diff --git a/platform/util/testSrc/com/intellij/util/PathUtilTest.java b/platform/util/testSrc/com/intellij/util/PathUtilTest.java index dae8ae4c20b0..777b6c1fa665 100644 --- a/platform/util/testSrc/com/intellij/util/PathUtilTest.java +++ b/platform/util/testSrc/com/intellij/util/PathUtilTest.java @@ -22,10 +22,14 @@ public class PathUtilTest { @Test public void fileExt() { - assertThat(PathUtilRt.getFileExt("foo.html")).isEqualTo("html"); - assertThat(PathUtilRt.getFileExt("foo.html/")).isEqualTo("html"); - assertThat(PathUtilRt.getFileExt("/foo.html/")).isEqualTo("html"); - assertThat(PathUtilRt.getFileExt("/bar/foo.html/")).isEqualTo("html"); + assertThat(PathUtilRt.getFileExtension("foo.html")).isEqualTo("html"); + assertThat(PathUtilRt.getFileExtension("foo.html/")).isEqualTo("html"); + assertThat(PathUtilRt.getFileExtension("/foo.html/")).isEqualTo("html"); + assertThat(PathUtilRt.getFileExtension("/bar/foo.html/")).isEqualTo("html"); + assertThat(PathUtilRt.getFileExtension("")).isNull(); + assertThat(PathUtilRt.getFileExtension("foo")).isNull(); + assertThat(PathUtilRt.getFileExtension("foo.or.bar/bar")).isNull(); + assertThat(PathUtilRt.getFileExtension("foo.")).isEmpty(); } @Test