forked from amkovkov/GranuSightSoftware2
feat: ImageStorageService add sub dirs
This commit is contained in:
@@ -22,10 +22,10 @@ public class ImageStorageService
|
|||||||
_logger.LogInformation("Initialized");
|
_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 fileName = GenerateFileName(extension);
|
||||||
var fullPath = GetFullPath(fileName);
|
var fullPath = GetFullPath(fileName, subDirectory);
|
||||||
|
|
||||||
var directory = Path.GetDirectoryName(fullPath)!;
|
var directory = Path.GetDirectoryName(fullPath)!;
|
||||||
Directory.CreateDirectory(directory);
|
Directory.CreateDirectory(directory);
|
||||||
@@ -45,15 +45,15 @@ public class ImageStorageService
|
|||||||
return fileName;
|
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);
|
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))
|
if (!File.Exists(fullPath))
|
||||||
return false;
|
return false;
|
||||||
@@ -68,11 +68,11 @@ public class ImageStorageService
|
|||||||
return $"{guid}{extension}";
|
return $"{guid}{extension}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetFullPath(string fileName)
|
public string GetFullPath(string fileName, string subDirectory)
|
||||||
{
|
{
|
||||||
var level1 = fileName.Substring(0, 2);
|
var level1 = fileName.Substring(0, 2);
|
||||||
var level2 = fileName.Substring(2, 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