Addressing IFileSystem breaking changes

This PR is to address the breaking changes introduced with https://github.com/aspnet/FileSystem/pull/20

@Tratcher @pranavkm
This commit is contained in:
Praburaj 2014-11-26 12:24:56 -08:00
parent fb9fc12cec
commit 1bc4e21c22
3 changed files with 28 additions and 25 deletions

View File

@ -48,32 +48,34 @@ namespace Microsoft.AspNet.StaticFiles
/// <returns></returns>
public Task Invoke(HttpContext context)
{
IEnumerable<IFileInfo> dirContents;
PathString subpath;
if (Helpers.IsGetOrHeadMethod(context.Request.Method)
&& Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath)
&& _options.FileSystem.TryGetDirectoryContents(subpath.Value, out dirContents))
&& Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath))
{
// Check if any of our default files exist.
for (int matchIndex = 0; matchIndex < _options.DefaultFileNames.Count; matchIndex++)
var dirContents = _options.FileSystem.GetDirectoryContents(subpath.Value);
if (dirContents.Exists)
{
string defaultFile = _options.DefaultFileNames[matchIndex];
IFileInfo file;
// TryMatchPath will make sure subpath always ends with a "/" by adding it if needed.
if (_options.FileSystem.TryGetFileInfo(subpath + defaultFile, out file))
// Check if any of our default files exist.
for (int matchIndex = 0; matchIndex < _options.DefaultFileNames.Count; matchIndex++)
{
// If the path matches a directory but does not end in a slash, redirect to add the slash.
// This prevents relative links from breaking.
if (!Helpers.PathEndsInSlash(context.Request.Path))
string defaultFile = _options.DefaultFileNames[matchIndex];
var file = _options.FileSystem.GetFileInfo(subpath + defaultFile);
// TryMatchPath will make sure subpath always ends with a "/" by adding it if needed.
if (file.Exists)
{
context.Response.StatusCode = 301;
context.Response.Headers[Constants.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
return Constants.CompletedTask;
}
// If the path matches a directory but does not end in a slash, redirect to add the slash.
// This prevents relative links from breaking.
if (!Helpers.PathEndsInSlash(context.Request.Path))
{
context.Response.StatusCode = 301;
context.Response.Headers[Constants.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
return Constants.CompletedTask;
}
// Match found, re-write the url. A later middleware will actually serve the file.
context.Request.Path = new PathString(context.Request.Path.Value + defaultFile);
break;
// Match found, re-write the url. A later middleware will actually serve the file.
context.Request.Path = new PathString(context.Request.Path.Value + defaultFile);
break;
}
}
}
}

View File

@ -50,7 +50,7 @@ namespace Microsoft.AspNet.StaticFiles
{
// Check if the URL matches any expected paths
PathString subpath;
IEnumerable<IFileInfo> contents;
IDirectoryContents contents;
if (Helpers.IsGetOrHeadMethod(context.Request.Method)
&& Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath)
&& TryGetDirectoryInfo(subpath, out contents))
@ -70,9 +70,10 @@ namespace Microsoft.AspNet.StaticFiles
return _next(context);
}
private bool TryGetDirectoryInfo(PathString subpath, out IEnumerable<IFileInfo> contents)
private bool TryGetDirectoryInfo(PathString subpath, out IDirectoryContents contents)
{
return _options.FileSystem.TryGetDirectoryContents(subpath.Value, out contents);
contents = _options.FileSystem.GetDirectoryContents(subpath.Value);
return contents.Exists;
}
}
}

View File

@ -116,8 +116,8 @@ namespace Microsoft.AspNet.StaticFiles
public bool LookupFileInfo()
{
bool found = _options.FileSystem.TryGetFileInfo(_subPath.Value, out _fileInfo);
if (found)
_fileInfo = _options.FileSystem.GetFileInfo(_subPath.Value);
if (_fileInfo.Exists)
{
_length = _fileInfo.Length;
@ -130,7 +130,7 @@ namespace Microsoft.AspNet.StaticFiles
_etag = Convert.ToString(etagHash, 16);
_etagQuoted = '\"' + _etag + '\"';
}
return found;
return _fileInfo.Exists;
}
public void ComprehendRequestHeaders()