diff options
| author | Bond-009 <bond.009@outlook.com> | 2019-12-19 17:46:17 +0100 |
|---|---|---|
| committer | Bond-009 <bond.009@outlook.com> | 2019-12-19 17:46:17 +0100 |
| commit | bb62dd14c2fee3f7a6fa8cb3d8592500046ed3c9 (patch) | |
| tree | c527cb62ab3d1205e783c87d7e639e180b088f55 | |
| parent | a5cd11735cc0320e576605410b68e4dace6caa05 (diff) | |
Limit size for playbacktest
| -rw-r--r-- | MediaBrowser.Api/Playback/MediaInfoService.cs | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/MediaBrowser.Api/Playback/MediaInfoService.cs b/MediaBrowser.Api/Playback/MediaInfoService.cs index 8caa03303..ac1cba09a 100644 --- a/MediaBrowser.Api/Playback/MediaInfoService.cs +++ b/MediaBrowser.Api/Playback/MediaInfoService.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.Globalization; using System.Linq; @@ -54,7 +55,7 @@ namespace MediaBrowser.Api.Playback public class GetBitrateTestBytes { [ApiMember(Name = "Size", Description = "Size", IsRequired = true, DataType = "int", ParameterType = "query", Verb = "GET")] - public long Size { get; set; } + public int Size { get; set; } public GetBitrateTestBytes() { @@ -101,8 +102,31 @@ namespace MediaBrowser.Api.Playback public object Get(GetBitrateTestBytes request) { - var bytes = new byte[request.Size]; - return ResultFactory.GetResult(null, bytes, "application/octet-stream"); + const int MaxSize = 10_000_000; + + var size = request.Size; + + if (size <= 0) + { + throw new ArgumentException($"The requested size can't be equal or smaller than 0.", nameof(request)); + } + + if (size > MaxSize) + { + throw new ArgumentException($"The requested size can't be larger than the max allowed value ({MaxSize}).", nameof(request)); + } + + byte[] buffer = ArrayPool<byte>.Shared.Rent(size); + try + { + // ArrayPool<byte>.Shared.Rent doesn't guarantee that the returned buffer is zeroed + Array.Fill<byte>(buffer, 0); + return ResultFactory.GetResult(null, buffer, "application/octet-stream"); + } + finally + { + ArrayPool<byte>.Shared.Return(buffer); + } } public async Task<object> Get(GetPlaybackInfo request) |
