forked from amkovkov/GranuSightSoftware2
feat: ImageStorageService add sub dirs
This commit is contained in:
@@ -22,10 +22,10 @@ public class ImageStorageService
|
||||
_logger.LogInformation("Initialized");
|
||||
}
|
||||
|
||||
public async Task<string> SaveAsync(Stream fileStream, string extension, CancellationToken ct = default)
|
||||
public async Task<string> SaveAsync(Stream fileStream, string extension, string subDirectory, CancellationToken ct = default)
|
||||
{
|
||||
var fileName = GenerateFileName(extension);
|
||||
var fullPath = GetFullPath(fileName);
|
||||
var fullPath = GetFullPath(fileName, subDirectory);
|
||||
|
||||
var directory = Path.GetDirectoryName(fullPath)!;
|
||||
Directory.CreateDirectory(directory);
|
||||
@@ -45,15 +45,15 @@ public class ImageStorageService
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public Stream OpenRead(string fileName)
|
||||
public Stream OpenRead(string fileName, string subDirectory)
|
||||
{
|
||||
var fullPath = GetFullPath(fileName);
|
||||
var fullPath = GetFullPath(fileName, subDirectory);
|
||||
return new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
}
|
||||
|
||||
public bool Delete(string fileName)
|
||||
public bool Delete(string fileName, string subDirectory)
|
||||
{
|
||||
var fullPath = GetFullPath(fileName);
|
||||
var fullPath = GetFullPath(fileName, subDirectory);
|
||||
|
||||
if (!File.Exists(fullPath))
|
||||
return false;
|
||||
@@ -68,11 +68,11 @@ public class ImageStorageService
|
||||
return $"{guid}{extension}";
|
||||
}
|
||||
|
||||
public string GetFullPath(string fileName)
|
||||
public string GetFullPath(string fileName, string subDirectory)
|
||||
{
|
||||
var level1 = fileName.Substring(0, 2);
|
||||
var level2 = fileName.Substring(2, 2);
|
||||
|
||||
return Path.Combine(_rootDirectory.FullName, level1, level2, fileName);
|
||||
return Path.Combine(_rootDirectory.FullName, subDirectory, level1, level2, fileName);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user