feat(work-orders): add file storage OpenRead port

This commit is contained in:
Arthur Bassi 2026-09-08 11:08:26 -03:00
parent 02df093798
commit bb651bb547
2 changed files with 33 additions and 0 deletions

View file

@ -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");

View file

@ -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>