forked from files-community/Files
/
FileExtensionHelpers.cs
221 lines (196 loc) · 8.49 KB
/
FileExtensionHelpers.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.
using System;
using System.Linq;
namespace Files.Shared.Helpers
{
/// <summary>
/// Provides static extension for path extension.
/// </summary>
public static class FileExtensionHelpers
{
/// <summary>
/// Check if the file extension matches one of the specified extensions.
/// </summary>
/// <param name="filePathToCheck">Path or name or extension of the file to check.</param>
/// <param name="extensions">List of the extensions to check.</param>
/// <returns><c>true</c> if the filePathToCheck has one of the specified extensions; otherwise, <c>false</c>.</returns>
public static bool HasExtension(string? filePathToCheck, params string[] extensions)
{
if (string.IsNullOrWhiteSpace(filePathToCheck))
return false;
return extensions.Any(ext => filePathToCheck.EndsWith(ext, StringComparison.OrdinalIgnoreCase));
}
/// <summary>
/// Check if the file extension is an image file.
/// </summary>
/// <param name="fileExtensionToCheck">The file extension to check.</param>
/// <returns><c>true</c> if the fileExtensionToCheck is an image; otherwise, <c>false</c>.</returns>
public static bool IsImageFile(string? fileExtensionToCheck)
{
return HasExtension(fileExtensionToCheck, ".png", ".bmp", ".jpg", ".jpeg", ".jfif", ".gif", ".tiff", ".tif");
}
/// <summary>
/// Check if the file extension is an audio file.
/// </summary>
/// <param name="fileExtensionToCheck">The file extension to check.</param>
/// <returns><c>true</c> if the fileExtensionToCheck is an audio file; otherwise, <c>false</c>.</returns>
public static bool IsAudioFile(string? fileExtensionToCheck)
{
return HasExtension(fileExtensionToCheck, ".mp3", ".m4a", ".wav", ".wma", ".aac", ".adt", ".adts", ".cda", ".flac");
}
/// <summary>
/// Check if the file extension is a video file.
/// </summary>
/// <param name="fileExtensionToCheck">The file extension to check.</param>
/// <returns><c>true</c> if the fileExtensionToCheck is a video file; otherwise, <c>false</c>.</returns>
public static bool IsVideoFile(string? fileExtensionToCheck)
{
return HasExtension(fileExtensionToCheck, ".mp4", ".webm", ".ogg", ".mov", ".qt", ".m4v", ".mp4v", ".3g2", ".3gp2", ".3gp", ".3gpp", ".mkv");
}
/// <summary>
/// Check if the file extension is a PowerShell script.
/// </summary>
/// <param name="fileExtensionToCheck">The file extension to check.</param>
/// <returns><c>true</c> if the fileExtensionToCheck is a PowerShell script; otherwise, <c>false</c>.</returns>
public static bool IsPowerShellFile(string fileExtensionToCheck)
{
return HasExtension(fileExtensionToCheck, ".ps1");
}
/// <summary>
/// Check if the file extension is a zip file.
/// </summary>
/// <param name="fileExtensionToCheck">The file extension to check.</param>
/// <returns><c>true</c> if the fileExtensionToCheck is a zip bundle file; otherwise, <c>false</c>.</returns>
public static bool IsZipFile(string? fileExtensionToCheck)
{
return HasExtension(fileExtensionToCheck, ".zip", ".msix", ".appx", ".msixbundle", ".appxbundle", ".7z", ".rar", ".tar", ".mcpack", ".mcworld", ".jar", ".gz", ".lzh");
}
public static bool IsBrowsableZipFile(string? filePath, out string? ext)
{
if (string.IsNullOrWhiteSpace(filePath))
{
ext = null;
return false;
}
// Only extensions we want to browse
ext = new[] { ".zip", ".7z", ".rar", ".tar", ".gz", ".lzh", ".jar" }
.FirstOrDefault(x => filePath.Contains(x, StringComparison.OrdinalIgnoreCase));
return ext is not null;
}
/// <summary>
/// Check if the file extension is a driver inf file.
/// </summary>
/// <param name="fileExtensionToCheck">The file extension to check.</param>
/// <returns><c>true</c> if the fileExtensionToCheck is an inf file; otherwise <c>false</c>.</returns>
public static bool IsInfFile(string? fileExtensionToCheck)
{
return HasExtension(fileExtensionToCheck, ".inf");
}
/// <summary>
/// Check if the file extension is a font file.
/// </summary>
/// <param name="fileExtensionToCheck">The file extension to check.</param>
/// <returns><c>true</c> if the fileExtensionToCheck is a font file; otherwise <c>false</c>.</returns>
/// <remarks>Font file types are; fon, otf, ttc, ttf</remarks>
public static bool IsFontFile(string? fileExtensionToCheck)
{
return HasExtension(fileExtensionToCheck, ".fon", ".otf", ".ttc", ".ttf");
}
/// <summary>
/// Check if the file path is a shortcut file.
/// </summary>
/// <param name="filePathToCheck">The file path to check.</param>
/// <returns><c>true</c> if the filePathToCheck is a shortcut file; otherwise, <c>false</c>.</returns>
/// <remarks>Shortcut file type is .lnk</remarks>
public static bool IsShortcutFile(string? filePathToCheck)
{
return HasExtension(filePathToCheck, ".lnk");
}
/// <summary>
/// Check if the file path is a web link file.
/// </summary>
/// <param name="filePathToCheck">The file path to check.</param>
/// <returns><c>true</c> if the filePathToCheck is a web link file; otherwise, <c>false</c>.</returns>
/// <remarks>Web link file type is .url</remarks>
public static bool IsWebLinkFile(string? filePathToCheck)
{
return HasExtension(filePathToCheck, ".url");
}
public static bool IsShortcutOrUrlFile(string? filePathToCheck)
{
return HasExtension(filePathToCheck, ".lnk", ".url");
}
/// <summary>
/// Check if the file path is an executable file.
/// </summary>
/// <param name="filePathToCheck">The file path to check.</param>
/// <returns><c>true</c> if the filePathToCheck is an executable file; otherwise, <c>false</c>.</returns>
/// /// <remarks>Executable file types are; exe, bat, cmd</remarks>
public static bool IsExecutableFile(string? filePathToCheck, bool exeOnly = false)
{
return
exeOnly
? HasExtension(filePathToCheck, ".exe")
: HasExtension(filePathToCheck, ".exe", ".bat", ".cmd");
}
/// <summary>
/// Check if the file path is an msi installer file.
/// </summary>
/// <param name="filePathToCheck">The file path to check.</param>
/// <returns><c>true</c> if the filePathToCheck is an msi installer file; otherwise, <c>false</c>.</returns>
public static bool IsMsiFile(string? filePathToCheck)
{
return HasExtension(filePathToCheck, ".msi");
}
/// <summary>
/// Check if the file extension is a vhd disk file.
/// </summary>
/// <param name="fileExtensionToCheck">The file extension to check.</param>
/// <returns><c>true</c> if the fileExtensionToCheck is a vhd disk file; otherwise, <c>false</c>.</returns>
/// <remarks>Vhd disk file types are; vhd, vhdx</remarks>
public static bool IsVhdFile(string? fileExtensionToCheck)
{
return HasExtension(fileExtensionToCheck, ".vhd", ".vhdx");
}
/// <summary>
/// Check if the file extension is a screen saver file.
/// </summary>
/// <param name="fileExtensionToCheck">The file extension to check.</param>
/// <returns><c>true</c> if the fileExtensionToCheck is a screen saver file; otherwise, <c>false</c>.</returns>
/// <remarks>Screen saver file types are; scr</remarks>
public static bool IsScreenSaverFile(string? fileExtensionToCheck)
{
return HasExtension(fileExtensionToCheck, ".scr");
}
/// <summary>
/// Check if the file extension is a media (audio/video) file.
/// </summary>
/// <param name="filePathToCheck">The file extension to check.</param>
/// <returns><c>true</c> if the filePathToCheck is a media file; otherwise, <c>false</c>.</returns>
public static bool IsMediaFile(string? filePathToCheck)
{
return HasExtension(
filePathToCheck, ".mp4", ".m4v", ".mp4v", ".3g2", ".3gp2", ".3gp", ".3gpp",
".mpg", ".mp2", ".mpeg", ".mpe", ".mpv", ".ogg", ".avi", ".wmv", ".mov", ".qt");
}
/// <summary>
/// Check if the file extension is a certificate file.
/// </summary>
/// <param name="filePathToCheck"></param>
/// <returns><c>true</c> if the filePathToCheck is a certificate file; otherwise, <c>false</c>.</returns>
public static bool IsCertificateFile(string? filePathToCheck)
{
return HasExtension(filePathToCheck, ".cer", ".crt", ".der", ".pfx");
}
/// <summary>
/// Check if the file extension is a Script file.
/// </summary>
/// <param name="filePathToCheck"></param>
/// <returns><c>true</c> if the filePathToCheck is a script file; otherwise, <c>false</c>.</returns>
public static bool IsScriptFile(string? filePathToCheck)
{
return HasExtension(filePathToCheck, ".py", ".ahk");
}
}
}