Skip to content

Commit

Permalink
Add FileSize to the batch creation bypassing the initial head request…
Browse files Browse the repository at this point in the history
…s that are required.
  • Loading branch information
Ryan Feline committed Apr 17, 2019
1 parent c21651b commit db07286
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 6 deletions.
18 changes: 14 additions & 4 deletions library/src/main/java/com/novoda/downloadmanager/BatchFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ public class BatchFile {
private final String networkAddress;
private final String path;
private final Optional<DownloadFileId> downloadFileId;
private final Optional<FileSize> fileSize;

BatchFile(String networkAddress, Optional<DownloadFileId> downloadFileId, String path) {
public BatchFile(String networkAddress, String path, Optional<DownloadFileId> downloadFileId, Optional<FileSize> fileSize) {
this.networkAddress = networkAddress;
this.downloadFileId = downloadFileId;
this.path = path;
this.downloadFileId = downloadFileId;
this.fileSize = fileSize;
}

static InternalBatchFileBuilder from(StorageRoot storageRoot, DownloadBatchId downloadBatchId, String networkAddress) {
Expand All @@ -28,6 +30,10 @@ public Optional<DownloadFileId> downloadFileId() {
return downloadFileId;
}

public Optional<FileSize> fileSize() {
return fileSize;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -45,14 +51,18 @@ public boolean equals(Object o) {
if (path != null ? !path.equals(batchFile.path) : batchFile.path != null) {
return false;
}
return downloadFileId != null ? downloadFileId.equals(batchFile.downloadFileId) : batchFile.downloadFileId == null;
if (downloadFileId != null ? !downloadFileId.equals(batchFile.downloadFileId) : batchFile.downloadFileId != null) {
return false;
}
return fileSize != null ? fileSize.equals(batchFile.fileSize) : batchFile.fileSize == null;
}

@Override
public int hashCode() {
int result = networkAddress != null ? networkAddress.hashCode() : 0;
result = 31 * result + (path != null ? path.hashCode() : 0);
result = 31 * result + (downloadFileId != null ? downloadFileId.hashCode() : 0);
result = 31 * result + (fileSize != null ? fileSize.hashCode() : 0);
return result;
}

Expand All @@ -62,7 +72,7 @@ public String toString() {
+ "networkAddress='" + networkAddress + '\''
+ ", path='" + path + '\''
+ ", downloadFileId=" + downloadFileId
+ ", fileSize=" + fileSize
+ '}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public interface BatchFileBuilder {
*/
BatchFileBuilder saveTo(String path, String fileName);

BatchFileBuilder withSize(FileSize fileSize);

/**
* Creates a {@link BatchFile} from the {@link BatchFileBuilder} and
* adds it to the parent {@link BatchBuilder} before returning to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public BatchFile asBatchFile() {
DownloadFileId downloadFileId = DownloadFileIdCreator.createFrom(fileId);
return new BatchFile(
originalNetworkAddress,
newFileLocation,
Optional.of(downloadFileId),
newFileLocation
Optional.of(fileSize)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ static DownloadBatch newInstance(Batch batch,
String networkAddress = batchFile.networkAddress();

InternalFileSize fileSize = InternalFileSizeCreator.unknownFileSize();
if (batchFile.fileSize().isPresent()) {
fileSize = InternalFileSizeCreator.from(batchFile.fileSize().get());
}

FilePersistence filePersistence = fileOperations.filePersistenceCreator().create();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ static InternalFileSize unknownFileSize() {
static InternalFileSize createFromCurrentAndTotalSize(long currentSize, long totalSize) {
return new LiteFileSize(currentSize, totalSize);
}

static InternalFileSize from(FileSize fileSize) {
return new LiteFileSize(fileSize.currentSize(), fileSize.totalSize());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class LiteBatchFileBuilder implements InternalBatchFileBuilder {
private Optional<DownloadFileId> downloadFileId = Optional.absent();
private Optional<String> path = Optional.absent();
private Optional<String> fileName = Optional.absent();
private Optional<FileSize> fileSize = Optional.absent();

private InternalBatchBuilder parentBuilder;

Expand Down Expand Up @@ -49,6 +50,12 @@ public BatchFileBuilder saveTo(String path, String fileName) {
return this;
}

@Override
public BatchFileBuilder withSize(FileSize fileSize) {
this.fileSize = Optional.fromNullable(fileSize);
return this;
}

@Override
public BatchBuilder apply() {
String absolutePath = buildPath(
Expand All @@ -58,7 +65,7 @@ public BatchBuilder apply() {
fileName.getOrElse(() -> FileNameExtractor.extractFrom(networkAddress))
);

parentBuilder.withFile(new BatchFile(networkAddress, downloadFileId, absolutePath));
parentBuilder.withFile(new BatchFile(networkAddress, absolutePath, downloadFileId, fileSize));
return parentBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.novoda.downloadmanager;

import android.util.Log;

import java.io.IOException;

class NetworkFileSizeRequester implements FileSizeRequester {
Expand All @@ -18,6 +20,7 @@ class NetworkFileSizeRequester implements FileSizeRequester {

@Override
public FileSize requestFileSize(String url) {
Log.e("TAG", "requestFileSize: " + url);
try {
long fileSize = executeRequestFileSize(url);
if (fileSize == UNKNOWN_CONTENT_LENGTH || fileSize == ZERO_FILE_SIZE) {
Expand Down

0 comments on commit db07286

Please sign in to comment.