From a4eede29abf1cf380c959835e59b7062ead1a654 Mon Sep 17 00:00:00 2001
From: Bond_009 <bond.009@outlook.com>
Date: Sat, 25 Sep 2021 20:17:12 +0200
Subject: [PATCH] Use RandomAccess instead of a FileStream where it makes sense

---
 Emby.Server.Implementations/IO/ManagedFileSystem.cs           | 4 ++--
 .../SymlinkFollowingPhysicalFileResultExecutor.cs             | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/Emby.Server.Implementations/IO/ManagedFileSystem.cs b/Emby.Server.Implementations/IO/ManagedFileSystem.cs
index 1bc229b0ca..77da46cd6d 100644
--- a/Emby.Server.Implementations/IO/ManagedFileSystem.cs
+++ b/Emby.Server.Implementations/IO/ManagedFileSystem.cs
@@ -246,9 +246,9 @@ namespace Emby.Server.Implementations.IO
                     {
                         try
                         {
-                            using (Stream thisFileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 1))
+                            using (var fileHandle = File.OpenHandle(fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                             {
-                                result.Length = thisFileStream.Length;
+                                result.Length = RandomAccess.GetLength(fileHandle);
                             }
                         }
                         catch (FileNotFoundException ex)
diff --git a/Jellyfin.Server/Infrastructure/SymlinkFollowingPhysicalFileResultExecutor.cs b/Jellyfin.Server/Infrastructure/SymlinkFollowingPhysicalFileResultExecutor.cs
index 4abd5b36d1..2296ac5c8a 100644
--- a/Jellyfin.Server/Infrastructure/SymlinkFollowingPhysicalFileResultExecutor.cs
+++ b/Jellyfin.Server/Infrastructure/SymlinkFollowingPhysicalFileResultExecutor.cs
@@ -55,8 +55,8 @@ namespace Jellyfin.Server.Infrastructure
             // This may or may not be fixed in .NET 6, but looks like it will not https://github.com/dotnet/aspnetcore/issues/34371
             if ((fileInfo.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
             {
-                using Stream thisFileStream = File.OpenRead(path);
-                length = thisFileStream.Length;
+                using var fileHandle = File.OpenHandle(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
+                length = RandomAccess.GetLength(fileHandle);
             }
 
             return new FileMetadata