Commit 8379101d authored by Anand K. Mistry's avatar Anand K. Mistry Committed by Commit Bot

Re-sniff AMR files to correctly identify them.

AMR files begin with "#!", which looks like a shell script, which causes
the primary sniffer to think the file is plain text. Re-sniff using the
expanded types sniffer to properly detect the file as AMR.

BUG=813059

Change-Id: Ic4843ce0773a0cba028dde7f134ad32215cacd5d
Reviewed-on: https://chromium-review.googlesource.com/c/1349129
Commit-Queue: Anand Mistry <amistry@chromium.org>
Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610752}
parent 864591fc
......@@ -43,6 +43,17 @@ void SniffMimeType(const base::FilePath& local_path, std::string* result) {
net::FilePathToFileURL(local_path),
std::string(), // type_hint (passes no hint)
net::ForceSniffFileUrlsForHtml::kDisabled, result);
if (*result == "text/plain") {
// text/plain misidentifies AMR files, which look like scripts because
// they start with "#!". Use SniffMimeTypeFromLocalData to try and get a
// better match.
// TODO(amistry): Potentially add other types (i.e. SVG).
std::string secondary_result;
net::SniffMimeTypeFromLocalData(&content[0], bytes_read,
&secondary_result);
if (!secondary_result.empty())
*result = secondary_result;
}
}
}
......
......@@ -11,6 +11,7 @@
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "content/public/browser/browser_context.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/api/extensions_api_client.h"
......@@ -58,22 +59,35 @@ class FileHandlersMimeUtilTest : public ExtensionsTest {
file_system_context_ = content::CreateFileSystemContextForTesting(
NULL, browser_context()->GetPath());
base::FilePath temp_filename;
EXPECT_TRUE(base::CreateTemporaryFile(&temp_filename));
const std::string kSampleContent = "<html><body></body></html>";
EXPECT_EQ(static_cast<int>(kSampleContent.size()),
base::WriteFile(temp_filename, kSampleContent.c_str(),
kSampleContent.size()));
base::FilePath temp_filename = CreateTemporaryFile(kSampleContent);
// File path must end in .html to avoid relying upon MIME-sniffing, which
// is disabled for HTML files delivered from file:// URIs.
html_mime_file_path_ =
temp_filename.AddExtension(FILE_PATH_LITERAL(".html"));
EXPECT_TRUE(base::Move(temp_filename, html_mime_file_path_));
// First 16 bytes of //media/test/data/bear.amr
const std::string kSampleAmrContent =
"\x23\x21\x41\x4d\x52\x0a\x3c\x53\x0a\x7c\xe8\xb8\x41\xa5\x80\xca";
temp_filename = CreateTemporaryFile(kSampleAmrContent);
amr_mime_file_path_ = temp_filename.AddExtension(FILE_PATH_LITERAL(".amr"));
EXPECT_TRUE(base::Move(temp_filename, amr_mime_file_path_));
}
base::FilePath CreateTemporaryFile(const std::string& contents) {
base::FilePath temp_filename;
EXPECT_TRUE(base::CreateTemporaryFile(&temp_filename));
EXPECT_EQ(
static_cast<int>(contents.size()),
base::WriteFile(temp_filename, contents.c_str(), contents.size()));
return temp_filename;
}
ExtensionsAPIClient extensions_api_client_;
scoped_refptr<storage::FileSystemContext> file_system_context_;
base::FilePath html_mime_file_path_;
base::FilePath amr_mime_file_path_;
};
TEST_F(FileHandlersMimeUtilTest, GetMimeTypeForLocalPath) {
......@@ -103,6 +117,17 @@ TEST_F(FileHandlersMimeUtilTest, GetMimeTypeForLocalPath) {
content::RunAllTasksUntilIdle();
EXPECT_EQ("text/html", result);
}
{
std::string result;
GetMimeTypeForLocalPath(browser_context(), amr_mime_file_path_,
base::BindRepeating(&OnMimeTypeResult, &result));
content::RunAllTasksUntilIdle();
// The MIME type for AMR files is supposed to be "audio/AMR" (note: upper
// case). However, platforms are inconsistent in this and some, including
// chrome, use lower case.
EXPECT_EQ("audio/amr", base::ToLowerASCII(result));
}
}
TEST_F(FileHandlersMimeUtilTest, MimeTypeCollector_ForURLs) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment