diff --git a/lib/src/download_engine/connection/base_http_download_connection.dart b/lib/src/download_engine/connection/base_http_download_connection.dart index 0d9b486..82561b8 100644 --- a/lib/src/download_engine/connection/base_http_download_connection.dart +++ b/lib/src/download_engine/connection/base_http_download_connection.dart @@ -253,17 +253,17 @@ abstract class BaseHttpDownloadConnection { totalConnectionWriteProgress = 1; connectionStatus = DownloadStatus.connectionComplete; final totalExistingLength = getTotalWrittenBytesLength(); - totalDownloadProgress = totalExistingLength / downloadItem.contentLength; + totalDownloadProgress = totalExistingLength / downloadItem.fileSize; } void _updateDownloadProgress() { downloadProgress = totalRequestReceivedBytes / segment.length; totalDownloadProgress = - totalConnectionReceivedBytes / downloadItem.contentLength; + totalConnectionReceivedBytes / downloadItem.fileSize; if (downloadProgress > 1) { final excessBytes = totalConnectionReceivedBytes - segment.length; totalDownloadProgress = (totalConnectionReceivedBytes - excessBytes) / - downloadItem.contentLength; + downloadItem.fileSize; } } @@ -342,7 +342,7 @@ abstract class BaseHttpDownloadConnection { totalRequestWrittenBytes = totalRequestReceivedBytes; totalConnectionWrittenBytes = totalExistingLength; totalDownloadProgress = - totalConnectionReceivedBytes / downloadItem.contentLength; + totalConnectionReceivedBytes / downloadItem.fileSize; logger?.info( "totalRequestReceivedBytes: $totalRequestReceivedBytes " "totalConnectionReceivedBytes: $totalConnectionReceivedBytes " @@ -410,7 +410,7 @@ abstract class BaseHttpDownloadConnection { _notifyProgress(); return; } - if (receivedBytesMatchEndByte && endByte != downloadItem.contentLength) { + if (receivedBytesMatchEndByte && endByte != downloadItem.fileSize) { _onByteExactMatch(); _notifyProgress(); return; @@ -426,12 +426,12 @@ abstract class BaseHttpDownloadConnection { "Received bytes match endByte! " "closing the connection and flushing the buffer...", ); - if (this.endByte == downloadItem.contentLength) { + if (this.endByte == downloadItem.fileSize) { logger?.info("Connection corresponds to the last segment!"); totalRequestReceivedBytes += 3; totalConnectionReceivedBytes += 3; totalDownloadProgress = - totalConnectionReceivedBytes / downloadItem.contentLength; + totalConnectionReceivedBytes / downloadItem.fileSize; } client.close(); _flushBuffer(); @@ -464,7 +464,7 @@ abstract class BaseHttpDownloadConnection { final file = File(filePath) ..writeAsBytesSync(mode: FileMode.writeOnly, bytes); - if (tempFileStartByte > downloadItem.contentLength) { + if (tempFileStartByte > downloadItem.fileSize) { logger?.warn( "Attention:: Extremely Weird:: conn$connectionNumber::$segment " "byteExceed?$receivedBytesExceededEndByte " @@ -484,7 +484,7 @@ abstract class BaseHttpDownloadConnection { void _onTempFileWriteComplete(File file) { totalRequestWrittenBytes += file.lengthSync(); totalConnectionWriteProgress = - totalConnectionWrittenBytes / downloadItem.contentLength; + totalConnectionWrittenBytes / downloadItem.fileSize; totalRequestWriteProgress = totalRequestReceivedBytes / segment.length; if (totalRequestWriteProgress == 1) { connectionStatus = DownloadStatus.connectionComplete; @@ -542,7 +542,7 @@ abstract class BaseHttpDownloadConnection { totalRequestWrittenBytes = segment.length; } totalDownloadProgress = - totalConnectionReceivedBytes / downloadItem.contentLength; + totalConnectionReceivedBytes / downloadItem.fileSize; logger?.info("TempFiles fix complete"); } @@ -564,8 +564,8 @@ abstract class BaseHttpDownloadConnection { if (tempFiles.length == 1) { final file = basename(tempFiles[0].path); final fileEndByte = getEndByteFromTempFileName(file); - if (endByte == downloadItem.contentLength && - fileEndByte == downloadItem.contentLength - 1) { + if (endByte == downloadItem.fileSize && + fileEndByte == downloadItem.fileSize - 1) { return true; } if (endByte != fileEndByte) { @@ -588,8 +588,8 @@ abstract class BaseHttpDownloadConnection { "IsDownloadComplete::Found inconsistent ranges : ${basename(prevFile.path)} != ${basename(file.path)}"); } if (isLastFile) { - if (endByte == downloadItem.contentLength && - fileEndByte == downloadItem.contentLength - 1) { + if (endByte == downloadItem.fileSize && + fileEndByte == downloadItem.fileSize - 1) { return true; } if (fileEndByte != endByte) { @@ -750,7 +750,7 @@ abstract class BaseHttpDownloadConnection { bytesTransferRate = 0; downloadProgress = totalRequestReceivedBytes / segment.length; totalDownloadProgress = - totalConnectionReceivedBytes / downloadItem.contentLength; + totalConnectionReceivedBytes / downloadItem.fileSize; _flushBuffer(); connectionStatus = DownloadStatus.connectionComplete; logger?.info("Download complete with completion signal"); @@ -805,8 +805,8 @@ abstract class BaseHttpDownloadConnection { bool isStartNotAllowed(bool connectionReset, bool connectionReuse) { if (startByte >= endByte || - startByte > downloadItem.contentLength || - endByte > downloadItem.contentLength) { + startByte > downloadItem.fileSize || + endByte > downloadItem.fileSize) { logger?.warn("Invalid requested byte ranges $segment. Skipping..."); return true; } diff --git a/lib/src/download_engine/http_download_engine.dart b/lib/src/download_engine/http_download_engine.dart index e696dce..0b8d0f8 100644 --- a/lib/src/download_engine/http_download_engine.dart +++ b/lib/src/download_engine/http_download_engine.dart @@ -778,7 +778,7 @@ class HttpDownloadEngine { } logger?.info("Building tree..."); engineChannel.segmentTree = DownloadSegmentTree.buildFromMissingBytes( - data.downloadItem.contentLength, + data.downloadItem.fileSize, downloadSettings!.totalConnections, missingByteRanges, ); @@ -837,7 +837,7 @@ class HttpDownloadEngine { static List _findMissingByteRanges( DownloadItemModel downloadItem, ) { - final contentLength = downloadItem.contentLength; + final contentLength = downloadItem.fileSize; List? tempFiles; final tempDirPath = join( downloadSettings!.baseTempDir.path, @@ -849,7 +849,7 @@ class HttpDownloadEngine { } if (tempFiles == null || tempFiles.isEmpty) { - return [Segment(0, downloadItem.contentLength)]; + return [Segment(0, downloadItem.fileSize)]; } tempFiles.sort(sortByByteRanges); @@ -920,8 +920,8 @@ class HttpDownloadEngine { logger?.info("Found bad length :: ${basename(file.path)}"); tempFilesToDelete.add(file); } - if (start > downloadItem.contentLength || - end > downloadItem.contentLength) { + if (start > downloadItem.fileSize || + end > downloadItem.fileSize) { logger?.info( "Found byte range exceeding contentLength :: ${basename(file.path)}", ); @@ -1021,7 +1021,7 @@ class HttpDownloadEngine { fileToWrite.writeAsBytesSync(bytes, mode: FileMode.writeOnlyAppend); } final assembleSuccessful = - fileToWrite.lengthSync() == downloadItem.contentLength; + fileToWrite.lengthSync() == downloadItem.fileSize; if (assembleSuccessful) { _connectionIsolates[downloadItem.uid]?.values.forEach((isolate) { isolate.kill(); @@ -1029,7 +1029,7 @@ class HttpDownloadEngine { tempDir.deleteSync(recursive: true); } else { logger?.error( - "Assemble failed! written file length = ${fileToWrite.lengthSync()} expected file length = ${downloadItem.contentLength}", + "Assemble failed! written file length = ${fileToWrite.lengthSync()} expected file length = ${downloadItem.fileSize}", ); } if (notifyProgress) { @@ -1087,7 +1087,7 @@ class HttpDownloadEngine { _tempTime + 1000 > nowMillis || bytesTransferRate == 0) return; int totalBytes = 0; - final contentLength = progresses.values.first.downloadItem.contentLength; + final contentLength = progresses.values.first.downloadItem.fileSize; for (var element in progresses.values) { totalBytes += element.totalReceivedBytes; } @@ -1252,7 +1252,7 @@ class HttpDownloadEngine { static bool isAssembledFileInvalid(DownloadItemModel downloadItem) { final assembledFile = File(downloadItem.filePath); return assembledFile.existsSync() && - assembledFile.lengthSync() != downloadItem.contentLength; + assembledFile.lengthSync() != downloadItem.fileSize; } /// TODO should notify the progress while building the file instead of when the file has already been built @@ -1281,6 +1281,8 @@ class HttpDownloadEngine { fileName: fileInfo.fileName, downloadUrl: downloadUrl, progress: 0, + fileSize: fileInfo.contentLength, + supportsPause: fileInfo.supportsPause, ); } diff --git a/lib/src/download_engine/model/download_item_model.dart b/lib/src/download_engine/model/download_item_model.dart index 91b5681..3e8e0fc 100644 --- a/lib/src/download_engine/model/download_item_model.dart +++ b/lib/src/download_engine/model/download_item_model.dart @@ -11,7 +11,7 @@ class DownloadItemModel { DateTime? startDate; - int contentLength; + int fileSize; DateTime? finishDate; @@ -32,7 +32,7 @@ class DownloadItemModel { this.startDate, this.finishDate, required this.progress, - this.contentLength = 0, + this.fileSize = 0, this.fileType = "other", this.supportsPause = false, this.status = "In Queue",