forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_extension.cc
88 lines (74 loc) · 2.95 KB
/
file_extension.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "pdf/file_extension.h"
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include "base/check_op.h"
#include "base/files/file_path.h"
#include "base/strings/string_util.h"
#if BUILDFLAG(IS_WIN)
#include "base/strings/utf_string_conversions.h"
#endif
namespace chrome_pdf {
// The order of the entries should always match `ViewFileType` in
// tools/metrics/histograms/enums.xml and the indexes defined in
// `ExtensionIndex`.
constexpr std::array<const char*, 76> kFileExtensions = {
"other", ".3ga", ".3gp",
".aac", ".alac", ".asf",
".avi", ".bmp", ".csv",
".doc", ".docx", ".flac",
".gif", ".jpeg", ".jpg",
".log", ".m3u", ".m3u8",
".m4a", ".m4v", ".mid",
".mkv", ".mov", ".mp3",
".mp4", ".mpg", ".odf",
".odp", ".ods", ".odt",
".oga", ".ogg", ".ogv",
".pdf", ".png", ".ppt",
".pptx", ".ra", ".ram",
".rar", ".rm", ".rtf",
".wav", ".webm", ".webp",
".wma", ".wmv", ".xls",
".xlsx", ".crdownload", ".crx",
".dmg", ".exe", ".html",
".htm", ".jar", ".ps",
".torrent", ".txt", ".zip",
"directory", "no extension", "unknown extension",
".mhtml", ".gdoc", ".gsheet",
".gslides", ".arw", ".cr2",
".dng", ".nef", ".nrw",
".orf", ".raf", ".rw2",
".tini",
};
static_assert(kFileExtensions.size() ==
static_cast<size_t>(ExtensionIndex::kMaxValue) + 1);
enum ExtensionIndex FileNameToExtensionIndex(const std::u16string& file_name) {
const base::FilePath::StringType extension_str =
base::FilePath::FromUTF16Unsafe(file_name).Extension();
if (extension_str.empty())
return ExtensionIndex::kEmptyExt;
// All known extensions are ASCII characters. So when an extension contains
// non-ASCII characters, this extension is not recognizable.
if (!base::IsStringASCII(extension_str))
return ExtensionIndex::kOtherExt;
const base::FilePath::StringType extension_str_lower =
base::ToLowerASCII(extension_str);
#if BUILDFLAG(IS_WIN)
const std::string extension = base::WideToUTF8(extension_str_lower);
#else
const std::string& extension = extension_str_lower;
#endif
auto* const* it =
std::find(kFileExtensions.begin(), kFileExtensions.end(), extension);
if (it == kFileExtensions.end())
return ExtensionIndex::kOtherExt;
const int distance = std::distance(kFileExtensions.begin(), it);
DCHECK_GT(distance, 0);
DCHECK_LT(static_cast<size_t>(distance), kFileExtensions.size());
return static_cast<enum ExtensionIndex>(distance);
}
} // namespace chrome_pdf