Skip to content

Commit

Permalink
fix: fix up drive/directory lookup in APIv3
Browse files Browse the repository at this point in the history
  • Loading branch information
revam committed Aug 20, 2024
1 parent 4197d16 commit 49c07fc
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions Shoko.Server/API/v3/Controllers/FolderController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Shoko.Server.API.Annotations;
using Shoko.Server.API.v3.Models.Shoko;
using Shoko.Server.Settings;
Expand All @@ -16,8 +17,10 @@ namespace Shoko.Server.API.v3.Controllers;
[Authorize]
public class FolderController : BaseController
{
private static HashSet<string> ExcludedFormats = new()
{
private readonly ILogger<FolderController> _logger;

private static readonly HashSet<string> _excludedFormats =
[
"msdos", // fat32 - might be overkill, but the esp (u)efi partition is usually formatted as such.
"ramfs",
"configfs",
Expand All @@ -35,7 +38,12 @@ public class FolderController : BaseController
"proc",
"tmpfs",
"sysfs",
};
];

public FolderController(ILogger<FolderController> logger, ISettingsProvider settingsProvider) : base(settingsProvider)
{
_logger = logger;
}

[HttpGet("MountPoints")]
[HttpGet("Drives")]
Expand All @@ -52,8 +60,9 @@ public ActionResult<IEnumerable<Drive>> GetMountPoints()
{
fullName = d.RootDirectory.FullName;
}
catch
catch (Exception ex)
{
_logger.LogError(ex, "An exception occurred while trying to get the full name of the drive: {ex}", ex.Message);
return null;
}

Expand All @@ -62,12 +71,13 @@ public ActionResult<IEnumerable<Drive>> GetMountPoints()
{
driveFormat = d.DriveFormat;
}
catch
catch (Exception ex)
{
_logger.LogError("An exception occurred while trying to get the drive format of the drive: {ex}", ex.Message);
return null;
}

foreach (var format in ExcludedFormats)
foreach (var format in _excludedFormats)
{
if (driveFormat == format)
return null;
Expand All @@ -84,8 +94,9 @@ public ActionResult<IEnumerable<Drive>> GetMountPoints()
}
: null;
}
catch (UnauthorizedAccessException)
catch (Exception ex)
{
_logger.LogError(ex, "An exception occurred while trying to get the child items of the drive: {ex}", ex.Message);
}

return new Drive()
Expand Down Expand Up @@ -118,20 +129,18 @@ public ActionResult<IEnumerable<Folder>> GetFolder([FromQuery] string path)
{
childItems = new ChildItems()
{
Files = dir.GetFiles()?.Length ?? 0, Folders = dir.GetDirectories()?.Length ?? 0
Files = dir.GetFiles()?.Length ?? 0,
Folders = dir.GetDirectories()?.Length ?? 0
};
}
catch (UnauthorizedAccessException)
catch (Exception ex)
{
_logger.LogError(ex, "An exception occurred while trying to get the child items of the directory: {ex}", ex.Message);
}

return new Folder() { Path = dir.FullName, IsAccessible = childItems != null, Sizes = childItems };
})
.OrderBy(folder => folder.Path)
.ToList();
}

public FolderController(ISettingsProvider settingsProvider) : base(settingsProvider)
{
}
}

0 comments on commit 49c07fc

Please sign in to comment.