@@ -26,6 +26,7 @@ public class AzureBlobFileSystemMiddleware : IMiddleware
26
26
private readonly string _name ;
27
27
private readonly IAzureBlobFileSystemProvider _fileSystemProvider ;
28
28
private string _rootPath ;
29
+ private string _containerRootPath ;
29
30
private readonly TimeSpan ? _maxAge = TimeSpan . FromDays ( 7 ) ;
30
31
31
32
/// <summary>
@@ -63,6 +64,7 @@ protected AzureBlobFileSystemMiddleware(string name, IOptionsMonitor<AzureBlobFi
63
64
64
65
var fileSystemOptions = options . Get ( name ) ;
65
66
_rootPath = hostingEnvironment . ToAbsolute ( fileSystemOptions . VirtualPath ) ;
67
+ _containerRootPath = fileSystemOptions . ContainerRootPath ?? _rootPath ;
66
68
67
69
options . OnChange ( ( o , n ) => OptionsOnChange ( o , n , hostingEnvironment ) ) ;
68
70
}
@@ -87,7 +89,8 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
87
89
return ;
88
90
}
89
91
90
- var blob = _fileSystemProvider . GetFileSystem ( _name ) . GetBlobClient ( request . Path ) ;
92
+ string containerPath = $ "{ _containerRootPath . TrimEnd ( '/' ) } /{ ( request . Path . Value . Remove ( 0 , _rootPath . Length ) ) . TrimStart ( '/' ) } ";
93
+ var blob = _fileSystemProvider . GetFileSystem ( _name ) . GetBlobClient ( containerPath ) ;
91
94
92
95
var blobRequestConditions = GetAccessCondition ( context . Request ) ;
93
96
@@ -98,13 +101,13 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
98
101
{
99
102
properties = await blob . GetPropertiesAsync ( blobRequestConditions , context . RequestAborted ) . ConfigureAwait ( false ) ;
100
103
}
101
- catch ( RequestFailedException ex ) when ( ex . Status == ( int ) HttpStatusCode . NotFound )
104
+ catch ( RequestFailedException ex ) when ( ex . Status == ( int ) HttpStatusCode . NotFound )
102
105
{
103
106
// the blob or file does not exist, let other middleware handle it
104
107
await next ( context ) . ConfigureAwait ( false ) ;
105
108
return ;
106
109
}
107
- catch ( RequestFailedException ex ) when ( ex . Status == ( int ) HttpStatusCode . PreconditionFailed )
110
+ catch ( RequestFailedException ex ) when ( ex . Status == ( int ) HttpStatusCode . PreconditionFailed )
108
111
{
109
112
// If-Range or If-Unmodified-Since is not met
110
113
// if the resource has been modified, we need to send the whole file back with a 200 OK
@@ -113,12 +116,12 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
113
116
properties = await blob . GetPropertiesAsync ( ) . ConfigureAwait ( false ) ;
114
117
response . Headers . Append ( "Content-Range" , $ "bytes */{ properties . Value . ContentLength } ") ;
115
118
}
116
- catch ( RequestFailedException ex ) when ( ex . Status == ( int ) HttpStatusCode . NotModified )
119
+ catch ( RequestFailedException ex ) when ( ex . Status == ( int ) HttpStatusCode . NotModified )
117
120
{
118
121
// If-None-Match or If-Modified-Since is not met
119
122
// we need to pass the status code back to the client
120
123
// so it knows it can reuse the cached data
121
- response . StatusCode = ( int ) HttpStatusCode . NotModified ;
124
+ response . StatusCode = ( int ) HttpStatusCode . NotModified ;
122
125
return ;
123
126
}
124
127
// for some reason we get an internal exception type with the message
@@ -140,7 +143,7 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
140
143
// If-None-Match or If-Modified-Since is not met
141
144
// we need to pass the status code back to the client
142
145
// so it knows it can reuse the cached data
143
- response . StatusCode = ( int ) HttpStatusCode . NotModified ;
146
+ response . StatusCode = ( int ) HttpStatusCode . NotModified ;
144
147
return ;
145
148
}
146
149
}
@@ -174,7 +177,7 @@ private async Task HandleRequestAsync(HttpContext context, RequestDelegate next)
174
177
{
175
178
// no ranges could be parsed
176
179
response . Clear ( ) ;
177
- response . StatusCode = ( int ) HttpStatusCode . RequestedRangeNotSatisfiable ;
180
+ response . StatusCode = ( int ) HttpStatusCode . RequestedRangeNotSatisfiable ;
178
181
responseHeaders . ContentRange = new ContentRangeHeaderValue ( properties . Value . ContentLength ) ;
179
182
return ;
180
183
}
@@ -376,6 +379,7 @@ private void OptionsOnChange(AzureBlobFileSystemOptions options, string name, IH
376
379
if ( name != _name ) return ;
377
380
378
381
_rootPath = hostingEnvironment . ToAbsolute ( options . VirtualPath ) ;
382
+ _containerRootPath = options . ContainerRootPath ?? _rootPath ;
379
383
}
380
384
}
381
385
}
0 commit comments