diff --git a/TelegramDownloader/Controllers/FileController.cs b/TelegramDownloader/Controllers/FileController.cs index 59e0bfe..8405d82 100644 --- a/TelegramDownloader/Controllers/FileController.cs +++ b/TelegramDownloader/Controllers/FileController.cs @@ -277,18 +277,20 @@ public async Task GetFileStream(string idChannel, string idFile, var rangeHeader = request.Headers["Range"].ToString(); var file = await _fs.getItemById(idChannel, idFile); + long totalLength = file.Size; TL.Message idM = await _ts.getMessageFile(idChannel, file.MessageId ?? file.ListMessageId.FirstOrDefault()); - long totalLength = 0; // (await _fs.getItemById(idChannel, idFile)).Size; + long totalPartialFileLength = 0; - if(idM.media is MessageMediaDocument { document: Document document }) + if (idM.media is MessageMediaDocument { document: Document document }) { - totalLength = document.size; + totalPartialFileLength = document.size; } long from = 0; long initialFrom = 0; + long initialPartFrom = 0; long to = 0; if (!string.IsNullOrEmpty(rangeHeader) && rangeHeader.StartsWith("bytes=")) @@ -296,10 +298,28 @@ public async Task GetFileStream(string idChannel, string idFile, var range = rangeHeader.Replace("bytes=", "").Split('-'); from = long.Parse(range[0]); initialFrom = from; + initialPartFrom = from; if (range.Length > 1 && !string.IsNullOrEmpty(range[1])) to = long.Parse(range[1]); } + if (from >= totalPartialFileLength) + { + Console.WriteLine("Take a file part: from: " + from + ", totalPartialFileLength: " + totalPartialFileLength); + int filePart = 0; + while(from >= totalPartialFileLength) + { + filePart++; + from -= totalPartialFileLength; + } + idM = await _ts.getMessageFile(idChannel, file.ListMessageId[filePart]); + if (idM.media is MessageMediaDocument { document: Document document2 }) + { + totalPartialFileLength = document2.size; + } + initialPartFrom = from; + } + if (from > 0) { from = (from / 524288) * 524288; @@ -315,12 +335,12 @@ public async Task GetFileStream(string idChannel, string idFile, else to = ((to + 524288) / 524288) * 524288; - if (to > totalLength) + if (to > totalPartialFileLength) { - to = totalLength; + to = totalPartialFileLength; } - if (totalLength == initialFrom) + if (totalPartialFileLength == initialPartFrom) { Response.StatusCode = 416; // Range Not Satisfiable Response.Headers["Content-Range"] = $"bytes */{totalLength}"; @@ -335,7 +355,7 @@ public async Task GetFileStream(string idChannel, string idFile, //Console.WriteLine("Total download length: " + data.Length); - long skipedBytes = initialFrom - from - (length - data.Length); + long skipedBytes = initialPartFrom - from - (length - data.Length); if (skipedBytes < 0) skipedBytes = 0; diff --git a/TelegramDownloader/TelegramDownloader.csproj b/TelegramDownloader/TelegramDownloader.csproj index f3d1bac..39718bc 100644 --- a/TelegramDownloader/TelegramDownloader.csproj +++ b/TelegramDownloader/TelegramDownloader.csproj @@ -15,13 +15,13 @@ - + - - + +