Skip to content

Commit

Permalink
refactor FileHelper.cs file
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Sep 18, 2024
1 parent de6461f commit 51b9239
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/Downloader/Extensions/Helpers/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ public static Stream CreateFile(string filename)
Directory.CreateDirectory(directory);
}

return new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete);
}
public static string GetTempFile()
{
return GetTempFile(Path.GetTempPath(), string.Empty);
return new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write,
FileShare.ReadWrite | FileShare.Delete);
}

public static string GetTempFile(string baseDirectory, string fileExtension)
{
if (string.IsNullOrWhiteSpace(baseDirectory))
Expand All @@ -41,13 +39,8 @@ public static long GetAvailableFreeSpaceOnDisk(string directory)
{
try
{
var drive = new DriveInfo(directory);
if (drive.IsReady)
{
return drive.AvailableFreeSpace;
}

return 0L;
DriveInfo drive = new(directory);
return drive.IsReady ? drive.AvailableFreeSpace : 0L;
}
catch (ArgumentException)
{
Expand All @@ -58,15 +51,16 @@ public static long GetAvailableFreeSpaceOnDisk(string directory)

public static void ThrowIfNotEnoughSpace(long actualNeededSize, params string[] directories)
{
if (directories != null)
if (directories == null)
return;

foreach (string directory in directories)
{
foreach (string directory in directories)
long availableFreeSpace = GetAvailableFreeSpaceOnDisk(directory);
if (availableFreeSpace > 0 && availableFreeSpace < actualNeededSize)
{
var availableFreeSpace = GetAvailableFreeSpaceOnDisk(directory);
if (availableFreeSpace > 0 && availableFreeSpace < actualNeededSize)
{
throw new IOException($"There is not enough space on the disk `{directory}` with {availableFreeSpace} bytes");
}
throw new IOException($"There is not enough space on the disk `{directory}` " +
$"with {availableFreeSpace} bytes");
}
}
}
Expand Down

0 comments on commit 51b9239

Please sign in to comment.