Skip to content
This repository was archived by the owner on Nov 22, 2018. It is now read-only.

Commit 1bc4e21

Browse files
author
Praburaj
committed
Addressing IFileSystem breaking changes
This PR is to address the breaking changes introduced with aspnet/FileSystem#20 @Tratcher @pranavkm
1 parent fb9fc12 commit 1bc4e21

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

src/Microsoft.AspNet.StaticFiles/DefaultFilesMiddleware.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,34 @@ public DefaultFilesMiddleware([NotNull] RequestDelegate next, [NotNull] IHosting
4848
/// <returns></returns>
4949
public Task Invoke(HttpContext context)
5050
{
51-
IEnumerable<IFileInfo> dirContents;
5251
PathString subpath;
5352
if (Helpers.IsGetOrHeadMethod(context.Request.Method)
54-
&& Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath)
55-
&& _options.FileSystem.TryGetDirectoryContents(subpath.Value, out dirContents))
53+
&& Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath))
5654
{
57-
// Check if any of our default files exist.
58-
for (int matchIndex = 0; matchIndex < _options.DefaultFileNames.Count; matchIndex++)
55+
var dirContents = _options.FileSystem.GetDirectoryContents(subpath.Value);
56+
if (dirContents.Exists)
5957
{
60-
string defaultFile = _options.DefaultFileNames[matchIndex];
61-
IFileInfo file;
62-
// TryMatchPath will make sure subpath always ends with a "/" by adding it if needed.
63-
if (_options.FileSystem.TryGetFileInfo(subpath + defaultFile, out file))
58+
// Check if any of our default files exist.
59+
for (int matchIndex = 0; matchIndex < _options.DefaultFileNames.Count; matchIndex++)
6460
{
65-
// If the path matches a directory but does not end in a slash, redirect to add the slash.
66-
// This prevents relative links from breaking.
67-
if (!Helpers.PathEndsInSlash(context.Request.Path))
61+
string defaultFile = _options.DefaultFileNames[matchIndex];
62+
var file = _options.FileSystem.GetFileInfo(subpath + defaultFile);
63+
// TryMatchPath will make sure subpath always ends with a "/" by adding it if needed.
64+
if (file.Exists)
6865
{
69-
context.Response.StatusCode = 301;
70-
context.Response.Headers[Constants.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
71-
return Constants.CompletedTask;
72-
}
66+
// If the path matches a directory but does not end in a slash, redirect to add the slash.
67+
// This prevents relative links from breaking.
68+
if (!Helpers.PathEndsInSlash(context.Request.Path))
69+
{
70+
context.Response.StatusCode = 301;
71+
context.Response.Headers[Constants.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
72+
return Constants.CompletedTask;
73+
}
7374

74-
// Match found, re-write the url. A later middleware will actually serve the file.
75-
context.Request.Path = new PathString(context.Request.Path.Value + defaultFile);
76-
break;
75+
// Match found, re-write the url. A later middleware will actually serve the file.
76+
context.Request.Path = new PathString(context.Request.Path.Value + defaultFile);
77+
break;
78+
}
7779
}
7880
}
7981
}

src/Microsoft.AspNet.StaticFiles/DirectoryBrowserMiddleware.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public Task Invoke(HttpContext context)
5050
{
5151
// Check if the URL matches any expected paths
5252
PathString subpath;
53-
IEnumerable<IFileInfo> contents;
53+
IDirectoryContents contents;
5454
if (Helpers.IsGetOrHeadMethod(context.Request.Method)
5555
&& Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out subpath)
5656
&& TryGetDirectoryInfo(subpath, out contents))
@@ -70,9 +70,10 @@ public Task Invoke(HttpContext context)
7070
return _next(context);
7171
}
7272

73-
private bool TryGetDirectoryInfo(PathString subpath, out IEnumerable<IFileInfo> contents)
73+
private bool TryGetDirectoryInfo(PathString subpath, out IDirectoryContents contents)
7474
{
75-
return _options.FileSystem.TryGetDirectoryContents(subpath.Value, out contents);
75+
contents = _options.FileSystem.GetDirectoryContents(subpath.Value);
76+
return contents.Exists;
7677
}
7778
}
7879
}

src/Microsoft.AspNet.StaticFiles/StaticFileContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public bool LookupContentType()
116116

117117
public bool LookupFileInfo()
118118
{
119-
bool found = _options.FileSystem.TryGetFileInfo(_subPath.Value, out _fileInfo);
120-
if (found)
119+
_fileInfo = _options.FileSystem.GetFileInfo(_subPath.Value);
120+
if (_fileInfo.Exists)
121121
{
122122
_length = _fileInfo.Length;
123123

@@ -130,7 +130,7 @@ public bool LookupFileInfo()
130130
_etag = Convert.ToString(etagHash, 16);
131131
_etagQuoted = '\"' + _etag + '\"';
132132
}
133-
return found;
133+
return _fileInfo.Exists;
134134
}
135135

136136
public void ComprehendRequestHeaders()

0 commit comments

Comments
 (0)