forked from amkovkov/GranuSightSoftware2
feat: ImageSotrage clear
This commit is contained in:
@@ -26,6 +26,7 @@ public class ImageStorageService
|
||||
{
|
||||
var fileName = GenerateFileName(extension);
|
||||
var fullPath = GetFullPath(fileName, subDirectory);
|
||||
System.Diagnostics.Debug.Assert(fullPath is not null);
|
||||
|
||||
var directory = Path.GetDirectoryName(fullPath)!;
|
||||
Directory.CreateDirectory(directory);
|
||||
@@ -45,20 +46,25 @@ public class ImageStorageService
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public Stream OpenRead(string fileName, string subDirectory)
|
||||
public Stream OpenRead(string fileName, string? subDirectory = null)
|
||||
{
|
||||
var fullPath = GetFullPath(fileName, subDirectory);
|
||||
if (fullPath is null)
|
||||
throw new FileNotFoundException(null, fullPath);
|
||||
return new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
}
|
||||
|
||||
public bool Delete(string fileName, string subDirectory)
|
||||
public bool Delete(string fileName, string? subDirectory = null)
|
||||
{
|
||||
var fullPath = GetFullPath(fileName, subDirectory);
|
||||
if (fullPath is null)
|
||||
throw new FileNotFoundException(null, fullPath);
|
||||
|
||||
if (!File.Exists(fullPath))
|
||||
return false;
|
||||
|
||||
File.Delete(fullPath);
|
||||
_logger.LogInformation("File removed: {}", fileName);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -68,11 +74,29 @@ public class ImageStorageService
|
||||
return $"{guid}{extension}";
|
||||
}
|
||||
|
||||
public string GetFullPath(string fileName, string subDirectory)
|
||||
public string? GetFullPath(string fileName, string? subDirectory = null)
|
||||
{
|
||||
var level1 = fileName.Substring(0, 2);
|
||||
var level2 = fileName.Substring(2, 2);
|
||||
|
||||
return Path.Combine(_rootDirectory.FullName, subDirectory, level1, level2, fileName);
|
||||
if (subDirectory is not null)
|
||||
return Path.Combine(_rootDirectory.FullName, subDirectory, level1, level2, fileName);
|
||||
|
||||
return ListFiles()
|
||||
.FirstOrDefault(File.Exists);
|
||||
}
|
||||
|
||||
public List<string> ListFiles(string? subDirectory = null)
|
||||
{
|
||||
if (subDirectory is not null)
|
||||
return Directory.GetDirectories(Path.Join(_rootDirectory.FullName, subDirectory))
|
||||
.SelectMany(Directory.GetDirectories)
|
||||
.SelectMany(Directory.GetFiles)
|
||||
.ToList();
|
||||
|
||||
return Directory.GetDirectories(_rootDirectory.FullName)
|
||||
.Select(Path.GetFileName)
|
||||
.SelectMany(ListFiles)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user