Skip to content

Commit 32dff43

Browse files
robertobasile84decriptor
authored andcommitted
Added method to check if file is supported instead of waiting for exception
1 parent a8421d9 commit 32dff43

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

src/TaglibSharp/File.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,37 @@ public static File Create (string path, string mimetype, ReadStyle propertiesSty
12351235
return Create (new LocalFileAbstraction (path), mimetype, propertiesStyle);
12361236
}
12371237

1238+
/// <summary>
1239+
/// Returns true if the file is supported
1240+
/// </summary>
1241+
/// <param name="path">The file path</param>
1242+
/// <returns>True if supported, false otherwise</returns>
1243+
public static bool IsSupportedFile (string path)
1244+
{
1245+
var abstraction = new LocalFileAbstraction (path);
1246+
1247+
return IsSupportedFile (abstraction);
1248+
}
1249+
1250+
private static bool IsSupportedFile(IFileAbstraction abstraction)
1251+
{
1252+
string mimetype = GetMimeType (abstraction);
1253+
1254+
return FileTypes.AvailableTypes.ContainsKey (mimetype);
1255+
}
1256+
1257+
private static string GetMimeType (IFileAbstraction abstraction)
1258+
{
1259+
string ext = string.Empty;
1260+
1261+
int index = abstraction.Name.LastIndexOf (".") + 1;
1262+
1263+
if (index >= 1 && index < abstraction.Name.Length)
1264+
ext = abstraction.Name.Substring (index, abstraction.Name.Length - index);
1265+
1266+
return $"taglib/{ext.ToLower (CultureInfo.InvariantCulture)}";
1267+
}
1268+
12381269
/// <summary>
12391270
/// Creates a new instance of a <see cref="File" /> subclass
12401271
/// for a specified file abstraction, mime-type, and read
@@ -1269,16 +1300,7 @@ public static File Create (string path, string mimetype, ReadStyle propertiesSty
12691300
/// </exception>
12701301
public static File Create (IFileAbstraction abstraction, string mimetype, ReadStyle propertiesStyle)
12711302
{
1272-
if (mimetype == null) {
1273-
string ext = string.Empty;
1274-
1275-
int index = abstraction.Name.LastIndexOf (".") + 1;
1276-
1277-
if (index >= 1 && index < abstraction.Name.Length)
1278-
ext = abstraction.Name.Substring (index, abstraction.Name.Length - index);
1279-
1280-
mimetype = "taglib/" + ext.ToLower (CultureInfo.InvariantCulture);
1281-
}
1303+
mimetype ??= GetMimeType (abstraction);
12821304

12831305
foreach (var resolver in file_type_resolvers) {
12841306
var file = resolver (abstraction, mimetype, propertiesStyle);
@@ -1287,7 +1309,7 @@ public static File Create (IFileAbstraction abstraction, string mimetype, ReadSt
12871309
return file;
12881310
}
12891311

1290-
if (!FileTypes.AvailableTypes.ContainsKey (mimetype))
1312+
if (!IsSupportedFile(abstraction))
12911313
throw new UnsupportedFormatException (
12921314
string.Format (CultureInfo.InvariantCulture, "{0} ({1})", abstraction.Name, mimetype));
12931315

0 commit comments

Comments
 (0)