Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Increased maximum volume label length to 128 characters for UDF images #12931

Merged
merged 4 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Files.App/Data/Items/DriveItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,19 @@ public DriveItem()
ItemType = NavigationControlItemType.CloudDrive;
}

public static async Task<DriveItem> CreateFromPropertiesAsync(StorageFolder root, string deviceId, DriveType type, IRandomAccessStream imageStream = null)
public static async Task<DriveItem> CreateFromPropertiesAsync(StorageFolder root, string deviceId, string label, DriveType type, IRandomAccessStream imageStream = null)
{
var item = new DriveItem();

if (imageStream is not null)
item.IconData = await imageStream.ToByteArrayAsync();

item.Text = type is DriveType.Network ? $"{root.DisplayName} ({deviceId})" : root.DisplayName;
item.Text = type switch
{
DriveType.Network => $"{root.DisplayName} ({deviceId})",
DriveType.CDRom when !string.IsNullOrEmpty(label) => root.DisplayName.Replace(label.Left(32), label),
_ => root.DisplayName
};
item.Type = type;
item.MenuOptions = new ContextMenuOptions
{
Expand Down Expand Up @@ -248,7 +253,7 @@ public async Task LoadThumbnailAsync(bool isSidebar = false)
{
if (!string.IsNullOrEmpty(DeviceID) && !string.Equals(DeviceID, "network-folder"))
IconData ??= await FileThumbnailHelper.LoadIconWithoutOverlayAsync(DeviceID, 24);

if (Root is not null)
{
using var thumbnail = await DriveHelpers.GetThumbnailAsync(Root);
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Files.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<ItemGroup>
<PackageReference Include="ByteSize" Version="2.1.1" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
<PackageReference Include="DiscUtils.Udf" Version="0.16.13" />
<PackageReference Include="FluentFTP" Version="43.0.1" />
<PackageReference Include="ini-parser-netstandard" Version="2.5.2" />
<PackageReference Include="LibGit2Sharp" Version="0.27.2" />
Expand Down
16 changes: 16 additions & 0 deletions src/Files.App/Helpers/Storage/DriveHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Windows.Devices.Portable;
using Windows.Storage;
using Windows.Storage.FileProperties;
using DiscUtils.Udf;

namespace Files.App.Helpers
{
Expand Down Expand Up @@ -136,6 +137,21 @@ public static Data.Items.DriveType GetDriveType(System.IO.DriveInfo drive)
};
}

public static string GetExtendedDriveLabel(DriveInfo drive)
{
if (drive.DriveType is not System.IO.DriveType.CDRom || drive.DriveFormat is not "UDF")
return drive.VolumeLabel;
return SafetyExtensions.IgnoreExceptions(() =>
{
var dosDevicePath = Vanara.PInvoke.Kernel32.QueryDosDevice(drive.Name).FirstOrDefault();
if (string.IsNullOrEmpty(dosDevicePath))
return drive.VolumeLabel;
using var driveStream = new FileStream(dosDevicePath.Replace(@"\Device\", @"\\.\"), FileMode.Open, FileAccess.Read);
using var udf = new UdfReader(driveStream);
return udf.VolumeLabel;
}) ?? drive.VolumeLabel;
}

public static async Task<StorageItemThumbnail> GetThumbnailAsync(StorageFolder folder)
=> (StorageItemThumbnail)await FilesystemTasks.Wrap(()
=> folder.GetThumbnailAsync(ThumbnailMode.SingleItem, 40, ThumbnailOptions.UseCurrentScale).AsTask()
Expand Down
3 changes: 2 additions & 1 deletion src/Files.App/Services/RemovableDrivesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public async IAsyncEnumerable<ILocatableFolder> GetDrivesAsync()

using var thumbnail = await DriveHelpers.GetThumbnailAsync(res.Result);
var type = DriveHelpers.GetDriveType(drive);
var driveItem = await DriveItem.CreateFromPropertiesAsync(res.Result, drive.Name.TrimEnd('\\'), type, thumbnail);
var label = DriveHelpers.GetExtendedDriveLabel(drive);
var driveItem = await DriveItem.CreateFromPropertiesAsync(res.Result, drive.Name.TrimEnd('\\'), label, type, thumbnail);

App.Logger.LogInformation($"Drive added: {driveItem.Path}, {driveItem.Type}");

Expand Down
8 changes: 6 additions & 2 deletions src/Files.App/Utils/WindowsStorageDeviceWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ private async void Win32_OnDeviceAdded(object? sender, DeviceEventArgs e)
}

var type = DriveHelpers.GetDriveType(driveAdded);
DriveItem driveItem = await DriveItem.CreateFromPropertiesAsync(rootAdded, e.DeviceId, type);
var label = DriveHelpers.GetExtendedDriveLabel(driveAdded);
DriveItem driveItem = await DriveItem.CreateFromPropertiesAsync(rootAdded, e.DeviceId, label, type);

DeviceAdded?.Invoke(this, driveItem);
}
Expand Down Expand Up @@ -95,18 +96,21 @@ private async void Watcher_Added(DeviceWatcher sender, DeviceInformation args)
}

Data.Items.DriveType type;
string label;
try
{
// Check if this drive is associated with a drive letter
var driveAdded = new DriveInfo(root.Path);
type = DriveHelpers.GetDriveType(driveAdded);
label = DriveHelpers.GetExtendedDriveLabel(driveAdded);
}
catch (ArgumentException)
{
type = Data.Items.DriveType.Removable;
label = string.Empty;
}

var driveItem = await DriveItem.CreateFromPropertiesAsync(root, deviceId, type);
var driveItem = await DriveItem.CreateFromPropertiesAsync(root, deviceId, label, type);

DeviceAdded?.Invoke(this, driveItem);
}
Expand Down