refactor namespaces and imports (#670)
* re-namespace * optimize usings * more usings * more of the same * more implicit/global usings * cleanup all usings * minor fixes
This commit is contained in:
@@ -1,127 +1,120 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using ErsatzTV.Core.Domain;
|
||||
using ErsatzTV.Core.Domain;
|
||||
using ErsatzTV.Core.Interfaces.Metadata;
|
||||
using LanguageExt;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using static LanguageExt.Prelude;
|
||||
|
||||
namespace ErsatzTV.Core.Metadata
|
||||
namespace ErsatzTV.Core.Metadata;
|
||||
|
||||
public class LocalFileSystem : ILocalFileSystem
|
||||
{
|
||||
public class LocalFileSystem : ILocalFileSystem
|
||||
private readonly ILogger<LocalFileSystem> _logger;
|
||||
|
||||
public LocalFileSystem(ILogger<LocalFileSystem> logger)
|
||||
{
|
||||
private readonly ILogger<LocalFileSystem> _logger;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public LocalFileSystem(ILogger<LocalFileSystem> logger)
|
||||
public Unit EnsureFolderExists(string folder)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger = logger;
|
||||
if (folder != null && !Directory.Exists(folder))
|
||||
{
|
||||
Directory.CreateDirectory(folder);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to ensure folder exists at {Folder}", folder);
|
||||
}
|
||||
|
||||
public Unit EnsureFolderExists(string folder)
|
||||
return Unit.Default;
|
||||
}
|
||||
|
||||
public DateTime GetLastWriteTime(string path) =>
|
||||
Try(File.GetLastWriteTimeUtc(path)).IfFail(() => SystemTime.MinValueUtc);
|
||||
|
||||
public bool IsLibraryPathAccessible(LibraryPath libraryPath) =>
|
||||
Directory.Exists(libraryPath.Path);
|
||||
|
||||
public IEnumerable<string> ListSubdirectories(string folder)
|
||||
{
|
||||
if (Directory.Exists(folder))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (folder != null && !Directory.Exists(folder))
|
||||
{
|
||||
Directory.CreateDirectory(folder);
|
||||
}
|
||||
return Directory.EnumerateDirectories(folder);
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to ensure folder exists at {Folder}", folder);
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
public IEnumerable<string> ListFiles(string folder)
|
||||
{
|
||||
if (Directory.Exists(folder))
|
||||
{
|
||||
try
|
||||
{
|
||||
return Directory.EnumerateFiles(folder, "*", SearchOption.TopDirectoryOnly);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
public bool FileExists(string path) => File.Exists(path);
|
||||
|
||||
public bool FolderExists(string folder) => Directory.Exists(folder);
|
||||
|
||||
public async Task<Either<BaseError, Unit>> CopyFile(string source, string destination)
|
||||
{
|
||||
try
|
||||
{
|
||||
string directory = Path.GetDirectoryName(destination) ?? string.Empty;
|
||||
if (!Directory.Exists(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
await using FileStream sourceStream = File.OpenRead(source);
|
||||
await using FileStream destinationStream = File.Create(destination);
|
||||
await sourceStream.CopyToAsync(destinationStream);
|
||||
|
||||
return Unit.Default;
|
||||
}
|
||||
|
||||
public DateTime GetLastWriteTime(string path) =>
|
||||
Try(File.GetLastWriteTimeUtc(path)).IfFail(() => SystemTime.MinValueUtc);
|
||||
|
||||
public bool IsLibraryPathAccessible(LibraryPath libraryPath) =>
|
||||
Directory.Exists(libraryPath.Path);
|
||||
|
||||
public IEnumerable<string> ListSubdirectories(string folder)
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (Directory.Exists(folder))
|
||||
{
|
||||
try
|
||||
{
|
||||
return Directory.EnumerateDirectories(folder);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
public IEnumerable<string> ListFiles(string folder)
|
||||
{
|
||||
if (Directory.Exists(folder))
|
||||
{
|
||||
try
|
||||
{
|
||||
return Directory.EnumerateFiles(folder, "*", SearchOption.TopDirectoryOnly);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
public bool FileExists(string path) => File.Exists(path);
|
||||
|
||||
public bool FolderExists(string folder) => Directory.Exists(folder);
|
||||
|
||||
public async Task<Either<BaseError, Unit>> CopyFile(string source, string destination)
|
||||
{
|
||||
try
|
||||
{
|
||||
string directory = Path.GetDirectoryName(destination) ?? string.Empty;
|
||||
if (!Directory.Exists(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
await using FileStream sourceStream = File.OpenRead(source);
|
||||
await using FileStream destinationStream = File.Create(destination);
|
||||
await sourceStream.CopyToAsync(destinationStream);
|
||||
|
||||
return Unit.Default;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BaseError.New(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public Unit EmptyFolder(string folder)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (string file in Directory.GetFiles(folder))
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
|
||||
foreach (string directory in Directory.GetDirectories(folder))
|
||||
{
|
||||
Directory.Delete(directory, true);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to empty folder at {Folder}", folder);
|
||||
}
|
||||
|
||||
return Unit.Default;
|
||||
return BaseError.New(ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Unit EmptyFolder(string folder)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (string file in Directory.GetFiles(folder))
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
|
||||
foreach (string directory in Directory.GetDirectories(folder))
|
||||
{
|
||||
Directory.Delete(directory, true);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to empty folder at {Folder}", folder);
|
||||
}
|
||||
|
||||
return Unit.Default;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user