Lowercase the file extension when looking it up against our well known extensions database.

PiperOrigin-RevId: 671835506
This commit is contained in:
Anay Wadhera
2024-09-06 11:28:19 -07:00
committed by Copybara-Service
parent e8e2218632
commit a8cd7d0b3d
3 changed files with 51 additions and 1 deletions
+1
View File
@@ -46,6 +46,7 @@ cc_test(
timeout = "short",
srcs = [
"encode_test.cc",
"mime_test.cc",
],
shard_count = 8,
deps = [
+3 -1
View File
@@ -17,6 +17,7 @@
#include <string>
#include "absl/container/flat_hash_map.h"
#include "absl/strings/ascii.h"
#include "absl/strings/string_view.h"
namespace nearby {
@@ -25,6 +26,7 @@ namespace utils {
// Refers to
// http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types.
std::string GetWellKnownMimeTypeFromExtension(absl::string_view extension) {
// The extension types in this map must be lower case.
static absl::flat_hash_map<
std::string,
std::string>* mime_map = new absl::flat_hash_map<std::string,
@@ -1029,7 +1031,7 @@ std::string GetWellKnownMimeTypeFromExtension(absl::string_view extension) {
{"zmm", "application/vnd.handheld-entertainment+xml"},
});
return (*mime_map)[extension];
return (*mime_map)[absl::AsciiStrToLower(extension)];
} // NOLINT
} // namespace utils
+47
View File
@@ -0,0 +1,47 @@
// Copyright 2022 Google LLC
//
// 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
//
// https://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.
#include "sharing/internal/base/mime.h"
#include "gtest/gtest.h"
namespace nearby::utils {
namespace {
TEST(MimeTest, GetWellKnownMimeTypeFromExtension) {
EXPECT_EQ(GetWellKnownMimeTypeFromExtension("txt"), "text/plain");
EXPECT_EQ(GetWellKnownMimeTypeFromExtension("pdf"), "application/pdf");
EXPECT_EQ(GetWellKnownMimeTypeFromExtension("doc"),
"application/msword");
EXPECT_EQ(GetWellKnownMimeTypeFromExtension("docx"),
"application/vnd.openxmlformats-officedocument.wordprocessingml."
"document");
EXPECT_EQ(GetWellKnownMimeTypeFromExtension("xls"),
"application/vnd.ms-excel");
}
TEST(MimeTest, GetWellKnownMimeTypeFromExtensionCaseInsensitive) {
EXPECT_EQ(GetWellKnownMimeTypeFromExtension("TXT"), "text/plain");
EXPECT_EQ(GetWellKnownMimeTypeFromExtension("PDF"), "application/pdf");
EXPECT_EQ(GetWellKnownMimeTypeFromExtension("DOC"), "application/msword");
EXPECT_EQ(GetWellKnownMimeTypeFromExtension("DOCX"),
"application/vnd.openxmlformats-officedocument.wordprocessingml."
"document");
EXPECT_EQ(GetWellKnownMimeTypeFromExtension("XLS"),
"application/vnd.ms-excel");
EXPECT_EQ(GetWellKnownMimeTypeFromExtension("PNG"), "image/png");
}
} // namespace
} // namespace nearby::utils