mirror of
https://github.com/Stone-Red-Code/HyperbolicDownloader.git
synced 2026-09-08 07:46:23 +02:00
Fix important bug
- Fixed a bug that caused the content of the file to be sent as the filename
This commit is contained in:
@@ -114,7 +114,7 @@ public class DownloadCommands
|
|||||||
int bytesRead;
|
int bytesRead;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
bytesRead = nwStream.Read(buffer, 0, tcpClient.ReceiveBufferSize);
|
bytesRead = nwStream.Read(buffer, 0, 1000);
|
||||||
}
|
}
|
||||||
catch (IOException)
|
catch (IOException)
|
||||||
{
|
{
|
||||||
@@ -127,107 +127,106 @@ public class DownloadCommands
|
|||||||
|
|
||||||
string[] parts = dataReceived.Split('/');
|
string[] parts = dataReceived.Split('/');
|
||||||
|
|
||||||
if (parts.Length == 2) //If received data does not contain 2 parts -> error
|
if (parts.Length != 2) //If received data does not contain 2 parts -> error
|
||||||
{
|
{
|
||||||
bool validFileSize = int.TryParse(parts[0], out int fileSize);
|
ApiManager.SendMessageNewLine(dataReceived, NotificationMessageType.Error);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (!validFileSize || fileSize <= 0)
|
bool validFileSize = int.TryParse(parts[0], out int fileSize);
|
||||||
|
|
||||||
|
if (!validFileSize || fileSize <= 0)
|
||||||
|
{
|
||||||
|
ApiManager.SendMessageNewLine("Invalid file size!", NotificationMessageType.Error);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
string fileName = parts[1].ToFileName();
|
||||||
|
string directoryPath = Path.Combine(ApiConfiguration.BasePath, "Downloads");
|
||||||
|
string filePath = Path.Combine(directoryPath, fileName);
|
||||||
|
|
||||||
|
ApiManager.SendMessageNewLine($"File name: {fileName}");
|
||||||
|
ApiManager.SendMessageNewLine($"Starting download...");
|
||||||
|
|
||||||
|
int totalBytesRead = 0;
|
||||||
|
|
||||||
|
if (!Directory.Exists(directoryPath))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(directoryPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
using FileStream? fileStream = new FileStream(filePath, FileMode.Create);
|
||||||
|
|
||||||
|
int bytesInOneSecond = 0;
|
||||||
|
int unitsPerSecond = 0;
|
||||||
|
string unit = "Kb";
|
||||||
|
|
||||||
|
Stopwatch stopWatch = new Stopwatch();
|
||||||
|
stopWatch.Start();
|
||||||
|
while (totalBytesRead < fileSize)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
ApiManager.SendMessageNewLine("Invalid file size!", NotificationMessageType.Error);
|
bytesRead = nwStream.Read(reciveBuffer, 0, reciveBuffer.Length);
|
||||||
continue;
|
}
|
||||||
|
catch (IOException)
|
||||||
|
{
|
||||||
|
ApiManager.SendMessageNewLine(string.Empty);
|
||||||
|
ApiManager.SendMessageNewLine("Lost connection to other host!", NotificationMessageType.Error);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
string fileName = parts[1].ToFileName();
|
bytesRead = Math.Min(bytesRead, fileSize - totalBytesRead);
|
||||||
string directoryPath = Path.Combine(ApiConfiguration.BasePath, "Downloads");
|
|
||||||
string filePath = Path.Combine(directoryPath, fileName);
|
|
||||||
|
|
||||||
ApiManager.SendMessageNewLine($"File name: {fileName}");
|
fileStream.Write(reciveBuffer, 0, bytesRead);
|
||||||
ApiManager.SendMessageNewLine($"Starting download...");
|
totalBytesRead += bytesRead;
|
||||||
|
|
||||||
int totalBytesRead = 0;
|
bytesInOneSecond += bytesRead;
|
||||||
|
|
||||||
if (!Directory.Exists(directoryPath))
|
if (stopWatch.ElapsedMilliseconds >= 1000)
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(directoryPath);
|
unitsPerSecond = (unitsPerSecond + bytesInOneSecond) / 2;
|
||||||
}
|
if (unitsPerSecond > 125000)
|
||||||
|
|
||||||
using FileStream? fileStream = new FileStream(filePath, FileMode.Create);
|
|
||||||
|
|
||||||
int bytesInOneSecond = 0;
|
|
||||||
int unitsPerSecond = 0;
|
|
||||||
string unit = "Kb";
|
|
||||||
|
|
||||||
Stopwatch stopWatch = new Stopwatch();
|
|
||||||
stopWatch.Start();
|
|
||||||
while (totalBytesRead < fileSize)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
bytesRead = nwStream.Read(reciveBuffer, 0, reciveBuffer.Length);
|
unitsPerSecond /= 125000;
|
||||||
|
unit = "Mb";
|
||||||
}
|
}
|
||||||
catch (IOException)
|
else
|
||||||
{
|
{
|
||||||
ApiManager.SendMessageNewLine(string.Empty);
|
unitsPerSecond /= 125;
|
||||||
ApiManager.SendMessageNewLine("Lost connection to other host!", NotificationMessageType.Error);
|
unit = "Kb";
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
bytesInOneSecond = 0;
|
||||||
bytesRead = Math.Min(bytesRead, fileSize - totalBytesRead);
|
stopWatch.Restart();
|
||||||
|
|
||||||
fileStream.Write(reciveBuffer, 0, bytesRead);
|
|
||||||
totalBytesRead += bytesRead;
|
|
||||||
|
|
||||||
bytesInOneSecond += bytesRead;
|
|
||||||
|
|
||||||
if (stopWatch.ElapsedMilliseconds >= 1000)
|
|
||||||
{
|
|
||||||
unitsPerSecond = (unitsPerSecond + bytesInOneSecond) / 2;
|
|
||||||
if (unitsPerSecond > 125000)
|
|
||||||
{
|
|
||||||
unitsPerSecond /= 125000;
|
|
||||||
unit = "Mb";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
unitsPerSecond /= 125;
|
|
||||||
unit = "Kb";
|
|
||||||
}
|
|
||||||
bytesInOneSecond = 0;
|
|
||||||
stopWatch.Restart();
|
|
||||||
}
|
|
||||||
|
|
||||||
ApiManager.SendMessage($"\rDownloading: {Math.Clamp(Math.Ceiling(100d / fileSize * totalBytesRead), 0, 100)}% {totalBytesRead / 1000}/{fileSize / 1000}KB [{unitsPerSecond}{unit}/s] ");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fileStream.Close();
|
ApiManager.SendMessage($"\rDownloading: {Math.Clamp(Math.Ceiling(100d / fileSize * totalBytesRead), 0, 100)}% {totalBytesRead / 1000}/{fileSize / 1000}KB [{unitsPerSecond}{unit}/s] ");
|
||||||
|
}
|
||||||
|
|
||||||
if (totalBytesRead < fileSize)
|
fileStream.Close();
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ApiManager.SendMessageNewLine(string.Empty);
|
if (totalBytesRead < fileSize)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
ApiManager.SendMessageNewLine("Validating file...");
|
ApiManager.SendMessageNewLine(string.Empty);
|
||||||
if (FileValidator.ValidateHash(filePath, hash))
|
|
||||||
{
|
|
||||||
_ = filesManager.TryAdd(filePath, out _, out _);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ApiManager.SendMessageNewLine("Warning: File hash does not match! File might me corrupted or manipulated!", NotificationMessageType.Warning);
|
|
||||||
}
|
|
||||||
|
|
||||||
ApiManager.SendMessageNewLine($"File saved at: {Path.GetFullPath(filePath)}");
|
ApiManager.SendMessageNewLine("Validating file...");
|
||||||
ApiManager.SendMessageNewLine("Done", NotificationMessageType.Success);
|
if (FileValidator.ValidateHash(filePath, hash))
|
||||||
stopWatch.Stop();
|
{
|
||||||
hostsManager.SaveHosts();
|
_ = filesManager.TryAdd(filePath, out _, out _);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ApiManager.SendMessageNewLine(dataReceived, NotificationMessageType.Error);
|
ApiManager.SendMessageNewLine("Warning: File hash does not match! File might me corrupted or manipulated!", NotificationMessageType.Warning);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ApiManager.SendMessageNewLine($"File saved at: {Path.GetFullPath(filePath)}");
|
||||||
|
ApiManager.SendMessageNewLine("Done", NotificationMessageType.Success);
|
||||||
|
stopWatch.Stop();
|
||||||
|
hostsManager.SaveHosts();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
ApiManager.SendMessageNewLine("None of the available hosts have the requested file!", NotificationMessageType.Error);
|
ApiManager.SendMessageNewLine("None of the available hosts have the requested file!", NotificationMessageType.Error);
|
||||||
hostsManager.SaveHosts();
|
hostsManager.SaveHosts();
|
||||||
|
|||||||
@@ -167,6 +167,9 @@ namespace HyperbolicDownloaderApi.Networking
|
|||||||
FileInfo fileInfo = new FileInfo(hyperFileInfo.FilePath);
|
FileInfo fileInfo = new FileInfo(hyperFileInfo.FilePath);
|
||||||
|
|
||||||
bytesToSend = Encoding.ASCII.GetBytes($"{fileInfo.Length}/{Path.GetFileName(hyperFileInfo.FilePath)}");
|
bytesToSend = Encoding.ASCII.GetBytes($"{fileInfo.Length}/{Path.GetFileName(hyperFileInfo.FilePath)}");
|
||||||
|
|
||||||
|
Array.Resize(ref bytesToSend, 1000);
|
||||||
|
|
||||||
await nwStream.WriteAsync(bytesToSend);
|
await nwStream.WriteAsync(bytesToSend);
|
||||||
|
|
||||||
foreach (byte[]? chunk in FileCompressor.ReadChunks(hyperFileInfo.FilePath, 64000))
|
foreach (byte[]? chunk in FileCompressor.ReadChunks(hyperFileInfo.FilePath, 64000))
|
||||||
@@ -182,12 +185,15 @@ namespace HyperbolicDownloaderApi.Networking
|
|||||||
bytesToSend = Encoding.ASCII.GetBytes("File not found!");
|
bytesToSend = Encoding.ASCII.GetBytes("File not found!");
|
||||||
await nwStream.WriteAsync(bytesToSend);
|
await nwStream.WriteAsync(bytesToSend);
|
||||||
}
|
}
|
||||||
client.Close();
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Debug.WriteLine(ex);
|
Debug.WriteLine(ex);
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
client.Close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StopListening()
|
public void StopListening()
|
||||||
|
|||||||
Reference in New Issue
Block a user