Skip to content

Commit

Permalink
Better support for file:// uri scheme within CodecFactory. filoe#265.
Browse files Browse the repository at this point in the history
  • Loading branch information
filoe committed Sep 9, 2017
1 parent 2ddcf6b commit 4f16100
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
49 changes: 45 additions & 4 deletions CSCore/Codecs/CodecFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ public IWaveSource GetCodec(Uri uri)

try
{
if (uri.IsFile)
{
return GetCodec(uri.LocalPath);
}
var filename = TryFindFilename(uri);
if (!String.IsNullOrEmpty(filename))
return GetCodec(filename);

return OpenWebStream(uri.ToString());
}
catch (IOException)
Expand Down Expand Up @@ -283,6 +283,47 @@ private string GenerateFilter()
return stringBuilder.ToString();
}

private string TryFindFilename(Uri uri)
{
if (File.Exists(uri.LocalPath))
return uri.LocalPath;

FileInfo fileInfo;
try
{
fileInfo = new FileInfo(uri.LocalPath);
if (fileInfo.Exists)
return fileInfo.FullName;
}
catch (Exception)
{
Debug.WriteLine(String.Format("{0} not found.", uri.LocalPath));
}

try
{
fileInfo = new FileInfo(uri.OriginalString);
if (fileInfo.Exists)
return fileInfo.FullName;
}
catch (Exception)
{
Debug.WriteLine(String.Format("{0} not found.", uri.OriginalString));
}

var path = Win32.NativeMethods.PathCreateFromUrl(uri.OriginalString);
if (path == null || !File.Exists(path))
{
path = Win32.NativeMethods.PathCreateFromUrl(uri.AbsoluteUri);
}
if (path != null && File.Exists(path))
{
return path;
}

return null;
}

private class DisposeFileStreamSource : WaveAggregatorBase
{
private Stream _stream;
Expand Down
19 changes: 18 additions & 1 deletion CSCore/Win32/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class NativeMethods
internal static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpBuffer, int nSize, IntPtr va_list_arguments);

[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
internal extern static IntPtr LoadLibrary(string librayName);
internal static extern IntPtr LoadLibrary(string librayName);

[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool FreeLibrary(IntPtr hModule);
Expand All @@ -32,5 +32,22 @@ internal class NativeMethods

[DllImport("ole32.dll")]
public static extern int PropVariantClear(ref PropertyVariant propertyVariant);

[DllImport("shlwapi.dll", SetLastError = true)]
public static extern int PathCreateFromUrl([In]string url, [Out] StringBuilder path, [In, Out]ref uint pathLength, [In]uint reserved);

public static string PathCreateFromUrl(string url)
{
const int internetMaxPathLength = 2048;
StringBuilder stringBuilder = new StringBuilder(internetMaxPathLength);
uint pathLength = internetMaxPathLength;

var error = PathCreateFromUrl(url, stringBuilder, ref pathLength, 0);
if (error == (int) HResult.S_OK)
{
return stringBuilder.ToString();
}
return null;
}
}
}

0 comments on commit 4f16100

Please sign in to comment.