Reputation: 504
ASP.NET Core 2.2.0
It looks like File.Exists() doesn't work when you're using a static file folder. My source:
Startup.cs - Configure()
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider("C:\\TEMP"),
RequestPath = PathString.FromUriComponent("/sub1/sub2")
});
In C:\TEMP
I have a file test.txt
.
When I open https://localhost/sub1/sub2/test.txt
I get the file from C:\TEMP
. So far so good.
But If I want to check if the file exists, I always get false. Using this:
File.Exists("/sub1/sub2/test.txt")
Anybody a solution how to check a file existance on static files?
Upvotes: 1
Views: 2201
Reputation: 25350
Not sure why you need detect File.Exists
, but if would like to do that, you could reuse the StaticFileOptions
& also the FileProvider
to detect whether a file exists.
Create a StaticFileDetector
service as below :
public class MyStaticFileOptions : StaticFileOptions { }
public class StaticFileDetector{
private StaticFileOptions _sfo;
public StaticFileDetector(IOptions<MyStaticFileOptions> sfo)
{
this._sfo = sfo.Value;
}
public bool FileExists(PathString path){
// you might custom it as you like
var baseUrl = this._sfo.RequestPath;
// get the relative path
PathString relativePath = null;
if(!path.StartsWithSegments(baseUrl, out relativePath))
{
return false;
}
var file = this._sfo.FileProvider.GetFileInfo(relativePath.Value);
return !file.IsDirectory && file.Exists;
}
}
and then register them as services :
public void ConfigureServices(IServiceCollection services)
{
// ...
services.Configure<MyStaticFileOptions>(o => {
o.FileProvider = new PhysicalFileProvider("C:\\TEMP");
o.RequestPath = PathString.FromUriComponent("/sub1/sub2");
});
services.AddSingleton<StaticFileDetector>()
}
Finally, suppose you want to detect whether a request path will be processed :
app.Use(async(ctx,next) => {
var path = ctx.Request.Path;
var detector = app.ApplicationServices.GetService<StaticFileDetector>();
var exists = detector.FileExists(path);
// now you know whether current request path exists or not.
await next();
});
app.UseStaticFiles();
app.UseStaticFiles(app.ApplicationServices.GetService<IOptions<MyStaticFileOptions>>().Value);
// ...
Or if you would like to detect some path within a Controller, simply inject this service as below :
public class HomeController : Controller
{
private StaticFileDetector _dector;
public HomeController(StaticFileDetector dector)
{
this._dector = dector;
}
public IActionResult Index()
{
var path = new PathString("some-path-here");
var x = this._dector.FileExists(path);
return Json(x);
}
}
[Edit] : Avoid using File.Exists(unsafe_path_string)
directly, it not safe if you pass an unsafe path string.
Upvotes: 0
Reputation: 16449
If you would have read the description of File.Exists method:
true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. This method also returns false if the path is null, an invalid path, or a zero-length string. If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of a path.
Also it says the following:
The Exists method should not be used for path validation, this method merely checks if the file specified in path exists. Passing an invalid path to Exists returns false. To check whether the path contains any invalid characters, you can call the GetInvalidPathChars method to retrieve the characters that are invalid for the file system. You can also create a regular expression to test whether the path is valid for your environment. For examples of acceptable paths, see File.
Hence, your scenario does not match with this
Upvotes: 1