Skip to content

Commit

Permalink
Add fileSize and supportsPause to DownloadItemModel
Browse files Browse the repository at this point in the history
  • Loading branch information
AminBhst committed Dec 9, 2024
1 parent 14e4e38 commit 0e6d974
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -342,7 +342,7 @@ abstract class BaseHttpDownloadConnection {
totalRequestWrittenBytes = totalRequestReceivedBytes;
totalConnectionWrittenBytes = totalExistingLength;
totalDownloadProgress =
totalConnectionReceivedBytes / downloadItem.contentLength;
totalConnectionReceivedBytes / downloadItem.fileSize;
logger?.info(
"totalRequestReceivedBytes: $totalRequestReceivedBytes "
"totalConnectionReceivedBytes: $totalConnectionReceivedBytes "
Expand Down Expand Up @@ -410,7 +410,7 @@ abstract class BaseHttpDownloadConnection {
_notifyProgress();
return;
}
if (receivedBytesMatchEndByte && endByte != downloadItem.contentLength) {
if (receivedBytesMatchEndByte && endByte != downloadItem.fileSize) {
_onByteExactMatch();
_notifyProgress();
return;
Expand All @@ -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();
Expand Down Expand Up @@ -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 "
Expand All @@ -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;
Expand Down Expand Up @@ -542,7 +542,7 @@ abstract class BaseHttpDownloadConnection {
totalRequestWrittenBytes = segment.length;
}
totalDownloadProgress =
totalConnectionReceivedBytes / downloadItem.contentLength;
totalConnectionReceivedBytes / downloadItem.fileSize;
logger?.info("TempFiles fix complete");
}

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}
Expand Down
20 changes: 11 additions & 9 deletions lib/src/download_engine/http_download_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ class HttpDownloadEngine {
}
logger?.info("Building tree...");
engineChannel.segmentTree = DownloadSegmentTree.buildFromMissingBytes(
data.downloadItem.contentLength,
data.downloadItem.fileSize,
downloadSettings!.totalConnections,
missingByteRanges,
);
Expand Down Expand Up @@ -837,7 +837,7 @@ class HttpDownloadEngine {
static List<Segment> _findMissingByteRanges(
DownloadItemModel downloadItem,
) {
final contentLength = downloadItem.contentLength;
final contentLength = downloadItem.fileSize;
List<File>? tempFiles;
final tempDirPath = join(
downloadSettings!.baseTempDir.path,
Expand All @@ -849,7 +849,7 @@ class HttpDownloadEngine {
}

if (tempFiles == null || tempFiles.isEmpty) {
return [Segment(0, downloadItem.contentLength)];
return [Segment(0, downloadItem.fileSize)];
}

tempFiles.sort(sortByByteRanges);
Expand Down Expand Up @@ -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)}",
);
Expand Down Expand Up @@ -1021,15 +1021,15 @@ 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();
});
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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1281,6 +1281,8 @@ class HttpDownloadEngine {
fileName: fileInfo.fileName,
downloadUrl: downloadUrl,
progress: 0,
fileSize: fileInfo.contentLength,
supportsPause: fileInfo.supportsPause,
);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/download_engine/model/download_item_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DownloadItemModel {

DateTime? startDate;

int contentLength;
int fileSize;

DateTime? finishDate;

Expand All @@ -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",
Expand Down

0 comments on commit 0e6d974

Please sign in to comment.