Skip to content

Commit 7d505ea

Browse files
Minor code adjustments for HugeMemoryStream and StaticFileSystem MediaList.
Minor code adjustments for HugeMemoryStream and StaticFileSystem MediaList.
1 parent 90de5fd commit 7d505ea

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
lines changed

BackendServices/CyberBackendLibrary/HTTP/HugeMemoryStream.cs renamed to BackendServices/CyberBackendLibrary/Extension/HugeMemoryStream.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.IO;
33

4-
namespace CyberBackendLibrary.HTTP
4+
namespace CyberBackendLibrary.Extension
55
{
66
// This class removes the 2gb limit of the classic MemoryStream (but might consume more ram).
77
public class HugeMemoryStream : Stream
@@ -25,32 +25,23 @@ public class HugeMemoryStream : Stream
2525

2626
public HugeMemoryStream(Stream st)
2727
{
28-
st.Position = 0;
2928
st.CopyTo(this);
30-
st.Close();
31-
st.Dispose();
32-
Position = 0;
3329
}
3430

3531
public HugeMemoryStream(Stream st, long BufferSize)
3632
{
3733
int bytesRead = 0;
38-
byte[] buffer = new byte[BufferSize];
39-
40-
// Read from source and write to destination in chunks
41-
while ((bytesRead = st.Read(buffer, 0, buffer.Length)) > 0)
34+
Span<byte> buffer = new byte[BufferSize];
35+
while ((bytesRead = st.Read(buffer)) > 0)
4236
{
43-
Write(buffer, 0, bytesRead);
37+
Write(buffer[..bytesRead]);
4438
}
45-
st.Close();
46-
st.Dispose();
47-
Position = 0;
39+
Flush();
4840
}
4941

5042
public HugeMemoryStream(Span<byte> SpanToMem)
5143
{
5244
Write(SpanToMem);
53-
Position = 0;
5445
}
5546

5647
public HugeMemoryStream()
@@ -62,7 +53,7 @@ private int GetPageCount(long length)
6253
{
6354
int pageCount = (int)(length / PAGE_SIZE) + 1;
6455

65-
if ((length % PAGE_SIZE) == 0)
56+
if (length % PAGE_SIZE == 0)
6657
pageCount--;
6758

6859
return pageCount;

BackendServices/CyberBackendLibrary/FileSystem/StaticFileSystem.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ public static IEnumerable<FileSystemInfo> AllFilesAndFoldersLinq(this DirectoryI
3131

3232
public static IEnumerable<string>? GetMediaFilesList(string directoryPath)
3333
{
34+
// Define a set of valid extensions for media quick lookup
35+
HashSet<string> validExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".mp3", ".aac", ".ts" };
36+
3437
return Directory.EnumerateFiles(directoryPath, "*.*")
35-
.Where(s => (s.ToLower().EndsWith(".mp3") || s.ToLower().EndsWith(".aac") || s.ToLower().EndsWith(".ts")) && !File.GetAttributes(s).HasFlag(FileAttributes.Hidden));
38+
.Where(s => validExtensions.Contains(Path.GetExtension(s)) && !File.GetAttributes(s).HasFlag(FileAttributes.Hidden));
3639
}
3740

3841
public static string? GetM3UStreamFromDirectory(string directoryPath, string httpdirectoryrequest)

BackendServices/CyberBackendLibrary/HTTP/HTTPProcessor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Text.RegularExpressions;
1313
using System.Web;
1414
using System.Threading;
15+
using CyberBackendLibrary.Extension;
1516

1617
namespace CyberBackendLibrary.HTTP
1718
{

WebServers/HTTPSecureServerLite/LocalFileStreamHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using CyberBackendLibrary.HTTP;
55
using System.IO;
66
using System;
7+
using CyberBackendLibrary.Extension;
78

89
namespace HTTPSecureServerLite
910
{

WebServers/HTTPServer/HttpProcessor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
using Newtonsoft.Json;
4040
using WebAPIService.UBISOFT.gsconnect;
4141
using CyberBackendLibrary.Crypto;
42+
using CyberBackendLibrary.Extension;
4243

4344
namespace HTTPServer
4445
{
@@ -962,13 +963,15 @@ public void HandleClient(TcpClient tcpClient, ushort ListenerPort)
962963
if (vid != null && vid.Available && vid.VideoStream != null)
963964
{
964965
using HugeMemoryStream ms = new(vid.VideoStream, HTTPServerConfiguration.BufferSize);
966+
vid.VideoStream.Close();
967+
vid.VideoStream.Dispose();
968+
ms.Position = 0;
965969
response = new(request.RetrieveHeaderValue("Connection") == "keep-alive")
966970
{
967971
HttpStatusCode = Models.HttpStatusCode.OK
968972
};
969973
response.Headers.Add("Content-Type", vid.ContentType);
970974
response.Headers.Add("Content-Length", ms.Length.ToString());
971-
ms.Flush();
972975
}
973976
else
974977
response = HttpBuilder.InternalServerError();

WebServers/HTTPServer/Models/HttpResponse.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Copyright (C) 2016 by Barend Erasmus and donated to the public domain
2-
using CyberBackendLibrary.HTTP;
32
using HTTPServer.Extensions;
43
using System;
54
using System.Collections.Generic;
@@ -155,7 +154,14 @@ public static HttpResponse Send(Stream? streamtosend, string mimetype = "text/pl
155154
if (streamtosend.CanSeek)
156155
response.ContentStream = streamtosend;
157156
else
158-
response.ContentStream = new HugeMemoryStream(streamtosend, HTTPServerConfiguration.BufferSize);
157+
{
158+
response.ContentStream = new CyberBackendLibrary.Extension.HugeMemoryStream(streamtosend, HTTPServerConfiguration.BufferSize)
159+
{
160+
Position = 0
161+
};
162+
streamtosend.Close();
163+
streamtosend.Dispose();
164+
}
159165
}
160166
else
161167
response.ContentStream = null;

0 commit comments

Comments
 (0)