The Free Software Media System - Server Backend & API
Use CryptoStream to convert stream from base64
Bond_009efc4c305a912
diff --git a/Jellyfin.Api/Controllers/ImageController.cs b/Jellyfin.Api/Controllers/ImageController.csindex 3c5f18af55..7b10ea170f 100644--- a/Jellyfin.Api/Controllers/ImageController.cs+++ b/Jellyfin.Api/Controllers/ImageController.cs@@ -7,6 +7,7 @@ using System.Globalization;using System.IO;using System.Linq;using System.Net.Mime;+using System.Security.Cryptography;using System.Threading;using System.Threading.Tasks;using Jellyfin.Api.Attributes;@@ -78,6 +79,9 @@ public class ImageController : BaseJellyfinApiController_appPaths = appPaths;}+ private static Stream GetFromBase64Stream(Stream inputStream)+ => new CryptoStream(inputStream, new FromBase64Transform(), CryptoStreamMode.Read);+/// <summary>/// Sets the user image./// </summary>@@ -116,8 +120,8 @@ public class ImageController : BaseJellyfinApiControllerreturn BadRequest("Incorrect ContentType.");}- var memoryStream = await GetMemoryStream(Request.Body).ConfigureAwait(false);- await using (memoryStream.ConfigureAwait(false))+ var stream = GetFromBase64Stream(Request.Body);+ await using (stream.ConfigureAwait(false)){// Handle image/png; charset=utf-8var mimeType = Request.ContentType?.Split(';').FirstOrDefault();@@ -130,7 +134,7 @@ public class ImageController : BaseJellyfinApiControlleruser.ProfileImage = new Data.Entities.ImageInfo(Path.Combine(userDataPath, "profile" + extension));await _providerManager- .SaveImage(memoryStream, mimeType, user.ProfileImage.Path)+ .SaveImage(stream, mimeType, user.ProfileImage.Path).ConfigureAwait(false);await _userManager.UpdateUserAsync(user).ConfigureAwait(false);@@ -176,8 +180,8 @@ public class ImageController : BaseJellyfinApiControllerreturn BadRequest("Incorrect ContentType.");}- var memoryStream = await GetMemoryStream(Request.Body).ConfigureAwait(false);- await using (memoryStream.ConfigureAwait(false))+ var stream = GetFromBase64Stream(Request.Body);+ await using (stream.ConfigureAwait(false)){// Handle image/png; charset=utf-8var mimeType = Request.ContentType?.Split(';').FirstOrDefault();@@ -190,7 +194,7 @@ public class ImageController : BaseJellyfinApiControlleruser.ProfileImage = new Data.Entities.ImageInfo(Path.Combine(userDataPath, "profile" + extension));await _providerManager- .SaveImage(memoryStream, mimeType, user.ProfileImage.Path)+ .SaveImage(stream, mimeType, user.ProfileImage.Path).ConfigureAwait(false);await _userManager.UpdateUserAsync(user).ConfigureAwait(false);@@ -372,12 +376,12 @@ public class ImageController : BaseJellyfinApiControllerreturn BadRequest("Incorrect ContentType.");}- var memoryStream = await GetMemoryStream(Request.Body).ConfigureAwait(false);- await using (memoryStream.ConfigureAwait(false))+ var stream = GetFromBase64Stream(Request.Body);+ await using (stream.ConfigureAwait(false)){// Handle image/png; charset=utf-8var mimeType = Request.ContentType?.Split(';').FirstOrDefault();- await _providerManager.SaveImage(item, memoryStream, mimeType, imageType, null, CancellationToken.None).ConfigureAwait(false);+ await _providerManager.SaveImage(item, stream, mimeType, imageType, null, CancellationToken.None).ConfigureAwait(false);await item.UpdateToRepositoryAsync(ItemUpdateType.ImageUpdate, CancellationToken.None).ConfigureAwait(false);return NoContent();@@ -416,12 +420,12 @@ public class ImageController : BaseJellyfinApiControllerreturn BadRequest("Incorrect ContentType.");}- var memoryStream = await GetMemoryStream(Request.Body).ConfigureAwait(false);- await using (memoryStream.ConfigureAwait(false))+ var stream = GetFromBase64Stream(Request.Body);+ await using (stream.ConfigureAwait(false)){// Handle image/png; charset=utf-8var mimeType = Request.ContentType?.Split(';').FirstOrDefault();- await _providerManager.SaveImage(item, memoryStream, mimeType, imageType, null, CancellationToken.None).ConfigureAwait(false);+ await _providerManager.SaveImage(item, stream, mimeType, imageType, null, CancellationToken.None).ConfigureAwait(false);await item.UpdateToRepositoryAsync(ItemUpdateType.ImageUpdate, CancellationToken.None).ConfigureAwait(false);return NoContent();@@ -1792,8 +1796,8 @@ public class ImageController : BaseJellyfinApiControllerreturn BadRequest("Incorrect ContentType.");}- var memoryStream = await GetMemoryStream(Request.Body).ConfigureAwait(false);- await using (memoryStream.ConfigureAwait(false))+ var stream = GetFromBase64Stream(Request.Body);+ await using (stream.ConfigureAwait(false)){var filePath = Path.Combine(_appPaths.DataPath, "splashscreen-upload" + extension);var brandingOptions = _serverConfigurationManager.GetConfiguration<BrandingOptions>("branding");@@ -1803,7 +1807,7 @@ public class ImageController : BaseJellyfinApiControllervar fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous);await using (fs.ConfigureAwait(false)){- await memoryStream.CopyToAsync(fs, CancellationToken.None).ConfigureAwait(false);+ await stream.CopyToAsync(fs, CancellationToken.None).ConfigureAwait(false);}return NoContent();@@ -1833,15 +1837,6 @@ public class ImageController : BaseJellyfinApiControllerreturn NoContent();}- private static async Task<MemoryStream> GetMemoryStream(Stream inputStream)- {- using var reader = new StreamReader(inputStream);- var text = await reader.ReadToEndAsync().ConfigureAwait(false);-- var bytes = Convert.FromBase64String(text);- return new MemoryStream(bytes, 0, bytes.Length, false, true);- }-private ImageInfo? GetImageInfo(BaseItem item, ItemImageInfo info, int? imageIndex){int? width = null;diff --git a/Jellyfin.Api/Controllers/SubtitleController.cs b/Jellyfin.Api/Controllers/SubtitleController.csindex 7d02550b68..fb89e96108 100644--- a/Jellyfin.Api/Controllers/SubtitleController.cs+++ b/Jellyfin.Api/Controllers/SubtitleController.cs@@ -6,6 +6,7 @@ using System.Globalization;using System.IO;using System.Linq;using System.Net.Mime;+using System.Security.Cryptography;using System.Text;using System.Threading;using System.Threading.Tasks;@@ -405,9 +406,8 @@ public class SubtitleController : BaseJellyfinApiController[FromBody, Required] UploadSubtitleDto body){var video = (Video)_libraryManager.GetItemById(itemId);- var data = Convert.FromBase64String(body.Data);- var memoryStream = new MemoryStream(data, 0, data.Length, false, true);- await using (memoryStream.ConfigureAwait(false))+ var stream = new CryptoStream(Request.Body, new FromBase64Transform(), CryptoStreamMode.Read);+ await using (stream.ConfigureAwait(false)){await _subtitleManager.UploadSubtitle(video,@@ -417,7 +417,7 @@ public class SubtitleController : BaseJellyfinApiControllerLanguage = body.Language,IsForced = body.IsForced,IsHearingImpaired = body.IsHearingImpaired,- Stream = memoryStream+ Stream = stream}).ConfigureAwait(false);_providerManager.QueueRefresh(video.Id, new MetadataRefreshOptions(new DirectoryService(_fileSystem)), RefreshPriority.High);diff --git a/MediaBrowser.Providers/Manager/ImageSaver.cs b/MediaBrowser.Providers/Manager/ImageSaver.csindex e7c2cd2558..d827168314 100644--- a/MediaBrowser.Providers/Manager/ImageSaver.cs+++ b/MediaBrowser.Providers/Manager/ImageSaver.cs@@ -263,7 +263,11 @@ namespace MediaBrowser.Providers.Managervar fileStreamOptions = AsyncFile.WriteOptions;fileStreamOptions.Mode = FileMode.Create;- fileStreamOptions.PreallocationSize = source.Length;+ if (source.CanSeek)+ {+ fileStreamOptions.PreallocationSize = source.Length;+ }+var fs = new FileStream(path, fileStreamOptions);await using (fs.ConfigureAwait(false)){