mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 08:23:12 +00:00
feat(work-orders): add file storage OpenRead port
This commit is contained in:
parent
02df093798
commit
bb651bb547
2 changed files with 33 additions and 0 deletions
|
|
@ -70,6 +70,33 @@ namespace Api.SeaHavenIndustries.Infrastructure
|
|||
}
|
||||
}
|
||||
|
||||
public Stream? OpenRead(string fileUrl)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(fileUrl))
|
||||
return null;
|
||||
|
||||
try
|
||||
{
|
||||
if (!Uri.TryCreate(fileUrl, UriKind.Absolute, out var uri))
|
||||
return null;
|
||||
|
||||
var relativePath = uri.AbsolutePath.TrimStart('/');
|
||||
if (string.IsNullOrWhiteSpace(relativePath)
|
||||
|| relativePath.Contains("..", StringComparison.Ordinal)
|
||||
|| !relativePath.StartsWith("Assets/Documents/", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var fullPath = Path.Combine(ResolveWebRoot(), relativePath.Replace('/', Path.DirectorySeparatorChar));
|
||||
return System.IO.File.Exists(fullPath) ? System.IO.File.OpenRead(fullPath) : null;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private string ResolveWebRoot()
|
||||
=> _webHostEnvironment.WebRootPath
|
||||
?? Path.Combine(_webHostEnvironment.ContentRootPath, "wwwroot");
|
||||
|
|
|
|||
|
|
@ -24,6 +24,12 @@ namespace SeaHaven.Services.Interfaces
|
|||
/// cannot be resolved or removed; never throws for missing paths.
|
||||
/// </summary>
|
||||
bool TryDelete(string fileUrl);
|
||||
|
||||
/// <summary>
|
||||
/// Opens a previously saved file URL for reading. Returns null when the URL or file
|
||||
/// cannot be safely resolved.
|
||||
/// </summary>
|
||||
Stream? OpenRead(string fileUrl);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue