Increase 'size' of downlaod if we've written more than the previous expected size

This commit is contained in:
anthony 2020-12-16 20:17:54 +00:00
parent b0286b674a
commit 38b61f46e7
3 changed files with 15 additions and 11 deletions

View File

@ -11,8 +11,8 @@
@interface DownloadItem: NSObject {
BOOL completed;
BOOL cancelled;
NSInteger size;
NSInteger written;
NSUInteger size;
NSUInteger written;
NSInteger index;
NSDate *startDate;
NSURL *destination;
@ -38,7 +38,7 @@
id<DownloadManagerDelegate> delegate;
}
+(DownloadManager*)defaultDownloadManager;
-(DownloadItem*)createDownloadForDestination: (NSURL*)path withSizeInBytes: (NSInteger)size;
-(DownloadItem*)createDownloadForDestination: (NSURL*)path withSizeInBytes: (NSUInteger)size;
-(NSArray*)downloads;
-(id)delegate;
-(void)setDelegate: (id<DownloadManagerDelegate>)aDelegate;

View File

@ -3,7 +3,7 @@
@implementation DownloadItem
-(id)initWithManager: (DownloadManager*)aManager destination: (NSURL*)aDestination size: (NSInteger)aSize index: (NSInteger)anIndex {
-(id)initWithManager: (DownloadManager*)aManager destination: (NSURL*)aDestination size: (NSInteger)aSize index: (NSUInteger)anIndex {
if (self = [super init]) {
error = nil;
index = anIndex;
@ -34,9 +34,13 @@
}
-(BOOL)appendToDownload: (NSData*)data {
NSInteger len = [data length];
NSInteger writtenNow = [outputStream write: [data bytes] maxLength: len];
NSUInteger len = [data length];
NSUInteger writtenNow = [outputStream write: [data bytes] maxLength: len];
written += writtenNow;
// Unless im misunderstanding download_context_get_total_length appears to return
// a too-small non-zero value for download size when called in
// gnustep_download_create...
size = MAX(written, size);
[[manager delegate] downloadManager: manager didUpdateItem: self];
return writtenNow == len;
}
@ -78,7 +82,7 @@
if (completed) {
return @"-";
}
NSInteger bytesLeft = size - written;
NSUInteger bytesLeft = size - written;
double kibLeft = (double)bytesLeft / 1024.0;
return [NSString stringWithFormat: @"%.2f KiB", kibLeft];
}
@ -96,6 +100,7 @@
if (written == size) {
return 1.0;
} else {
NSLog(@"prog: %f", (double)written / size);
return (double)written / size;
}
}
@ -144,7 +149,7 @@
[super dealloc];
}
-(DownloadItem*)createDownloadForDestination: (NSURL*)path withSizeInBytes: (NSInteger)size {
-(DownloadItem*)createDownloadForDestination: (NSURL*)path withSizeInBytes: (NSUInteger)size {
DownloadItem *item = [[DownloadItem alloc] initWithManager: self destination: path
size: size index: [downloads count]];
[downloads addObject: item];

View File

@ -9,12 +9,12 @@
/**********************/
/****** Download ******/
/**********************/
// This won't really return a window ref, but a ref to a download item.
static struct gui_download_window *gnustep_download_create(struct download_context *ctx, struct gui_window *parent) {
NSLog(@"gnustep_download_create");
NSURL *url = [[NSApp delegate] requestDownloadDestination];
NSInteger dataSize = download_context_get_total_length(ctx);
// TODO: - dataSize is smaller than the actual size in some cases. Why?
NSUInteger dataSize = download_context_get_total_length(ctx);
DownloadItem *download = [[DownloadManager defaultDownloadManager]
createDownloadForDestination: url withSizeInBytes: dataSize];
[[NSApp delegate] showDownloadsWindow: nil];
@ -24,7 +24,6 @@ static struct gui_download_window *gnustep_download_create(struct download_conte
// ??
static nserror gnustep_download_data(struct gui_download_window *dw, const char *data, unsigned int size) {
NSLog(@"gnustep_download_data");
BOOL success = [(id)dw appendToDownload: [NSData dataWithBytesNoCopy: (void*)data
length: size freeWhenDone: NO]];
if (success) {