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

View File

@ -50,7 +50,7 @@ namespace Microsoft.AspNet.StaticFiles
{ {
// Check if the URL matches any expected paths // Check if the URL matches any expected paths
PathString subpath; PathString subpath;
IEnumerable<IFileInfo> contents; IDirectoryContents contents;
if (Helpers.IsGetOrHeadMethod(context.Request.Method) if (Helpers.IsGetOrHeadMethod(context.Request.Method)
&& Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath) && Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath)
&& TryGetDirectoryInfo(subpath, out contents)) && TryGetDirectoryInfo(subpath, out contents))
@ -70,9 +70,10 @@ namespace Microsoft.AspNet.StaticFiles
return _next(context); 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() public bool LookupFileInfo()
{ {
bool found = _options.FileSystem.TryGetFileInfo(_subPath.Value, out _fileInfo); _fileInfo = _options.FileSystem.GetFileInfo(_subPath.Value);
if (found) if (_fileInfo.Exists)
{ {
_length = _fileInfo.Length; _length = _fileInfo.Length;
@ -130,7 +130,7 @@ namespace Microsoft.AspNet.StaticFiles
_etag = Convert.ToString(etagHash, 16); _etag = Convert.ToString(etagHash, 16);
_etagQuoted = '\"' + _etag + '\"'; _etagQuoted = '\"' + _etag + '\"';
} }
return found; return _fileInfo.Exists;
} }
public void ComprehendRequestHeaders() public void ComprehendRequestHeaders()